001: /*
002: * @(#)JarResourceConnection.java 1.2 04/12/06
003: *
004: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.lib.jar;
010:
011: import java.net.URL;
012: import java.net.URLConnection;
013: import java.net.MalformedURLException;
014: import java.io.InputStream;
015: import java.io.IOException;
016: import java.io.FileNotFoundException;
017: import java.io.BufferedInputStream;
018: import java.util.*;
019: import java.util.zip.*;
020: import java.lang.reflect.Method;
021:
022: /**
023: * URLConnection for resource in a JAR file
024: * The URL scheme is same as jdk1.2's.
025: */
026: class JarResourceConnection extends URLConnection {
027: private String name; // name of the resource
028: private URL jarFileURL;
029: private URLConnection jarFileURLConnection;
030: private InputStream in;
031: private ZipFile jarFile;
032: private ZipEntry jarEntry;
033: private boolean connected = false;
034: private String contentType = null;
035:
036: protected JarResourceConnection(URL url)
037: throws MalformedURLException, IOException {
038: super (url);
039: String file = url.getFile();
040: if (file.charAt(0) == '/') {
041: file = file.substring(1);
042: }
043: int idx = file.indexOf("!/");
044: if (idx < 0) {
045: throw new MalformedURLException();
046: }
047: String sub = file.substring(0, idx);
048: name = file.substring(idx + 2);
049:
050: jarFileURL = new URL(sub);
051: try {
052: jarFile = new ZipFile(jarFileURL.getFile());
053: } catch (IOException e) {
054: // do nothing
055: }
056: }
057:
058: public void connect() throws IOException {
059: if (!connected) {
060: jarFileURLConnection = jarFileURL.openConnection();
061: if (jarFile != null) {
062: jarEntry = jarFile.getEntry(name);
063: in = jarFile.getInputStream(jarEntry);
064: } else {
065: ZipEntry e = null;
066: ZipInputStream zin = new ZipInputStream(
067: jarFileURLConnection.getInputStream());
068: while ((e = zin.getNextEntry()) != null) {
069: if (e.getName().equals(name)) {
070: jarEntry = e;
071: in = zin;
072: break;
073: }
074: }
075: }
076: connected = true;
077: }
078: }
079:
080: public String getContentType() {
081: if (contentType == null) {
082: if (name == null) {
083: contentType = "x-java/jar";
084: } else {
085: try {
086: connect();
087: InputStream in = jarFile.getInputStream(jarEntry);
088: contentType = guessContentTypeFromStream(new BufferedInputStream(
089: in));
090: in.close();
091: } catch (IOException e) {
092: /* ignore */
093: }
094: }
095: if (contentType == null) {
096: contentType = guessContentTypeFromName(name);
097: }
098: }
099: return contentType;
100: }
101:
102: public InputStream getInputStream() throws IOException {
103: connect();
104: if (jarEntry == null) {
105: throw new FileNotFoundException();
106: }
107: return in;
108: }
109:
110: public int getContentLength() {
111: int result = -1;
112: try {
113: connect();
114: if (jarEntry == null) {
115: result = jarFileURLConnection.getContentLength();
116: } else {
117: result = (int) jarEntry.getSize();
118: }
119: } catch (IOException e) {
120: }
121: return result;
122: }
123: }
|