| 1 | /* Copyright (C) 1999, 2000 Free Software Foundation
|
|---|
| 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 <[email protected]>
|
|---|
| 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 () throws java.io.IOException
|
|---|
| 152 | {
|
|---|
| 153 | JarFile jarfile = null;
|
|---|
| 154 |
|
|---|
| 155 | if (element == null)
|
|---|
| 156 | return null;
|
|---|
| 157 |
|
|---|
| 158 | if (! doInput)
|
|---|
| 159 | throw new ProtocolException("Can't open JarEntry if doInput is false");
|
|---|
| 160 |
|
|---|
| 161 | try
|
|---|
| 162 | {
|
|---|
| 163 | jarfile = getJarFile ();
|
|---|
| 164 | }
|
|---|
| 165 | catch (java.io.IOException x)
|
|---|
| 166 | {
|
|---|
| 167 | /* ignore */
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | if (jarfile == null)
|
|---|
| 171 | {
|
|---|
| 172 | JarInputStream zis = new JarInputStream(jarFileURLConnection.getInputStream ());
|
|---|
| 173 |
|
|---|
| 174 | // This is hideous, we're doing a linear search for the thing...
|
|---|
| 175 | for (ZipEntry ent = zis.getNextEntry ();
|
|---|
| 176 | ent != null;
|
|---|
| 177 | ent = zis.getNextEntry ())
|
|---|
| 178 | {
|
|---|
| 179 | if (element.equals (ent.getName ()))
|
|---|
| 180 | {
|
|---|
| 181 | return new JarEntry (ent);
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | else
|
|---|
| 187 | {
|
|---|
| 188 | return jarfile.getJarEntry (element);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | return null;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | public abstract JarFile getJarFile() throws java.io.IOException;
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 | // Steal and borrow from protocol/file/Connection.java
|
|---|
| 198 |
|
|---|
| 199 | private Hashtable hdrHash = new Hashtable();
|
|---|
| 200 | private Vector hdrVec = new Vector();
|
|---|
| 201 | private boolean gotHeaders = false;
|
|---|
| 202 |
|
|---|
| 203 | // Override default method in URLConnection.
|
|---|
| 204 | public String getHeaderField(String name)
|
|---|
| 205 | {
|
|---|
| 206 | try
|
|---|
| 207 | {
|
|---|
| 208 | getHeaders();
|
|---|
| 209 | }
|
|---|
| 210 | catch (IOException x)
|
|---|
| 211 | {
|
|---|
| 212 | return null;
|
|---|
| 213 | }
|
|---|
| 214 | return (String) hdrHash.get(name.toLowerCase());
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | // Override default method in URLConnection.
|
|---|
| 218 | public String getHeaderField(int n)
|
|---|
| 219 | {
|
|---|
| 220 | try
|
|---|
| 221 | {
|
|---|
| 222 | getHeaders();
|
|---|
| 223 | }
|
|---|
| 224 | catch (IOException x)
|
|---|
| 225 | {
|
|---|
| 226 | return null;
|
|---|
| 227 | }
|
|---|
| 228 | if (n < hdrVec.size())
|
|---|
| 229 | return getField((String) hdrVec.elementAt(n));
|
|---|
| 230 |
|
|---|
| 231 | return null;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | // Override default method in URLConnection.
|
|---|
| 235 | public String getHeaderFieldKey(int n)
|
|---|
| 236 | {
|
|---|
| 237 | try
|
|---|
| 238 | {
|
|---|
| 239 | getHeaders();
|
|---|
| 240 | }
|
|---|
| 241 | catch (IOException x)
|
|---|
| 242 | {
|
|---|
| 243 | return null;
|
|---|
| 244 | }
|
|---|
| 245 | if (n < hdrVec.size())
|
|---|
| 246 | return getKey((String) hdrVec.elementAt(n));
|
|---|
| 247 |
|
|---|
| 248 | return null;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | private String getKey(String str)
|
|---|
| 252 | {
|
|---|
| 253 | if (str == null)
|
|---|
| 254 | return null;
|
|---|
| 255 | int index = str.indexOf(':');
|
|---|
| 256 | if (index >= 0)
|
|---|
| 257 | return str.substring(0, index);
|
|---|
| 258 | else
|
|---|
| 259 | return null;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | private String getField(String str)
|
|---|
| 263 | {
|
|---|
| 264 | if (str == null)
|
|---|
| 265 | return null;
|
|---|
| 266 | int index = str.indexOf(':');
|
|---|
| 267 | if (index >= 0)
|
|---|
| 268 | return str.substring(index + 1).trim();
|
|---|
| 269 | else
|
|---|
| 270 | return str;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | private void getHeaders() throws IOException
|
|---|
| 274 | {
|
|---|
| 275 | if (gotHeaders)
|
|---|
| 276 | return;
|
|---|
| 277 | gotHeaders = true;
|
|---|
| 278 |
|
|---|
| 279 | connect();
|
|---|
| 280 |
|
|---|
| 281 | // Yes, it is overkill to use the hash table and vector here since
|
|---|
| 282 | // we're only putting one header in the file, but in case we need
|
|---|
| 283 | // to add others later and for consistency, we'll implement it this way.
|
|---|
| 284 |
|
|---|
| 285 | // Add the only header we know about right now: Content-length.
|
|---|
| 286 | long len;
|
|---|
| 287 |
|
|---|
| 288 | if (element == null)
|
|---|
| 289 | len = jarFileURLConnection.getContentLength ();
|
|---|
| 290 | else
|
|---|
| 291 | len = getJarEntry ().getSize ();
|
|---|
| 292 |
|
|---|
| 293 | String line = "Content-length: " + len;
|
|---|
| 294 | hdrVec.addElement(line);
|
|---|
| 295 |
|
|---|
| 296 | // The key will never be null in this scenario since we build up the
|
|---|
| 297 | // headers ourselves. If we ever rely on getting a header from somewhere
|
|---|
| 298 | // else, then we may have to check if the result of getKey() is null.
|
|---|
| 299 | String key = getKey(line);
|
|---|
| 300 | hdrHash.put(key.toLowerCase(), Long.toString(len));
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | }
|
|---|