OSDN Git Service

* All files: Updated copyright to reflect Cygnus purchase.
[pf3gnuchains/gcc-fork.git] / libjava / java / net / JarURLConnection.java
1 /* Copyright (C) 1999  Red Hat, Inc.
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 package java.net;
10
11 import java.net.*;
12 import java.io.*;
13 import java.util.jar.*;
14 import java.util.zip.*;
15 import java.util.Vector;
16 import java.util.Hashtable;
17
18 /**
19  * @author Kresten Krab Thorup <krab@gnu.org>
20  * @date Aug 10, 1999.
21  */
22
23
24 public abstract class JarURLConnection extends URLConnection
25 {
26   // three different ways to say the same thing
27   private final URL jarFileURL;
28
29   /** The connection to the jar file itself. A JarURLConnection
30    *  can represent an entry in a jar file or an entire jar file.  In
31    *  either case this describes just the jar file itself. */
32   protected URLConnection jarFileURLConnection;
33
34   // If this is a connection to a jar file element this is set, otherwose null.
35   private final String element;
36
37   // Cached JarURLConnection's 
38   static Hashtable conn_cache = new Hashtable();
39
40   public URL getJarFileURL ()
41   {
42     return jarFileURL;
43   }
44
45   public String getEntryName ()
46   {
47     return element;
48   }
49
50   public JarURLConnection(URL url)
51     throws MalformedURLException
52   {
53     super(url);
54
55     String spec = url.getFile();
56     int bang = spec.indexOf ("!/", 0);
57     if (bang == -1)
58       throw new MalformedURLException (url + ": No `!/' in spec.");
59
60     // Extact the url for the jar itself.
61     jarFileURL = new URL(spec.substring (0, bang));
62
63     // Get the name of the element, if any.
64     element = (bang+2==spec.length() ? null : spec.substring (bang+2));
65   }
66
67   public synchronized void connect() throws IOException
68   {
69     // Call is ignored if already connected.
70     if (connected)
71       return;
72
73     if (getUseCaches())
74       {
75         jarFileURLConnection = (URLConnection) conn_cache.get (jarFileURL);
76
77         if (jarFileURLConnection == null)
78           {
79             jarFileURLConnection = jarFileURL.openConnection ();
80             jarFileURLConnection.setUseCaches (true);
81             jarFileURLConnection.connect ();
82             conn_cache.put (jarFileURL, jarFileURLConnection);
83           }
84       }
85     else
86       {
87         jarFileURLConnection = jarFileURL.openConnection ();
88         jarFileURLConnection.connect ();
89       }
90
91     connected = true;
92   }
93
94   public InputStream getInputStream() throws IOException
95   {
96     if (!connected)
97       connect();
98
99     if (! doInput)
100       throw new ProtocolException("Can't open InputStream if doInput is false");
101
102     if (element == null)
103       {
104         // This is a JarURLConnection for the entire jar file.  
105
106         InputStream jar_is = new BufferedInputStream(jarFileURLConnection.getInputStream ());
107         return new JarInputStream(jar_is);
108       }
109
110     // Reaching this point, we're looking for an element of a jar file.
111
112     JarFile jarfile = null;
113
114     try
115       {
116         jarfile = getJarFile ();
117       }
118     catch (java.io.IOException x)
119       {
120         /* ignore */
121       }
122     
123     if (jarfile != null)
124       {
125         // this is the easy way...
126         return jarfile.getInputStream (jarfile.getEntry (element));
127       }
128     else
129       {
130         // If the jar file is not local, ...
131         JarInputStream zis = new JarInputStream(jarFileURLConnection.getInputStream ());
132
133         // This is hideous, we're doing a linear search...
134         for (ZipEntry ent = zis.getNextEntry (); 
135              ent != null; 
136              ent = zis.getNextEntry ())
137           {
138             if (element.equals (ent.getName ()))
139               {
140                 int size = (int)ent.getSize();
141                 byte[] data = new byte[size];
142                 zis.read (data, 0, size);
143                 return new ByteArrayInputStream (data);
144               }
145           }
146       }
147
148     return null;
149   }
150
151   public JarEntry getJarEntry (String name)
152     throws java.io.IOException
153   {
154     JarFile jarfile = null;
155
156     if (! doInput)
157       throw new ProtocolException("Can't open JarEntry if doInput is false");
158
159     try
160       {
161         jarfile = getJarFile ();
162       }
163     catch (java.io.IOException x)
164       {
165         /* ignore */
166       }
167     
168     if (jarfile == null)
169       {
170         JarInputStream zis = new JarInputStream(jarFileURLConnection.getInputStream ());
171
172         // This is hideous, we're doing a linear search for the thing...
173         for (ZipEntry ent = zis.getNextEntry (); 
174              ent != null; 
175              ent = zis.getNextEntry ())
176           {
177             if (element.equals (ent.getName ()))
178               {
179                 return new JarEntry (ent);
180               }
181           }
182       }
183
184     else
185       {
186         return jarfile.getJarEntry (element);
187       }
188
189     return null;
190   }
191
192   public abstract JarFile getJarFile() throws java.io.IOException;
193
194
195   // Steal and borrow from protocol/file/Connection.java
196
197   private Hashtable hdrHash = new Hashtable();
198   private Vector hdrVec = new Vector();
199   private boolean gotHeaders = false;
200
201   // Override default method in URLConnection.
202   public String getHeaderField(String name)
203   {
204     try
205       {
206         getHeaders();
207       }
208     catch (IOException x)
209       {
210         return null;
211       }
212     return (String) hdrHash.get(name.toLowerCase());
213   }
214
215   // Override default method in URLConnection.
216   public String getHeaderField(int n)
217   {
218     try
219       {
220         getHeaders();
221       }
222     catch (IOException x)
223       {
224         return null;
225       }
226     if (n < hdrVec.size())
227       return getField((String) hdrVec.elementAt(n));
228
229     return null;
230   }
231
232   // Override default method in URLConnection.
233   public String getHeaderFieldKey(int n)
234   {
235     try
236       {
237         getHeaders();
238       }
239     catch (IOException x)
240       {
241         return null;
242       }
243     if (n < hdrVec.size())
244       return getKey((String) hdrVec.elementAt(n));
245
246     return null;
247   }
248
249   private String getKey(String str)
250   {
251     if (str == null)
252       return null;
253     int index = str.indexOf(':');
254     if (index >= 0)
255       return str.substring(0, index);
256     else
257       return null;
258   }
259
260   private String getField(String str)
261   {
262     if (str == null)
263       return null;
264     int index = str.indexOf(':');
265     if (index >= 0)
266       return str.substring(index + 1).trim();
267     else
268       return str;
269   }
270
271   private void getHeaders() throws IOException
272   {
273     if (gotHeaders)
274       return;
275     gotHeaders = true;
276
277     connect();
278
279     // Yes, it is overkill to use the hash table and vector here since
280     // we're only putting one header in the file, but in case we need
281     // to add others later and for consistency, we'll implement it this way.
282
283     // Add the only header we know about right now:  Content-length.
284     long len;
285
286     if (element == null)
287       len = jarFileURLConnection.getContentLength ();
288     else
289       len = getJarEntry (element).getSize ();
290
291     String line = "Content-length: " + len;
292     hdrVec.addElement(line);
293
294     // The key will never be null in this scenario since we build up the
295     // headers ourselves.  If we ever rely on getting a header from somewhere
296     // else, then we may have to check if the result of getKey() is null.
297     String key = getKey(line);
298     hdrHash.put(key.toLowerCase(), Long.toString(len));
299   }
300
301 }