1 |
/* |
/* |
2 |
FileURLConnection.java -- URLConnection class for "file" protocol |
FileURLConnection.java -- URLConnection class for "file" protocol |
3 |
Copyright (C) 1998, 2003 Free Software Foundation, Inc. |
Copyright (C) 1998, 1999, 2003 Free Software Foundation, Inc. |
4 |
|
|
5 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
6 |
|
|
38 |
*/ |
*/ |
39 |
package gnu.java.net.protocol.file; |
package gnu.java.net.protocol.file; |
40 |
|
|
|
import java.net.URL; |
|
|
import java.net.URLConnection; |
|
41 |
import java.io.File; |
import java.io.File; |
42 |
import java.io.FileInputStream; |
import java.io.FileInputStream; |
43 |
import java.io.FileNotFoundException; |
import java.io.FileNotFoundException; |
45 |
import java.io.InputStream; |
import java.io.InputStream; |
46 |
import java.io.IOException; |
import java.io.IOException; |
47 |
import java.io.OutputStream; |
import java.io.OutputStream; |
48 |
|
import java.net.URL; |
49 |
|
import java.net.URLConnection; |
50 |
import java.util.AbstractSet; |
import java.util.AbstractSet; |
51 |
import java.util.Iterator; |
import java.util.Iterator; |
52 |
import java.util.Set; |
import java.util.Set; |
56 |
* This subclass of java.net.URLConnection models a URLConnection via |
* This subclass of java.net.URLConnection models a URLConnection via |
57 |
* the "file" protocol. |
* the "file" protocol. |
58 |
* |
* |
59 |
* @author Aaron M. Renn (arenn@urbanophile.com) |
* @author Aaron M. Renn <arenn@urbanophile.com> |
60 |
* @author Nic Ferrier (nferrier@tapsellferrier.co.uk) |
* @author Nic Ferrier <nferrier@tapsellferrier.co.uk> |
61 |
|
* @author Warren Levy <warrenl@cygnus.com> |
62 |
*/ |
*/ |
63 |
public class Connection extends URLConnection |
public class Connection extends URLConnection |
64 |
{ |
{ |
70 |
/** |
/** |
71 |
* InputStream if we are reading from the file |
* InputStream if we are reading from the file |
72 |
*/ |
*/ |
73 |
private FileInputStream in_stream; |
private FileInputStream inputStream; |
74 |
|
|
75 |
/** |
/** |
76 |
* OutputStream if we are writing to the file |
* OutputStream if we are writing to the file |
77 |
*/ |
*/ |
78 |
private FileOutputStream out_stream; |
private FileOutputStream outputStream; |
79 |
|
|
80 |
/** |
/** |
81 |
* Calls superclass constructor to initialize. |
* Calls superclass constructor to initialize. |
93 |
*/ |
*/ |
94 |
public void connect() throws IOException |
public void connect() throws IOException |
95 |
{ |
{ |
96 |
|
// Call is ignored if already connected. |
97 |
if (connected) |
if (connected) |
98 |
return; |
return; |
99 |
|
|
100 |
|
// If not connected, then file needs to be openned. |
101 |
file = new File (getURL().getFile()); |
file = new File (getURL().getFile()); |
102 |
|
|
103 |
if (!file.exists()) |
if (!file.exists()) |
104 |
throw new FileNotFoundException (file.getPath()); |
throw new FileNotFoundException (file.getPath()); |
105 |
|
|
106 |
|
if (doInput) |
107 |
|
inputStream = new FileInputStream (file); |
108 |
|
|
109 |
|
if (doOutput) |
110 |
|
outputStream = new FileOutputStream (file); |
111 |
|
|
112 |
connected = true; |
connected = true; |
113 |
} |
} |
125 |
if (!connected) |
if (!connected) |
126 |
connect(); |
connect(); |
127 |
|
|
128 |
in_stream = new FileInputStream (file); |
return inputStream; |
|
return in_stream; |
|
129 |
} |
} |
130 |
|
|
131 |
/** |
/** |
141 |
if (!connected) |
if (!connected) |
142 |
connect(); |
connect(); |
143 |
|
|
144 |
out_stream = new FileOutputStream (file); |
return outputStream; |
|
return out_stream; |
|
145 |
} |
} |
146 |
|
|
147 |
/** Get the last modified time of the resource. |
/** |
148 |
|
* Get the last modified time of the resource. |
149 |
* |
* |
150 |
* @return the time since epoch that the resource was modified. |
* @return the time since epoch that the resource was modified. |
151 |
*/ |
*/ |
164 |
} |
} |
165 |
} |
} |
166 |
|
|
167 |
/** Get the length of content. |
/** |
168 |
|
* Get the length of content. |
169 |
* |
* |
170 |
* @return the length of the content. |
* @return the length of the content. |
171 |
*/ |
*/ |
184 |
} |
} |
185 |
} |
} |
186 |
|
|
|
// These are GNU only implementation methods. |
|
187 |
|
|
188 |
/** Does the resource pointed to actually exist? |
/** |
189 |
|
* Does the resource pointed to actually exist? |
190 |
*/ |
*/ |
191 |
public final boolean exists() |
public final boolean exists() |
192 |
{ |
{ |
204 |
return file.isDirectory(); |
return file.isDirectory(); |
205 |
} |
} |
206 |
|
|
207 |
/** Get a listing of the directory, if it is a directory. |
/** |
208 |
|
* Get a listing of the directory, if it is a directory. |
209 |
* |
* |
210 |
* @return a set which can supply an iteration of the |
* @return a set which can supply an iteration of the |
211 |
* contents of the directory. |
* contents of the directory. |