1 |
/* |
/* |
2 |
FileURLConnection.java -- URLConnection class for "file" protocol |
FileURLConnection.java -- URLConnection class for "file" protocol |
3 |
Copyright (C) 1998 Free Software Foundation, Inc. |
Copyright (C) 1998, 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 |
|
|
41 |
|
import java.net.URL; |
42 |
|
import java.net.URLConnection; |
43 |
|
import java.io.File; |
44 |
|
import java.io.FileInputStream; |
45 |
|
import java.io.FileNotFoundException; |
46 |
|
import java.io.FileOutputStream; |
47 |
|
import java.io.InputStream; |
48 |
|
import java.io.IOException; |
49 |
|
import java.io.OutputStream; |
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; |
53 |
import java.util.NoSuchElementException; |
import java.util.NoSuchElementException; |
|
import java.io.*; |
|
54 |
|
|
55 |
/** |
/** |
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 |
*/ |
*/ |
62 |
public class FileURLConnection extends java.net.URLConnection |
public class FileURLConnection extends URLConnection |
63 |
{ |
{ |
|
|
|
64 |
/** |
/** |
65 |
* This is a File object for this connection |
* This is a File object for this connection |
66 |
*/ |
*/ |
67 |
private java.io.File file; |
private File file; |
68 |
|
|
69 |
/** |
/** |
70 |
* InputStream if we are reading from the file |
* InputStream if we are reading from the file |
71 |
*/ |
*/ |
72 |
private java.io.FileInputStream in_stream; |
private FileInputStream in_stream; |
73 |
|
|
74 |
/** |
/** |
75 |
* OutputStream if we are writing to the file |
* OutputStream if we are writing to the file |
76 |
*/ |
*/ |
77 |
private java.io.FileOutputStream out_stream; |
private FileOutputStream out_stream; |
78 |
|
|
79 |
/** |
/** |
80 |
* Calls superclass constructor to initialize. |
* Calls superclass constructor to initialize. |
81 |
*/ |
*/ |
82 |
protected FileURLConnection(java.net.URL url) |
protected FileURLConnection (URL url) |
83 |
{ |
{ |
84 |
super(url); |
super (url); |
85 |
|
|
86 |
/* Set up some variables */ |
/* Set up some variables */ |
87 |
doOutput = false; |
doOutput = false; |
88 |
} |
} |
89 |
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Instance Methods |
|
|
*/ |
|
|
|
|
90 |
/** |
/** |
91 |
* "Connects" to the file by opening it. |
* "Connects" to the file by opening it. |
92 |
*/ |
*/ |
93 |
public void connect() throws java.io.IOException |
public void connect() throws IOException |
94 |
{ |
{ |
95 |
if (connected) |
if (connected) |
96 |
return; |
return; |
97 |
file = new java.io.File(getURL().getFile()); |
|
98 |
|
file = new File (getURL().getFile()); |
99 |
|
|
100 |
if (!file.exists()) |
if (!file.exists()) |
101 |
throw new java.io.FileNotFoundException(file.getPath()); |
throw new FileNotFoundException (file.getPath()); |
102 |
|
|
103 |
connected = true; |
connected = true; |
104 |
} |
} |
105 |
|
|
110 |
* |
* |
111 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
112 |
*/ |
*/ |
113 |
public java.io.InputStream getInputStream () |
public InputStream getInputStream() |
114 |
throws java.io.IOException |
throws IOException |
115 |
{ |
{ |
116 |
if (!connected) |
if (!connected) |
117 |
connect(); |
connect(); |
118 |
in_stream = new java.io.FileInputStream(file); |
|
119 |
return (in_stream); |
in_stream = new FileInputStream (file); |
120 |
|
return in_stream; |
121 |
} |
} |
122 |
|
|
123 |
/** |
/** |
127 |
* |
* |
128 |
* @exception IOException If an error occurs. |
* @exception IOException If an error occurs. |
129 |
*/ |
*/ |
130 |
public java.io.OutputStream getOutputStream () |
public OutputStream getOutputStream() |
131 |
throws java.io.IOException |
throws IOException |
132 |
{ |
{ |
133 |
if (!connected) |
if (!connected) |
134 |
connect(); |
connect(); |
135 |
out_stream = new java.io.FileOutputStream(file); |
|
136 |
return (out_stream); |
out_stream = new FileOutputStream (file); |
137 |
|
return out_stream; |
138 |
} |
} |
139 |
|
|
140 |
/** Get the last modified time of the resource. |
/** Get the last modified time of the resource. |
141 |
* |
* |
142 |
* @return the time since epoch that the resource was modified. |
* @return the time since epoch that the resource was modified. |
143 |
*/ |
*/ |
144 |
public long getLastModified () |
public long getLastModified() |
145 |
{ |
{ |
146 |
try |
try |
147 |
{ |
{ |
148 |
if (! connected) |
if (!connected) |
149 |
connect(); |
connect(); |
150 |
|
|
151 |
return file.lastModified(); |
return file.lastModified(); |
152 |
} |
} |
153 |
catch (IOException e) |
catch (IOException e) |
160 |
* |
* |
161 |
* @return the length of the content. |
* @return the length of the content. |
162 |
*/ |
*/ |
163 |
public int getContentLength () |
public int getContentLength() |
164 |
{ |
{ |
165 |
try |
try |
166 |
{ |
{ |
167 |
if (! connected) |
if (!connected) |
168 |
connect(); |
connect(); |
169 |
|
|
170 |
return (int) file.length(); |
return (int) file.length(); |
171 |
} |
} |
172 |
catch (IOException e) |
catch (IOException e) |
175 |
} |
} |
176 |
} |
} |
177 |
|
|
|
|
|
178 |
// These are GNU only implementation methods. |
// These are GNU only implementation methods. |
179 |
|
|
180 |
/** Does the resource pointed to actually exist? |
/** Does the resource pointed to actually exist? |
181 |
*/ |
*/ |
182 |
public final boolean exists () |
public final boolean exists() |
183 |
{ |
{ |
184 |
if (file == null) |
if (file == null) |
185 |
return false; |
return false; |
186 |
|
|
187 |
return file.exists(); |
return file.exists(); |
188 |
} |
} |
189 |
|
|
190 |
/** Is the resource pointed to a directory? |
/** |
191 |
|
* Is the resource pointed to a directory? |
192 |
*/ |
*/ |
193 |
public final boolean isDirectory () |
public final boolean isDirectory() |
194 |
{ |
{ |
195 |
return file.isDirectory(); |
return file.isDirectory(); |
196 |
} |
} |
199 |
* |
* |
200 |
* @return a set which can supply an iteration of the |
* @return a set which can supply an iteration of the |
201 |
* contents of the directory. |
* contents of the directory. |
202 |
|
* |
203 |
* @throws IllegalStateException if this is not pointing |
* @throws IllegalStateException if this is not pointing |
204 |
* to a directory. |
* to a directory. |
205 |
*/ |
*/ |
206 |
public Set getListing () |
public Set getListing() |
207 |
{ |
{ |
208 |
if (! file.isDirectory()) |
if (!file.isDirectory()) |
209 |
throw new IllegalStateException("this is not a directory"); |
throw new IllegalStateException ("this is not a directory"); |
210 |
|
|
211 |
final File[] directoryList = file.listFiles(); |
final File[] directoryList = file.listFiles(); |
212 |
return new AbstractSet() |
return new AbstractSet() |
213 |
{ |
{ |
214 |
File[] dirList = directoryList; |
File[] dirList = directoryList; |
215 |
|
|
216 |
public int size () |
public int size() |
217 |
{ |
{ |
218 |
return dirList.length; |
return dirList.length; |
219 |
} |
} |
220 |
|
|
221 |
public Iterator iterator () |
public Iterator iterator() |
222 |
{ |
{ |
223 |
return new Iterator() |
return new Iterator() |
224 |
{ |
{ |
225 |
int index = 0; |
int index = 0; |
226 |
|
|
227 |
public boolean hasNext () |
public boolean hasNext() |
228 |
{ |
{ |
229 |
return index < dirList.length; |
return index < dirList.length; |
230 |
} |
} |
231 |
|
|
232 |
public Object next () |
public Object next() |
233 |
{ |
{ |
234 |
try |
try |
235 |
{ |
{ |
236 |
String value = dirList[index++].getName(); |
String value = dirList [index++].getName(); |
237 |
return value; |
return value; |
238 |
} |
} |
239 |
catch (ArrayIndexOutOfBoundsException e) |
catch (ArrayIndexOutOfBoundsException e) |
240 |
{ |
{ |
241 |
throw new NoSuchElementException("no more content"); |
throw new NoSuchElementException ("no more content"); |
242 |
} |
} |
243 |
} |
} |
244 |
|
|
245 |
public void remove () |
public void remove() |
246 |
{ |
{ |
247 |
try |
try |
248 |
{ |
{ |
249 |
File[] newDirList = new File[dirList.length - 1]; |
File[] newDirList = new File [dirList.length - 1]; |
250 |
int realIndex = index - 1; |
int realIndex = index - 1; |
251 |
|
|
252 |
if (realIndex < 1) |
if (realIndex < 1) |
253 |
{ |
{ |
254 |
System.arraycopy(dirList, 1, newDirList, 0, dirList.length - 1); |
System.arraycopy (dirList, 1, newDirList, 0, |
255 |
|
dirList.length - 1); |
256 |
index--; |
index--; |
257 |
} |
} |
258 |
else |
else |
259 |
{ |
{ |
260 |
System.arraycopy(dirList, 0, newDirList, 0, realIndex); |
System.arraycopy (dirList, 0, newDirList, 0, realIndex); |
261 |
|
|
262 |
if (index < dirList.length - 1) |
if (index < dirList.length - 1) |
263 |
System.arraycopy(dirList, index, |
System.arraycopy (dirList, index, |
264 |
newDirList, realIndex, dirList.length - realIndex); |
newDirList, realIndex, |
265 |
|
dirList.length - realIndex); |
266 |
} |
} |
267 |
dirList = newDirList; |
dirList = newDirList; |
268 |
} |
} |
275 |
} |
} |
276 |
}; |
}; |
277 |
} |
} |
278 |
|
|
279 |
} // class FileURLConnection |
} // class FileURLConnection |
|
|
|