2004-09-10 Michael Koch <konqueror@gmx.de>
authorMichael Koch <konqueror@gmx.de>
Fri, 10 Sep 2004 07:20:09 +0000 (07:20 +0000)
committerMichael Koch <mkoch@gcc.gnu.org>
Fri, 10 Sep 2004 07:20:09 +0000 (07:20 +0000)
* gnu/java/net/protocol/file/Connection.java
(lineSeparator): Made non-final.
(static): Removed.
(connect): Initialize lineSeparator lazily. Use ByteArrayInputStream
instead of StringBufferInputStream.

2004-09-10  Michael Koch  <konqueror@gmx.de>

* gnu/java/net/protocol/file/Connection.java
(connect): Handle file is a directory case.

From-SVN: r87279

libjava/ChangeLog
libjava/gnu/java/net/protocol/file/Connection.java

index c3c457b86a5efd14b17330fdf1b1812f054ea0c8..2ad07335bd2a0091d247d38bf274cf6755939b09 100644 (file)
@@ -1,3 +1,16 @@
+2004-09-10  Michael Koch  <konqueror@gmx.de>
+
+       * gnu/java/net/protocol/file/Connection.java
+       (lineSeparator): Made non-final.
+       (static): Removed.
+       (connect): Initialize lineSeparator lazily. Use ByteArrayInputStream
+       instead of StringBufferInputStream.
+
+2004-09-10  Michael Koch  <konqueror@gmx.de>
+
+       * gnu/java/net/protocol/file/Connection.java
+       (connect): Handle file is a directory case.
+
 2004-09-10  Michael Koch  <konqueror@gmx.de>
 
        * Makefile.am
index 4da6e882c002d4cda7de2df41a479e1d58801824..3927130101b84752843c809537f12c12a0795ec4 100644 (file)
@@ -37,8 +37,11 @@ exception statement from your version.  */
 
 package gnu.java.net.protocol.file;
 
+import gnu.java.security.action.GetPropertyAction;
+
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -50,6 +53,7 @@ import java.net.ProtocolException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.security.Permission;
+import java.security.AccessController;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Locale;
@@ -71,6 +75,8 @@ public class Connection extends URLConnection
     = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss 'GMT'",
                            new Locale ("En", "Us", "Unix"));
 
+  private static String lineSeparator;
+  
   /**
    * This is a File object for this connection
    */
@@ -105,11 +111,38 @@ public class Connection extends URLConnection
     
     // If not connected, then file needs to be openned.
     file = new File (getURL().getFile());
-    if (doInput)
-      inputStream = new BufferedInputStream(new FileInputStream(file));
+
+    if (! file.isDirectory())
+      {
+       if (doInput)
+         inputStream = new BufferedInputStream(new FileInputStream(file));
     
-    if (doOutput)
-      outputStream = new BufferedOutputStream(new FileOutputStream(file));
+       if (doOutput)
+         outputStream = new BufferedOutputStream(new FileOutputStream(file));
+      }
+    else
+      {
+       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());
+         }
+
+       if (doOutput)
+         throw new ProtocolException
+           ("file: protocol does not support output on directories");
+      }
     
     connected = true;
   }
@@ -184,6 +217,7 @@ public class Connection extends URLConnection
 
   /**
    * Get the length of content.
+   *
    * @return the length of the content.
    */
   public int getContentLength()
@@ -212,7 +246,7 @@ public class Connection extends URLConnection
       {
        if (!connected)
          connect();
-
+        
        return file.lastModified();
       }
     catch (IOException e)
@@ -220,7 +254,7 @@ public class Connection extends URLConnection
        return -1;
       }
   }
-
+  
   /**
    * This method returns a <code>Permission</code> object representing the
    * permissions required to access this URL.  This method returns a