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:
59:
65: public final class Connection extends JarURLConnection
66: {
67:
71: private static SimpleDateFormat dateFormat;
72:
73: private JarFile jar_file;
74: private JarEntry jar_entry;
75: private URL jar_url;
76:
77: public static class JarFileCache
78: {
79: private static Hashtable cache = new Hashtable();
80: private static final int READBUFSIZE = 4*1024;
81:
82: public static synchronized JarFile get (URL url, boolean useCaches)
83: throws IOException
84: {
85: JarFile jf;
86: if (useCaches)
87: {
88: jf = (JarFile) cache.get (url);
89: if (jf != null)
90: return jf;
91: }
92:
93: if ("file".equals (url.getProtocol()))
94: {
95: String fn = url.getFile();
96: fn = gnu.java.net.protocol.file.Connection.unquote(fn);
97: File f = new File (fn);
98: jf = new JarFile (f, true, ZipFile.OPEN_READ);
99: }
100: else
101: {
102: URLConnection urlconn = url.openConnection();
103: InputStream is = urlconn.getInputStream();
104: byte[] buf = new byte [READBUFSIZE];
105: File f = File.createTempFile ("cache", "jar");
106: FileOutputStream fos = new FileOutputStream (f);
107: int len = 0;
108:
109: while ((len = is.read (buf)) != -1)
110: {
111: fos.write (buf, 0, len);
112: }
113:
114: fos.close();
115:
116: jf = new JarFile (f, true,
117: ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
118: }
119:
120: if (useCaches)
121: cache.put (url, jf);
122:
123: return jf;
124: }
125: }
126:
127: protected Connection(URL url)
128: throws MalformedURLException
129: {
130: super(url);
131: }
132:
133: public synchronized void connect() throws IOException
134: {
135:
136: if (connected)
137: return;
138:
139: jar_url = getJarFileURL();
140: jar_file = JarFileCache.get (jar_url, useCaches);
141: String entry_name = getEntryName();
142:
143: if (entry_name != null
144: && !entry_name.equals (""))
145: {
146: jar_entry = (JarEntry) jar_file.getEntry (entry_name);
147:
148: if(jar_entry == null)
149: throw new FileNotFoundException("No entry for " + entry_name + " exists.");
150: }
151:
152: connected = true;
153: }
154:
155: public InputStream getInputStream() throws IOException
156: {
157: if (!connected)
158: connect();
159:
160: if (! doInput)
161: throw new ProtocolException("Can't open InputStream if doInput is false");
162:
163: return jar_file.getInputStream (jar_entry);
164: }
165:
166: public synchronized JarFile getJarFile() throws IOException
167: {
168: if (!connected)
169: connect();
170:
171: if (! doInput)
172: throw new ProtocolException("Can't open JarFile if doInput is false");
173:
174: return jar_file;
175: }
176:
177: public String getHeaderField(String field)
178: {
179: try
180: {
181: if (!connected)
182: connect();
183:
184: if (field.equals("content-type"))
185: return guessContentTypeFromName(getJarEntry().getName());
186: else if (field.equals("content-length"))
187: return Long.toString(getJarEntry().getSize());
188: else if (field.equals("last-modified"))
189: {
190:
191: synchronized (Connection.class)
192: {
193: if (dateFormat == null)
194: dateFormat = new SimpleDateFormat
195: ("EEE, dd MMM yyyy hh:mm:ss 'GMT'",
196: new Locale ("En", "Us", "Unix"));
197:
198: return dateFormat.format(new Date(getJarEntry().getTime()));
199: }
200: }
201: }
202: catch (IOException e)
203: {
204:
205: }
206: return null;
207: }
208:
209: public int getContentLength()
210: {
211: if (!connected)
212: return -1;
213:
214: return (int) jar_entry.getSize();
215: }
216:
217: public long getLastModified()
218: {
219: if (!connected)
220: return -1;
221:
222: try
223: {
224: return getJarEntry().getTime();
225: }
226: catch (IOException e)
227: {
228: return -1;
229: }
230: }
231: }