1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63:
64:
70: public final class Connection extends JarURLConnection
71: {
72: private static Hashtable file_cache = new Hashtable();
73:
74:
77: private static SimpleDateFormat dateFormat
78: = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss 'GMT'",
79: new Locale ("En", "Us", "Unix"));
80:
81: private JarFile jar_file;
82:
83:
86: static HashMap connectionCache = new HashMap();
87:
88: protected Connection(URL url)
89: throws MalformedURLException
90: {
91: super(url);
92: }
93:
94: public synchronized void connect() throws IOException
95: {
96:
97: if (connected)
98: return;
99:
100: if (getUseCaches())
101: {
102: jarFileURLConnection =
103: (URLConnection) connectionCache.get(getJarFileURL());
104:
105: if (jarFileURLConnection == null)
106: {
107: jarFileURLConnection = getJarFileURL().openConnection();
108: jarFileURLConnection.setUseCaches(true);
109: jarFileURLConnection.connect();
110: connectionCache.put(getJarFileURL(), jarFileURLConnection);
111: }
112: }
113: else
114: {
115: jarFileURLConnection = getJarFileURL().openConnection();
116: jarFileURLConnection.connect();
117: }
118:
119: connected = true;
120: }
121:
122: public InputStream getInputStream() throws IOException
123: {
124: if (!connected)
125: connect();
126:
127: if (! doInput)
128: throw new ProtocolException("Can't open InputStream if doInput is false");
129:
130: if (getEntryName() == null)
131: {
132:
133:
134: InputStream in = new BufferedInputStream
135: (jarFileURLConnection.getInputStream());
136: return new JarInputStream(in);
137: }
138:
139:
140:
141: JarFile jarfile = null;
142:
143: try
144: {
145: jarfile = getJarFile ();
146: }
147: catch (IOException x)
148: {
149:
150: }
151:
152: if (jarfile != null)
153: {
154:
155: ZipEntry entry = jarfile.getEntry
156: (gnu.java.net.protocol.file.Connection.unquote(getEntryName()));
157:
158: if (entry != null)
159: return jarfile.getInputStream (entry);
160: }
161: else
162: {
163:
164: JarInputStream zis = new JarInputStream(
165: jarFileURLConnection.getInputStream ());
166:
167: String entryName = gnu.java.net.protocol.file.Connection.unquote(getEntryName());
168:
169:
170: for (ZipEntry entry = zis.getNextEntry();
171: entry != null;
172: entry = zis.getNextEntry())
173: {
174: if (entryName.equals(entry.getName()))
175: {
176: int size = (int) entry.getSize();
177: byte[] data = new byte[size];
178: zis.read (data, 0, size);
179: return new ByteArrayInputStream (data);
180: }
181: }
182: }
183:
184: throw new FileNotFoundException("No entry for \"" + getEntryName()
185: + "\" in \""
186: + getJarFileURL()
187: + "\"");
188: }
189:
190: public synchronized JarFile getJarFile() throws IOException
191: {
192: if (!connected)
193: connect();
194:
195: if (! doInput)
196: throw new ProtocolException("Can't open JarFile if doInput is false");
197:
198: if (jar_file != null)
199: return jar_file;
200:
201: URL jarFileURL = getJarFileURL();
202:
203: if (jarFileURL.getProtocol().equals ("file")
204: && jarFileURL.getHost().equals (""))
205: {
206: if (getUseCaches())
207: {
208: jar_file = (JarFile) file_cache.get (jarFileURL);
209: if (jar_file == null)
210: {
211: jar_file = new JarFile
212: (gnu.java.net.protocol.file.Connection.unquote(jarFileURL.getFile()));
213: file_cache.put (jarFileURL, jar_file);
214: }
215: }
216: else
217: jar_file = new JarFile
218: (gnu.java.net.protocol.file.Connection.unquote(jarFileURL.getFile()));
219: }
220: else
221: {
222: URLConnection urlconn = jarFileURL.openConnection();
223: InputStream is = urlconn.getInputStream();
224: byte[] buf = new byte[4*1024];
225: File f = File.createTempFile("cache", "jar");
226: FileOutputStream fos = new FileOutputStream(f);
227: int len = 0;
228: while ((len = is.read(buf)) != -1)
229: fos.write(buf, 0, len);
230: fos.close();
231:
232: jar_file = new JarFile (f, true,
233: ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
234: }
235:
236: return jar_file;
237: }
238:
239: public String getHeaderField(String field)
240: {
241: try
242: {
243: if (!connected)
244: connect();
245:
246: if (field.equals("content-type"))
247: return guessContentTypeFromName(getJarEntry().getName());
248: else if (field.equals("content-length"))
249: return Long.toString(getJarEntry().getSize());
250: else if (field.equals("last-modified"))
251: {
252: synchronized (dateFormat)
253: {
254: return dateFormat.format(new Date(getJarEntry().getTime()));
255: }
256: }
257: }
258: catch (IOException e)
259: {
260:
261: }
262: return null;
263: }
264:
265: public int getContentLength()
266: {
267: if (!connected)
268: return -1;
269:
270: try
271: {
272: return (int) getJarEntry().getSize();
273: }
274: catch (IOException e)
275: {
276: return -1;
277: }
278: }
279:
280: public long getLastModified()
281: {
282: if (!connected)
283: return -1;
284:
285: try
286: {
287: return getJarEntry().getTime();
288: }
289: catch (IOException e)
290: {
291: return -1;
292: }
293: }
294: }