/[classpath]/classpath/gnu/java/net/protocol/file/Connection.java
ViewVC logotype

Diff of /classpath/gnu/java/net/protocol/file/Connection.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.8.2.3 by gnu_andrew, Wed Feb 16 01:11:40 2005 UTC revision 1.8.2.4 by gnu_andrew, Sun Mar 13 14:38:31 2005 UTC
# Line 33  module.  An independent module is a modu Line 33  module.  An independent module is a modu
33  or based on this library.  If you modify this library, you may extend  or based on this library.  If you modify this library, you may extend
34  this exception to your version of the library, but you are not  this exception to your version of the library, but you are not
35  obligated to do so.  If you do not wish to do so, delete this  obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version.  */  exception statement from your version. */
37    
38  package gnu.java.net.protocol.file;  package gnu.java.net.protocol.file;
39    
# Line 42  import gnu.classpath.SystemProperties; Line 42  import gnu.classpath.SystemProperties;
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;
49  import java.io.FilePermission;  import java.io.FilePermission;
 import java.io.InputStream;  
50  import java.io.IOException;  import java.io.IOException;
51    import java.io.InputStream;
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;
# Line 61  import java.util.Locale; Line 64  import java.util.Locale;
64   * This subclass of java.net.URLConnection models a URLConnection via   * This subclass of java.net.URLConnection models a URLConnection via
65   * the "file" protocol.   * the "file" protocol.
66   *   *
67   * @author Aaron M. Renn <arenn@urbanophile.com>   * @author Aaron M. Renn (arenn@urbanophile.com)
68   * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>   * @author Nic Ferrier (nferrier@tapsellferrier.co.uk)
69   * @author Warren Levy <warrenl@cygnus.com>   * @author Warren Levy (warrenl@cygnus.com)
70   */   */
71  public class Connection extends URLConnection  public class Connection extends URLConnection
72  {  {
# Line 92  public class Connection extends URLConne Line 95  public class Connection extends URLConne
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;
# Line 140  public class Connection extends URLConne Line 148  public class Connection extends URLConne
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)
# Line 156  public class Connection extends URLConne Line 158  public class Connection extends URLConne
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.
# Line 229  public class Connection extends URLConne Line 257  public class Connection extends URLConne
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)
# Line 258  public class Connection extends URLConne Line 292  public class Connection extends URLConne
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)

Legend:
Removed from v.1.8.2.3  
changed lines
  Added in v.1.8.2.4

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26