42 |
import java.io.BufferedInputStream; |
import java.io.BufferedInputStream; |
43 |
import java.io.BufferedOutputStream; |
import java.io.BufferedOutputStream; |
44 |
import java.io.ByteArrayInputStream; |
import java.io.ByteArrayInputStream; |
45 |
|
import java.io.ByteArrayOutputStream; |
46 |
import java.io.File; |
import java.io.File; |
47 |
import java.io.FileInputStream; |
import java.io.FileInputStream; |
48 |
import java.io.FileOutputStream; |
import java.io.FileOutputStream; |
50 |
import java.io.InputStream; |
import java.io.InputStream; |
51 |
import java.io.IOException; |
import java.io.IOException; |
52 |
import java.io.OutputStream; |
import java.io.OutputStream; |
53 |
|
import java.io.OutputStreamWriter; |
54 |
|
import java.io.Writer; |
55 |
import java.net.ProtocolException; |
import java.net.ProtocolException; |
56 |
import java.net.URL; |
import java.net.URL; |
57 |
import java.net.URLConnection; |
import java.net.URLConnection; |
95 |
private File file; |
private File file; |
96 |
|
|
97 |
/** |
/** |
98 |
|
* If a directory, contains a list of files in the directory. |
99 |
|
*/ |
100 |
|
private byte[] directoryListing; |
101 |
|
|
102 |
|
/** |
103 |
* InputStream if we are reading from the file |
* InputStream if we are reading from the file |
104 |
*/ |
*/ |
105 |
private InputStream inputStream; |
private InputStream inputStream; |
148 |
{ |
{ |
149 |
if (doInput) |
if (doInput) |
150 |
{ |
{ |
151 |
StringBuffer sb = new StringBuffer(); |
inputStream = new ByteArrayInputStream(getDirectoryListing()); |
|
String[] files = file.list(); |
|
|
|
|
|
for (int index = 0; index < files.length; ++index) |
|
|
sb.append(files[index]).append(StaticData.lineSeparator); |
|
|
|
|
|
inputStream = new ByteArrayInputStream(sb.toString().getBytes()); |
|
152 |
} |
} |
153 |
|
|
154 |
if (doOutput) |
if (doOutput) |
158 |
|
|
159 |
connected = true; |
connected = true; |
160 |
} |
} |
161 |
|
|
162 |
|
/** |
163 |
|
* Populates the <code>directoryListing</code> field with a byte array |
164 |
|
* containing a representation of the directory listing. |
165 |
|
*/ |
166 |
|
byte[] getDirectoryListing() |
167 |
|
throws IOException |
168 |
|
{ |
169 |
|
if (directoryListing == null) |
170 |
|
{ |
171 |
|
ByteArrayOutputStream sink = new ByteArrayOutputStream(); |
172 |
|
// NB uses default character encoding for this system |
173 |
|
Writer writer = new OutputStreamWriter(sink); |
174 |
|
|
175 |
|
String[] files = file.list(); |
176 |
|
|
177 |
|
for (int i = 0; i < files.length; i++) |
178 |
|
{ |
179 |
|
writer.write(files[i]); |
180 |
|
writer.write(StaticData.lineSeparator); |
181 |
|
} |
182 |
|
|
183 |
|
directoryListing = sink.toByteArray(); |
184 |
|
} |
185 |
|
return directoryListing; |
186 |
|
} |
187 |
|
|
188 |
/** |
/** |
189 |
* Opens the file for reading and returns a stream for it. |
* Opens the file for reading and returns a stream for it. |
257 |
if (field.equals("content-type")) |
if (field.equals("content-type")) |
258 |
return guessContentTypeFromName(file.getName()); |
return guessContentTypeFromName(file.getName()); |
259 |
else if (field.equals("content-length")) |
else if (field.equals("content-length")) |
260 |
return Long.toString(file.length()); |
{ |
261 |
|
if (file.isDirectory()) |
262 |
|
{ |
263 |
|
return Integer.toString(getContentLength()); |
264 |
|
} |
265 |
|
return Long.toString(file.length()); |
266 |
|
} |
267 |
else if (field.equals("last-modified")) |
else if (field.equals("last-modified")) |
268 |
{ |
{ |
269 |
synchronized (StaticData.dateFormat) |
synchronized (StaticData.dateFormat) |
292 |
if (!connected) |
if (!connected) |
293 |
connect(); |
connect(); |
294 |
|
|
295 |
|
if (file.isDirectory()) |
296 |
|
{ |
297 |
|
return getDirectoryListing().length; |
298 |
|
} |
299 |
return (int) file.length(); |
return (int) file.length(); |
300 |
} |
} |
301 |
catch (IOException e) |
catch (IOException e) |