/[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.14 by gnu_andrew, Sat Feb 12 14:26:02 2005 UTC revision 1.15 by dog, Wed Mar 2 08:46:34 2005 UTC
# 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;
# Line 49  import java.io.FilePermission; Line 50  import java.io.FilePermission;
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;
# 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.14  
changed lines
  Added in v.1.15

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