package gnu.gcj.protocol.jar;
import java.net.URL;
import java.net.URLConnection;
import java.net.JarURLConnection;
import java.net.URLStreamHandler;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.jar.JarFile;
import java.util.zip.ZipFile;
import java.util.Hashtable;
public class Connection extends JarURLConnection
{
static Hashtable file_cache = new Hashtable();
private JarFile jarfile;
public Connection(URL url)
throws MalformedURLException
{
super(url);
}
public synchronized JarFile getJarFile() throws java.io.IOException
{
if (!connected)
connect();
if (! doInput)
throw new ProtocolException("Can't open JarFile if doInput is false");
if (jarfile != null)
return jarfile;
URL jarFileURL = getJarFileURL ();
if (jarFileURL.getProtocol ().equals ("file")
&& jarFileURL.getHost ().equals (""))
{
if (getUseCaches())
{
jarfile = (JarFile) file_cache.get(jarFileURL);
if (jarfile == null)
{
jarfile = new JarFile (jarFileURL.getFile ());
file_cache.put (jarFileURL, jarfile);
}
}
else
jarfile = new JarFile (jarFileURL.getFile ());
}
else
{
URLConnection urlconn = jarFileURL.openConnection();
InputStream is = urlconn.getInputStream();
byte[] buf = new byte[4*1024];
File f = File.createTempFile("cache", "jar");
FileOutputStream fos = new FileOutputStream(f);
int len = 0;
while((len = is.read(buf)) != -1)
fos.write(buf, 0, len);
fos.close();
jarfile = new JarFile(f, true, ZipFile.OPEN_READ);
}
return jarfile;
}
}