1 // Connection.java - Implementation of URLConnection for file protocol.
3 /* Copyright (C) 1999 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 package gnu.gcj.protocol.file;
15 import java.util.Vector;
16 import java.util.Hashtable;
17 import java.util.Enumeration;
20 * @author Warren Levy <warrenl@cygnus.com>
21 * @date April 13, 1999.
25 * Written using on-line Java Platform 1.2 API Specification, as well
26 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
27 * Status: Minimal subset of functionality.
30 class Connection extends URLConnection
32 private Hashtable hdrHash = new Hashtable();
33 private Vector hdrVec = new Vector();
34 private boolean gotHeaders = false;
37 public Connection(URL url)
42 // Implementation of abstract method.
43 public void connect() throws IOException
45 // Call is ignored if already connected.
49 // If not connected, then file needs to be openned.
50 fileIn = new File(url.getFile());
55 throw new FileNotFoundException("No such file or directory");
58 public InputStream getInputStream() throws IOException
64 throw new ProtocolException("Can't open InputStream if doInput is false");
65 return new BufferedInputStream(new FileInputStream(fileIn));
68 // Override default method in URLConnection.
69 public OutputStream getOutputStream() throws IOException
76 ProtocolException("Can't open OutputStream if doOutput is false");
77 return new BufferedOutputStream(new FileOutputStream(fileIn));
80 // Override default method in URLConnection.
81 public String getHeaderField(String name)
91 return (String) hdrHash.get(name.toLowerCase());
94 // Override default method in URLConnection.
95 public String getHeaderField(int n)
101 catch (IOException x)
105 if (n < hdrVec.size())
106 return getField((String) hdrVec.elementAt(n));
111 // Override default method in URLConnection.
112 public String getHeaderFieldKey(int n)
118 catch (IOException x)
122 if (n < hdrVec.size())
123 return getKey((String) hdrVec.elementAt(n));
128 private String getKey(String str)
132 int index = str.indexOf(':');
134 return str.substring(0, index);
139 private String getField(String str)
143 int index = str.indexOf(':');
145 return str.substring(index + 1).trim();
150 private void getHeaders() throws IOException
158 // Yes, it is overkill to use the hash table and vector here since
159 // we're only putting one header in the file, but in case we need
160 // to add others later and for consistency, we'll implement it this way.
162 // Add the only header we know about right now: Content-length.
163 long len = fileIn.length();
164 String line = "Content-length: " + len;
165 hdrVec.addElement(line);
167 // The key will never be null in this scenario since we build up the
168 // headers ourselves. If we ever rely on getting a header from somewhere
169 // else, then we may have to check if the result of getKey() is null.
170 String key = getKey(line);
171 hdrHash.put(key.toLowerCase(), Long.toString(len));