summaryrefslogtreecommitdiff
path: root/libjava
diff options
context:
space:
mode:
authormkoch <mkoch@138bc75d-0d04-0410-961f-82ee72b054a4>2005-04-27 21:03:00 +0000
committermkoch <mkoch@138bc75d-0d04-0410-961f-82ee72b054a4>2005-04-27 21:03:00 +0000
commit184b7bf54c26c7366ba668d7788610fb5111846e (patch)
treeb58fc962d6b987c3d38b9da9dd5fe177155c159a /libjava
parentfc6c4b2c5fcf758f99662a37b3f23bf1def6a9cb (diff)
downloadgcc-184b7bf54c26c7366ba668d7788610fb5111846e.tar.gz
2005-04-27 Chris Burdess <dog@gnu.org>
* gnu/java/net/protocol/file/Connection.java: Return correct content length for directory listing. * java/net/URLClassLoader.java: Correction for URLClassLoader, bug #11285: return valid URLs for directories.i git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@98884 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava')
-rw-r--r--libjava/ChangeLog7
-rw-r--r--libjava/gnu/java/net/protocol/file/Connection.java69
-rw-r--r--libjava/java/net/URLClassLoader.java27
3 files changed, 88 insertions, 15 deletions
diff --git a/libjava/ChangeLog b/libjava/ChangeLog
index cfe27e53305..ec4edf1376c 100644
--- a/libjava/ChangeLog
+++ b/libjava/ChangeLog
@@ -1,3 +1,10 @@
+2005-04-27 Chris Burdess <dog@gnu.org>
+
+ * gnu/java/net/protocol/file/Connection.java: Return correct content
+ length for directory listing.
+ * java/net/URLClassLoader.java: Correction for URLClassLoader, bug
+ #11285: return valid URLs for directories.i
+
2005-04-27 Thomas Fitzsimmons <fitzsim@redhat.com>
* Makefile.am (ordinary_java_source_files): Add
diff --git a/libjava/gnu/java/net/protocol/file/Connection.java b/libjava/gnu/java/net/protocol/file/Connection.java
index 77f8453406f..0f6e4d6f2b9 100644
--- a/libjava/gnu/java/net/protocol/file/Connection.java
+++ b/libjava/gnu/java/net/protocol/file/Connection.java
@@ -42,6 +42,7 @@ import gnu.java.security.action.GetPropertyAction;
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;
@@ -49,6 +50,8 @@ import java.io.FilePermission;
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;
@@ -82,12 +85,26 @@ public class Connection extends 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;
@@ -136,19 +153,7 @@ public class Connection extends URLConnection
{
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)
@@ -158,6 +163,32 @@ public class Connection extends URLConnection
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.
@@ -231,7 +262,13 @@ public class Connection extends URLConnection
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)
@@ -259,6 +296,10 @@ public class Connection extends URLConnection
if (!connected)
connect();
+ if (file.isDirectory())
+ {
+ return getDirectoryListing().length;
+ }
return (int) file.length();
}
catch (IOException e)
diff --git a/libjava/java/net/URLClassLoader.java b/libjava/java/net/URLClassLoader.java
index 9593e7d0fb0..4b9599816a9 100644
--- a/libjava/java/net/URLClassLoader.java
+++ b/libjava/java/net/URLClassLoader.java
@@ -610,7 +610,7 @@ public class URLClassLoader extends SecureClassLoader
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;
}
@@ -628,11 +628,36 @@ public class URLClassLoader extends SecureClassLoader
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();
}