import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
private static String lineSeparator;
+ static
+ {
+ if (lineSeparator == null)
+ {
+ GetPropertyAction getProperty = new GetPropertyAction("line.separator");
+ lineSeparator = (String) AccessController.doPrivileged(getProperty);
+ }
+ }
+
/**
* This is a File object for this connection
*/
private File file;
/**
+ * If a directory, contains a list of files in the directory.
+ */
+ private byte[] directoryListing;
+
+ /**
* InputStream if we are reading from the file
*/
private InputStream inputStream;
{
if (doInput)
{
- if (lineSeparator == null)
- {
- GetPropertyAction getProperty = new GetPropertyAction("line.separator");
- lineSeparator = (String) AccessController.doPrivileged(getProperty);
- }
-
- StringBuffer sb = new StringBuffer();
- String[] files = file.list();
-
- for (int index = 0; index < files.length; ++index)
- sb.append(files[index]).append(lineSeparator);
-
- inputStream = new ByteArrayInputStream(sb.toString().getBytes());
+ inputStream = new ByteArrayInputStream(getDirectoryListing());
}
if (doOutput)
connected = true;
}
+
+ /**
+ * Populates the <code>directoryListing</code> field with a byte array
+ * containing a representation of the directory listing.
+ */
+ byte[] getDirectoryListing()
+ throws IOException
+ {
+ if (directoryListing == null)
+ {
+ ByteArrayOutputStream sink = new ByteArrayOutputStream();
+ // NB uses default character encoding for this system
+ Writer writer = new OutputStreamWriter(sink);
+
+ String[] files = file.list();
+
+ for (int i = 0; i < files.length; i++)
+ {
+ writer.write(files[i]);
+ writer.write(lineSeparator);
+ }
+
+ directoryListing = sink.toByteArray();
+ }
+ return directoryListing;
+ }
/**
* Opens the file for reading and returns a stream for it.
if (field.equals("content-type"))
return guessContentTypeFromName(file.getName());
else if (field.equals("content-length"))
- return Long.toString(file.length());
+ {
+ if (file.isDirectory())
+ {
+ return Integer.toString(getContentLength());
+ }
+ return Long.toString(file.length());
+ }
else if (field.equals("last-modified"))
{
synchronized (dateFormat)
if (!connected)
connect();
+ if (file.isDirectory())
+ {
+ return getDirectoryListing().length;
+ }
return (int) file.length();
}
catch (IOException e)
Resource getResource(String name)
{
File file = new File(dir, name);
- if (file.exists() && ! file.isDirectory())
+ if (file.exists())
return new FileResource(this, name, file);
return null;
}
InputStream getInputStream() throws IOException
{
+ // Delegate to the URL content handler mechanism to retrieve an
+ // HTML representation of the directory listing if a directory
+ if (file.isDirectory())
+ {
+ URL url = getURL();
+ return url.openStream();
+ }
+ // Otherwise simply return a FileInputStream
return new FileInputStream(file);
}
public int getLength()
{
+ // Delegate to the URL content handler mechanism to retrieve the
+ // length of the HTML representation of the directory listing if
+ // a directory, or -1 if an exception occurs opening the directory.
+ if (file.isDirectory())
+ {
+ URL url = getURL();
+ try
+ {
+ URLConnection connection = url.openConnection();
+ return connection.getContentLength();
+ }
+ catch (IOException e)
+ {
+ return -1;
+ }
+ }
+ // Otherwise simply return the file length
return (int) file.length();
}