1:
37:
38: package ;
39:
40: import ;
41:
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:
72: public class Connection extends URLConnection
73: {
74:
77: private static final String DEFAULT_PERMISSION = "read";
78:
79: private static class StaticData
80: {
81:
84: static SimpleDateFormat dateFormat
85: = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss 'GMT'",
86: new Locale ("En", "Us", "Unix"));
87:
88: static String lineSeparator =
89: SystemProperties.getProperty("line.separator");
90: }
91:
92:
93:
96: private File file;
97:
98:
101: private byte[] directoryListing;
102:
103:
106: private InputStream inputStream;
107:
108:
111: private OutputStream outputStream;
112:
113:
116: private FilePermission permission;
117:
118:
121: public Connection(URL url)
122: {
123: super (url);
124:
125: permission = new FilePermission(getURL().getFile(), DEFAULT_PERMISSION);
126: }
127:
128:
142: public static String unquote(String str) throws MalformedURLException
143: {
144: if (str == null)
145: return null;
146: byte[] buf = new byte[str.length()];
147: int pos = 0;
148: for (int i = 0; i < str.length(); i++)
149: {
150: char c = str.charAt(i);
151: if (c > 127)
152: throw new MalformedURLException(str + " : Invalid character");
153: if (c == '%')
154: {
155: if (i + 2 >= str.length())
156: throw new MalformedURLException(str + " : Invalid quoted character");
157: int hi = Character.digit(str.charAt(++i), 16);
158: int lo = Character.digit(str.charAt(++i), 16);
159: if (lo < 0 || hi < 0)
160: throw new MalformedURLException(str + " : Invalid quoted character");
161: buf[pos++] = (byte) (hi * 16 + lo);
162: }
163: else
164: buf[pos++] = (byte) c;
165: }
166: try
167: {
168: return new String(buf, 0, pos, "utf-8");
169: }
170: catch (java.io.UnsupportedEncodingException x2)
171: {
172: throw (Error) new InternalError().initCause(x2);
173: }
174: }
175:
176:
179: public void connect() throws IOException
180: {
181:
182: if (connected)
183: return;
184:
185:
186: file = new File (unquote(getURL().getFile()));
187:
188: if (! file.isDirectory())
189: {
190: if (doInput)
191: inputStream = new BufferedInputStream(new FileInputStream(file));
192:
193: if (doOutput)
194: outputStream = new BufferedOutputStream(new FileOutputStream(file));
195: }
196: else
197: {
198: if (doInput)
199: {
200: inputStream = new ByteArrayInputStream(getDirectoryListing());
201: }
202:
203: if (doOutput)
204: throw new ProtocolException
205: ("file: protocol does not support output on directories");
206: }
207:
208: connected = true;
209: }
210:
211:
215: byte[] getDirectoryListing()
216: throws IOException
217: {
218: if (directoryListing == null)
219: {
220: ByteArrayOutputStream sink = new ByteArrayOutputStream();
221:
222: Writer writer = new OutputStreamWriter(sink);
223:
224: String[] files = file.list();
225:
226: for (int i = 0; i < files.length; i++)
227: {
228: writer.write(files[i]);
229: writer.write(StaticData.lineSeparator);
230: }
231:
232: directoryListing = sink.toByteArray();
233: }
234: return directoryListing;
235: }
236:
237:
244: public InputStream getInputStream()
245: throws IOException
246: {
247: if (!doInput)
248: throw new ProtocolException("Can't open InputStream if doInput is false");
249:
250: if (!connected)
251: connect();
252:
253: return inputStream;
254: }
255:
256:
263: public OutputStream getOutputStream()
264: throws IOException
265: {
266: if (!doOutput)
267: throw new
268: ProtocolException("Can't open OutputStream if doOutput is false");
269:
270: if (!connected)
271: connect();
272:
273: return outputStream;
274: }
275:
276:
281: public long getLastModified()
282: {
283: try
284: {
285: if (!connected)
286: connect();
287:
288: return file.lastModified();
289: }
290: catch (IOException e)
291: {
292: return -1;
293: }
294: }
295:
296:
299: public String getHeaderField(String field)
300: {
301: try
302: {
303: if (!connected)
304: connect();
305:
306: if (field.equals("content-type"))
307: return guessContentTypeFromName(file.getName());
308: else if (field.equals("content-length"))
309: {
310: if (file.isDirectory())
311: {
312: return Integer.toString(getContentLength());
313: }
314: return Long.toString(file.length());
315: }
316: else if (field.equals("last-modified"))
317: {
318: synchronized (StaticData.dateFormat)
319: {
320: return StaticData.dateFormat.format(
321: new Date(file.lastModified()));
322: }
323: }
324: }
325: catch (IOException e)
326: {
327:
328: }
329: return null;
330: }
331:
332:
337: public int getContentLength()
338: {
339: try
340: {
341: if (!connected)
342: connect();
343:
344: if (file.isDirectory())
345: {
346: return getDirectoryListing().length;
347: }
348: return (int) file.length();
349: }
350: catch (IOException e)
351: {
352: return -1;
353: }
354: }
355:
356:
364: public Permission getPermission() throws IOException
365: {
366: return permission;
367: }
368: }