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