38 |
*/ |
*/ |
39 |
package gnu.java.net.protocol.file; |
package gnu.java.net.protocol.file; |
40 |
|
|
41 |
|
import java.util.AbstractSet; |
42 |
|
import java.util.Iterator; |
43 |
|
import java.util.Set; |
44 |
|
import java.util.NoSuchElementException; |
45 |
|
import java.io.*; |
46 |
|
|
47 |
/** |
/** |
48 |
* This subclass of java.net.URLConnection models a URLConnection via |
* This subclass of java.net.URLConnection models a URLConnection via |
49 |
* the "file" protocol. |
* the "file" protocol. |
50 |
* |
* |
51 |
* @author Aaron M. Renn (arenn@urbanophile.com) |
* @author Aaron M. Renn (arenn@urbanophile.com) |
52 |
|
* @author Nic Ferrier (nferrier@tapsellferrier.co.uk) |
53 |
*/ |
*/ |
54 |
public class FileURLConnection extends java.net.URLConnection |
public class FileURLConnection extends java.net.URLConnection |
55 |
{ |
{ |
68 |
* OutputStream if we are writing to the file |
* OutputStream if we are writing to the file |
69 |
*/ |
*/ |
70 |
private java.io.FileOutputStream out_stream; |
private java.io.FileOutputStream out_stream; |
|
|
|
|
|
|
71 |
|
|
72 |
/** |
/** |
73 |
* Calls superclass constructor to initialize. |
* Calls superclass constructor to initialize. |
131 |
return (out_stream); |
return (out_stream); |
132 |
} |
} |
133 |
|
|
134 |
|
/** Get the last modified time of the resource. |
135 |
|
* |
136 |
|
* @return the time since epoch that the resource was modified. |
137 |
|
*/ |
138 |
|
public long getLastModified () |
139 |
|
{ |
140 |
|
try |
141 |
|
{ |
142 |
|
if (! connected) |
143 |
|
connect(); |
144 |
|
return file.lastModified(); |
145 |
|
} |
146 |
|
catch (IOException e) |
147 |
|
{ |
148 |
|
return -1; |
149 |
|
} |
150 |
|
} |
151 |
|
|
152 |
|
/** Get the length of content. |
153 |
|
* |
154 |
|
* @return the length of the content. |
155 |
|
*/ |
156 |
|
public int getContentLength () |
157 |
|
{ |
158 |
|
try |
159 |
|
{ |
160 |
|
if (! connected) |
161 |
|
connect(); |
162 |
|
return (int) file.length(); |
163 |
|
} |
164 |
|
catch (IOException e) |
165 |
|
{ |
166 |
|
return -1; |
167 |
|
} |
168 |
|
} |
169 |
|
|
170 |
|
|
171 |
|
// These are GNU only implementation methods. |
172 |
|
|
173 |
|
/** Does the resource pointed to actually exist? |
174 |
|
*/ |
175 |
|
public final boolean exists () |
176 |
|
{ |
177 |
|
if (file == null) |
178 |
|
return false; |
179 |
|
return file.exists(); |
180 |
|
} |
181 |
|
|
182 |
|
/** Is the resource pointed to a directory? |
183 |
|
*/ |
184 |
|
public final boolean isDirectory () |
185 |
|
{ |
186 |
|
return file.isDirectory(); |
187 |
|
} |
188 |
|
|
189 |
|
/** Get a listing of the directory, if it is a directory. |
190 |
|
* |
191 |
|
* @return a set which can supply an iteration of the |
192 |
|
* contents of the directory. |
193 |
|
* @throws IllegalStateException if this is not pointing |
194 |
|
* to a directory. |
195 |
|
*/ |
196 |
|
public Set getListing () |
197 |
|
{ |
198 |
|
if (! file.isDirectory()) |
199 |
|
throw new IllegalStateException("this is not a directory"); |
200 |
|
final File[] directoryList = file.listFiles(); |
201 |
|
return new AbstractSet() |
202 |
|
{ |
203 |
|
File[] dirList = directoryList; |
204 |
|
|
205 |
|
public int size () |
206 |
|
{ |
207 |
|
return dirList.length; |
208 |
|
} |
209 |
|
|
210 |
|
public Iterator iterator () |
211 |
|
{ |
212 |
|
return new Iterator() |
213 |
|
{ |
214 |
|
int index = 0; |
215 |
|
|
216 |
|
public boolean hasNext () |
217 |
|
{ |
218 |
|
return index < dirList.length; |
219 |
|
} |
220 |
|
|
221 |
|
public Object next () |
222 |
|
{ |
223 |
|
try |
224 |
|
{ |
225 |
|
String value = dirList[index++].getName(); |
226 |
|
return value; |
227 |
|
} |
228 |
|
catch (ArrayIndexOutOfBoundsException e) |
229 |
|
{ |
230 |
|
throw new NoSuchElementException("no more content"); |
231 |
|
} |
232 |
|
} |
233 |
|
|
234 |
|
public void remove () |
235 |
|
{ |
236 |
|
try |
237 |
|
{ |
238 |
|
File[] newDirList = new File[dirList.length - 1]; |
239 |
|
int realIndex = index - 1; |
240 |
|
if (realIndex < 1) |
241 |
|
{ |
242 |
|
System.arraycopy(dirList, 1, newDirList, 0, dirList.length - 1); |
243 |
|
index--; |
244 |
|
} |
245 |
|
else |
246 |
|
{ |
247 |
|
System.arraycopy(dirList, 0, newDirList, 0, realIndex); |
248 |
|
if (index < dirList.length - 1) |
249 |
|
System.arraycopy(dirList, index, |
250 |
|
newDirList, realIndex, dirList.length - realIndex); |
251 |
|
} |
252 |
|
dirList = newDirList; |
253 |
|
} |
254 |
|
catch (ArrayIndexOutOfBoundsException e) |
255 |
|
{ |
256 |
|
throw new NoSuchElementException("no more content"); |
257 |
|
} |
258 |
|
} |
259 |
|
}; |
260 |
|
} |
261 |
|
}; |
262 |
|
} |
263 |
} // class FileURLConnection |
} // class FileURLConnection |
264 |
|
|