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
9 package gnu
.gcj
.protocol
.jar
;
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
;
20 import java
.io
.FileOutputStream
;
21 import java
.util
.jar
.JarFile
;
22 import java
.util
.zip
.ZipFile
;
23 import java
.util
.Hashtable
;
26 * Written using on-line Java Platform 1.2 API Specification.
28 * @author Kresten Krab Thorup <krab@gnu.org>
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
45 public synchronized JarFile
getJarFile() throws java
.io
.IOException
51 throw new ProtocolException("Can't open JarFile if doInput is false");
56 URL jarFileURL
= getJarFileURL ();
58 if (jarFileURL
.getProtocol ().equals ("file")
59 && jarFileURL
.getHost ().equals (""))
63 jarfile
= (JarFile
) file_cache
.get(jarFileURL
);
66 jarfile
= new JarFile (jarFileURL
.getFile ());
67 file_cache
.put (jarFileURL
, jarfile
);
71 jarfile
= new JarFile (jarFileURL
.getFile ());
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
);
81 while((len
= is
.read(buf
)) != -1)
82 fos
.write(buf
, 0, len
);
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
);