* gnu/gcj/protocol/jar/Connection.java (getJarFile): download and
[official-gcc.git] / libjava / gnu / gcj / protocol / jar / Connection.java
blobc9db04997dc2c7d7317583a070d3cf84ee9e5b40
1 /* Copyright (C) 1999, 2002 Free Software Foundation
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
7 details. */
9 package gnu.gcj.protocol.jar;
11 import java.net.URL;
12 import java.net.URLConnection;
13 import java.net.JarURLConnection;
14 import java.net.URLStreamHandler;
15 import java.net.MalformedURLException;
16 import java.net.ProtocolException;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.util.jar.JarFile;
22 import java.util.zip.ZipFile;
23 import java.util.Hashtable;
25 /**
26 * Written using on-line Java Platform 1.2 API Specification.
28 * @author Kresten Krab Thorup <krab@gnu.org>
29 * @date Aug 10, 1999.
34 public class Connection extends JarURLConnection
36 static Hashtable file_cache = new Hashtable();
37 private JarFile jarfile;
39 public Connection(URL url)
40 throws MalformedURLException
42 super(url);
45 public synchronized JarFile getJarFile() throws java.io.IOException
47 if (!connected)
48 connect();
50 if (! doInput)
51 throw new ProtocolException("Can't open JarFile if doInput is false");
53 if (jarfile != null)
54 return jarfile;
56 URL jarFileURL = getJarFileURL ();
58 if (jarFileURL.getProtocol ().equals ("file")
59 && jarFileURL.getHost ().equals (""))
61 if (getUseCaches())
63 jarfile = (JarFile) file_cache.get(jarFileURL);
64 if (jarfile == null)
66 jarfile = new JarFile (jarFileURL.getFile ());
67 file_cache.put (jarFileURL, jarfile);
70 else
71 jarfile = new JarFile (jarFileURL.getFile ());
73 else
75 URLConnection urlconn = jarFileURL.openConnection();
76 InputStream is = urlconn.getInputStream();
77 byte[] buf = new byte[4*1024];
78 File f = File.createTempFile("cache", "jar");
79 FileOutputStream fos = new FileOutputStream(f);
80 int len = 0;
81 while((len = is.read(buf)) != -1)
82 fos.write(buf, 0, len);
83 fos.close();
84 // Always verify the Manifest, open read only and delete when done.
85 // XXX ZipFile.OPEN_DELETE not yet implemented.
86 // jf = new JarFile(f, true, ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
87 jarfile = new JarFile(f, true, ZipFile.OPEN_READ);
90 return jarfile;