1 /* Copyright (C) 1999 Red Hat, Inc.
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
13 import java.util.jar.*;
14 import java.util.zip.*;
15 import java.util.Vector;
16 import java.util.Hashtable;
19 * @author Kresten Krab Thorup <krab@gnu.org>
24 public abstract class JarURLConnection extends URLConnection
26 // three different ways to say the same thing
27 private final URL jarFileURL;
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;
34 // If this is a connection to a jar file element this is set, otherwose null.
35 private final String element;
37 // Cached JarURLConnection's
38 static Hashtable conn_cache = new Hashtable();
40 public URL getJarFileURL ()
45 public String getEntryName ()
50 public JarURLConnection(URL url)
51 throws MalformedURLException
55 String spec = url.getFile();
56 int bang = spec.indexOf ("!/", 0);
58 throw new MalformedURLException (url + ": No `!/' in spec.");
60 // Extact the url for the jar itself.
61 jarFileURL = new URL(spec.substring (0, bang));
63 // Get the name of the element, if any.
64 element = (bang+2==spec.length() ? null : spec.substring (bang+2));
67 public synchronized void connect() throws IOException
69 // Call is ignored if already connected.
75 jarFileURLConnection = (URLConnection) conn_cache.get (jarFileURL);
77 if (jarFileURLConnection == null)
79 jarFileURLConnection = jarFileURL.openConnection ();
80 jarFileURLConnection.setUseCaches (true);
81 jarFileURLConnection.connect ();
82 conn_cache.put (jarFileURL, jarFileURLConnection);
87 jarFileURLConnection = jarFileURL.openConnection ();
88 jarFileURLConnection.connect ();
94 public InputStream getInputStream() throws IOException
100 throw new ProtocolException("Can't open InputStream if doInput is false");
104 // This is a JarURLConnection for the entire jar file.
106 InputStream jar_is = new BufferedInputStream(jarFileURLConnection.getInputStream ());
107 return new JarInputStream(jar_is);
110 // Reaching this point, we're looking for an element of a jar file.
112 JarFile jarfile = null;
116 jarfile = getJarFile ();
118 catch (java.io.IOException x)
125 // this is the easy way...
126 return jarfile.getInputStream (jarfile.getEntry (element));
130 // If the jar file is not local, ...
131 JarInputStream zis = new JarInputStream(jarFileURLConnection.getInputStream ());
133 // This is hideous, we're doing a linear search...
134 for (ZipEntry ent = zis.getNextEntry ();
136 ent = zis.getNextEntry ())
138 if (element.equals (ent.getName ()))
140 int size = (int)ent.getSize();
141 byte[] data = new byte[size];
142 zis.read (data, 0, size);
143 return new ByteArrayInputStream (data);
151 public JarEntry getJarEntry (String name)
152 throws java.io.IOException
154 JarFile jarfile = null;
157 throw new ProtocolException("Can't open JarEntry if doInput is false");
161 jarfile = getJarFile ();
163 catch (java.io.IOException x)
170 JarInputStream zis = new JarInputStream(jarFileURLConnection.getInputStream ());
172 // This is hideous, we're doing a linear search for the thing...
173 for (ZipEntry ent = zis.getNextEntry ();
175 ent = zis.getNextEntry ())
177 if (element.equals (ent.getName ()))
179 return new JarEntry (ent);
186 return jarfile.getJarEntry (element);
192 public abstract JarFile getJarFile() throws java.io.IOException;
195 // Steal and borrow from protocol/file/Connection.java
197 private Hashtable hdrHash = new Hashtable();
198 private Vector hdrVec = new Vector();
199 private boolean gotHeaders = false;
201 // Override default method in URLConnection.
202 public String getHeaderField(String name)
208 catch (IOException x)
212 return (String) hdrHash.get(name.toLowerCase());
215 // Override default method in URLConnection.
216 public String getHeaderField(int n)
222 catch (IOException x)
226 if (n < hdrVec.size())
227 return getField((String) hdrVec.elementAt(n));
232 // Override default method in URLConnection.
233 public String getHeaderFieldKey(int n)
239 catch (IOException x)
243 if (n < hdrVec.size())
244 return getKey((String) hdrVec.elementAt(n));
249 private String getKey(String str)
253 int index = str.indexOf(':');
255 return str.substring(0, index);
260 private String getField(String str)
264 int index = str.indexOf(':');
266 return str.substring(index + 1).trim();
271 private void getHeaders() throws IOException
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.
283 // Add the only header we know about right now: Content-length.
287 len = jarFileURLConnection.getContentLength ();
289 len = getJarEntry (element).getSize ();
291 String line = "Content-length: " + len;
292 hdrVec.addElement(line);
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));