OSDN Git Service

e9ef08f2c30655fe8946d2e4775de92670d156dc
[pf3gnuchains/gcc-fork.git] / libjava / gnu / gcj / protocol / file / Connection.java
1 // Connection.java - Implementation of URLConnection for file protocol.
2
3 /* Copyright (C) 1999  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 package gnu.gcj.protocol.file;
12
13 import java.net.*;
14 import java.io.*;
15 import java.util.Vector;
16 import java.util.Hashtable;
17 import java.util.Enumeration;
18
19 /**
20  * @author Warren Levy <warrenl@cygnus.com>
21  * @date April 13, 1999.
22  */
23
24 /**
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.
28  */
29
30 class Connection extends URLConnection
31 {
32   private Hashtable hdrHash = new Hashtable();
33   private Vector hdrVec = new Vector();
34   private boolean gotHeaders = false;
35   private File fileIn;
36
37   public Connection(URL url)
38   {
39     super(url);
40   }
41
42   // Implementation of abstract method.
43   public void connect() throws IOException
44   {
45     // Call is ignored if already connected.
46     if (connected)
47       return;
48
49     // If not connected, then file needs to be openned.
50     fileIn = new File(url.getFile());
51     
52     if (fileIn.exists())
53       connected = true;
54     else
55       throw new FileNotFoundException("No such file or directory");
56   }
57
58   public InputStream getInputStream() throws IOException
59   {
60     if (!connected)
61       connect();
62
63     if (! doInput)
64       throw new ProtocolException("Can't open InputStream if doInput is false");
65     return new BufferedInputStream(new FileInputStream(fileIn));
66   }
67
68   // Override default method in URLConnection.
69   public OutputStream getOutputStream() throws IOException
70   {
71     if (!connected)
72       connect();
73
74     if (! doOutput)
75       throw new
76         ProtocolException("Can't open OutputStream if doOutput is false");
77     return new BufferedOutputStream(new FileOutputStream(fileIn));
78   }
79
80   // Override default method in URLConnection.
81   public String getHeaderField(String name)
82   {
83     try
84       {
85         getHeaders();
86       }
87     catch (IOException x)
88       {
89         return null;
90       }
91     return (String) hdrHash.get(name.toLowerCase());
92   }
93
94   // Override default method in URLConnection.
95   public String getHeaderField(int n)
96   {
97     try
98       {
99         getHeaders();
100       }
101     catch (IOException x)
102       {
103         return null;
104       }
105     if (n < hdrVec.size())
106       return getField((String) hdrVec.elementAt(n));
107
108     return null;
109   }
110
111   // Override default method in URLConnection.
112   public String getHeaderFieldKey(int n)
113   {
114     try
115       {
116         getHeaders();
117       }
118     catch (IOException x)
119       {
120         return null;
121       }
122     if (n < hdrVec.size())
123       return getKey((String) hdrVec.elementAt(n));
124
125     return null;
126   }
127
128   private String getKey(String str)
129   {
130     if (str == null)
131       return null;
132     int index = str.indexOf(':');
133     if (index >= 0)
134       return str.substring(0, index);
135     else
136       return null;
137   }
138
139   private String getField(String str)
140   {
141     if (str == null)
142       return null;
143     int index = str.indexOf(':');
144     if (index >= 0)
145       return str.substring(index + 1).trim();
146     else
147       return str;
148   }
149
150   private void getHeaders() throws IOException
151   {
152     if (gotHeaders)
153       return;
154     gotHeaders = true;
155
156     connect();
157
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.
161
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);
166
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));
172   }
173 }