package sun.net.www.protocol.jar;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.BufferedInputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.net.UnknownServiceException;
import java.util.Enumeration;
import java.util.Map;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.security.Permission;
public class JarURLConnection extends java.net.JarURLConnection {
private static final boolean debug = false;
private static JarFileFactory factory = new JarFileFactory();
private URL jarFileURL;
private Permission permission;
private URLConnection jarFileURLConnection;
private String entryName;
private JarEntry jarEntry;
private JarFile jarFile;
private String contentType;
public JarURLConnection(URL url, Handler handler)
throws MalformedURLException, IOException {
super(url);
jarFileURL = getJarFileURL();
jarFileURLConnection = jarFileURL.openConnection();
entryName = getEntryName();
}
public JarFile getJarFile() throws IOException {
connect();
return jarFile;
}
public JarEntry getJarEntry() throws IOException {
connect();
return jarEntry;
}
public Permission getPermission() throws IOException {
return jarFileURLConnection.getPermission();
}
class JarURLInputStream extends java.io.FilterInputStream {
JarURLInputStream (InputStream src) {
super (src);
}
public void close () throws IOException {
try {
super.close();
} finally {
if (!getUseCaches()) {
jarFile.close();
}
}
}
}
public void connect() throws IOException {
if (!connected) {
jarFile = factory.get(getJarFileURL(), getUseCaches());
if (getUseCaches()) {
jarFileURLConnection = factory.getConnection(jarFile);
}
if ((entryName != null)) {
jarEntry = (JarEntry)jarFile.getEntry(entryName);
if (jarEntry == null) {
try {
if (!getUseCaches()) {
jarFile.close();
}
} catch (Exception e) {
}
throw new FileNotFoundException("JAR entry " + entryName +
" not found in " +
jarFile.getName());
}
}
connected = true;
}
}
public InputStream getInputStream() throws IOException {
connect();
InputStream result = null;
if (entryName == null) {
throw new IOException("no entry name specified");
} else {
if (jarEntry == null) {
throw new FileNotFoundException("JAR entry " + entryName +
" not found in " +
jarFile.getName());
}
result = new JarURLInputStream (jarFile.getInputStream(jarEntry));
}
return result;
}
public int getContentLength() {
int result = -1;
try {
connect();
if (jarEntry == null) {
result = jarFileURLConnection.getContentLength();
} else {
result = (int)getJarEntry().getSize();
}
} catch (IOException e) {
}
return result;
}
public Object getContent() throws IOException {
Object result = null;
connect();
if (entryName == null) {
result = jarFile;
} else {
result = super.getContent();
}
return result;
}
public String getContentType() {
if (contentType == null) {
if (entryName == null) {
contentType = "x-java/jar";
} else {
try {
connect();
InputStream in = jarFile.getInputStream(jarEntry);
contentType = guessContentTypeFromStream(
new BufferedInputStream(in));
in.close();
} catch (IOException e) {
}
}
if (contentType == null) {
contentType = guessContentTypeFromName(entryName);
}
if (contentType == null) {
contentType = "content/unknown";
}
}
return contentType;
}
public String getHeaderField(String name) {
return jarFileURLConnection.getHeaderField(name);
}
public void setRequestProperty(String key, String value) {
jarFileURLConnection.setRequestProperty(key, value);
}
public String getRequestProperty(String key) {
return jarFileURLConnection.getRequestProperty(key);
}
public void addRequestProperty(String key, String value) {
jarFileURLConnection.addRequestProperty(key, value);
}
public Map<String,List<String>> getRequestProperties() {
return jarFileURLConnection.getRequestProperties();
}
public void setAllowUserInteraction(boolean allowuserinteraction) {
jarFileURLConnection.setAllowUserInteraction(allowuserinteraction);
}
public boolean getAllowUserInteraction() {
return jarFileURLConnection.getAllowUserInteraction();
}
public void setUseCaches(boolean usecaches) {
jarFileURLConnection.setUseCaches(usecaches);
}
public boolean getUseCaches() {
return jarFileURLConnection.getUseCaches();
}
public void setIfModifiedSince(long ifmodifiedsince) {
jarFileURLConnection.setIfModifiedSince(ifmodifiedsince);
}
public void setDefaultUseCaches(boolean defaultusecaches) {
jarFileURLConnection.setDefaultUseCaches(defaultusecaches);
}
public boolean getDefaultUseCaches() {
return jarFileURLConnection.getDefaultUseCaches();
}
}