/[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 by mkoch, Wed Feb 11 19:14:33 2004 UTC revision 1.8.2.1 by gnu_andrew, Fri Jan 14 10:24:04 2005 UTC
# Line 37  exception statement from your version. Line 37  exception statement from your version.
37    
38  package gnu.java.net.protocol.file;  package gnu.java.net.protocol.file;
39    
40    import gnu.java.security.action.GetPropertyAction;
41    
42  import java.io.BufferedInputStream;  import java.io.BufferedInputStream;
43  import java.io.BufferedOutputStream;  import java.io.BufferedOutputStream;
44    import java.io.ByteArrayInputStream;
45  import java.io.File;  import java.io.File;
46  import java.io.FileInputStream;  import java.io.FileInputStream;
47  import java.io.FileOutputStream;  import java.io.FileOutputStream;
# Line 50  import java.net.ProtocolException; Line 53  import java.net.ProtocolException;
53  import java.net.URL;  import java.net.URL;
54  import java.net.URLConnection;  import java.net.URLConnection;
55  import java.security.Permission;  import java.security.Permission;
56  import java.util.AbstractSet;  import java.security.AccessController;
57  import java.util.Iterator;  import java.text.SimpleDateFormat;
58  import java.util.Set;  import java.util.Date;
59  import java.util.NoSuchElementException;  import java.util.Locale;
60    
61  /**  /**
62   * This subclass of java.net.URLConnection models a URLConnection via   * This subclass of java.net.URLConnection models a URLConnection via
# Line 71  public class Connection extends URLConne Line 74  public class Connection extends URLConne
74    private static final String DEFAULT_PERMISSION = "read";    private static final String DEFAULT_PERMISSION = "read";
75    
76    /**    /**
77       * HTTP-style DateFormat, used to format the last-modified header.
78       */
79      private static SimpleDateFormat dateFormat
80        = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss 'GMT'",
81                               new Locale ("En", "Us", "Unix"));
82    
83      private static String lineSeparator;
84      
85      /**
86     * This is a File object for this connection     * This is a File object for this connection
87     */     */
88    private File file;    private File file;
# Line 111  public class Connection extends URLConne Line 123  public class Connection extends URLConne
123            
124      // If not connected, then file needs to be openned.      // If not connected, then file needs to be openned.
125      file = new File (getURL().getFile());      file = new File (getURL().getFile());
126      if (doInput)  
127        inputStream = new BufferedInputStream(new FileInputStream(file));      if (! file.isDirectory())
128          {
129            if (doInput)
130              inputStream = new BufferedInputStream(new FileInputStream(file));
131            
132      if (doOutput)          if (doOutput)
133        outputStream = new BufferedOutputStream(new FileOutputStream(file));            outputStream = new BufferedOutputStream(new FileOutputStream(file));
134          }
135        else
136          {
137            if (doInput)
138              {
139                if (lineSeparator == null)
140                  {
141                    GetPropertyAction getProperty = new GetPropertyAction("line.separator");
142                    lineSeparator = (String) AccessController.doPrivileged(getProperty);
143                  }
144                
145                StringBuffer sb = new StringBuffer();
146                String[] files = file.list();
147    
148                for (int index = 0; index < files.length; ++index)
149                   sb.append(files[index]).append(lineSeparator);
150    
151                inputStream = new ByteArrayInputStream(sb.toString().getBytes());
152              }
153    
154            if (doOutput)
155              throw new ProtocolException
156                ("file: protocol does not support output on directories");
157          }
158            
159      connected = true;      connected = true;
160    }    }
# Line 178  public class Connection extends URLConne Line 217  public class Connection extends URLConne
217          return -1;          return -1;
218        }        }
219    }    }
220      
221      /**
222       *  Get an http-style header field. Just handle a few common ones.
223       */
224      public String getHeaderField(String field)
225      {
226        try
227          {
228            if (!connected)
229              connect();
230    
231            if (field.equals("content-type"))
232              return guessContentTypeFromName(file.getName());
233            else if (field.equals("content-length"))
234              return Long.toString(file.length());
235            else if (field.equals("last-modified"))
236              {
237                synchronized (dateFormat)
238                  {
239                    return dateFormat.format(new Date(file.lastModified()));
240                  }
241              }
242          }
243        catch (IOException e)
244          {
245            // Fall through.
246          }
247        return null;
248      }
249    
250    /**    /**
251     * Get the length of content.     * Get the length of content.
# Line 211  public class Connection extends URLConne Line 279  public class Connection extends URLConne
279    {    {
280      return permission;      return permission;
281    }    }
   
   /**  
    * Does the resource pointed to actually exist?  
    */  
   public final boolean exists()  
   {  
     if (file == null)  
       return false;  
   
     return file.exists();  
   }  
   
   /**  
    * Is the resource pointed to a directory?  
    */  
   public final boolean isDirectory()  
   {  
     return file.isDirectory();  
   }  
     
   /**  
    * Get a listing of the directory, if it is a directory.  
    *  
    * @return a set which can supply an iteration of the  
    * contents of the directory.  
    *  
    * @throws IllegalStateException if this is not pointing  
    * to a directory.  
    */  
   public Set getListing()  
   {  
     if (!file.isDirectory())  
       throw new IllegalStateException ("this is not a directory");  
       
     final File[] directoryList = file.listFiles();  
     return new AbstractSet()  
       {  
         File[] dirList = directoryList;  
   
         public int size()  
         {  
           return dirList.length;  
         }  
   
         public Iterator iterator()  
         {  
           return new Iterator()  
             {  
               int index = 0;  
   
               public boolean hasNext()  
               {  
                 return index < dirList.length;  
               }  
   
               public Object next()  
               {  
                 try  
                   {  
                     String value = dirList [index++].getName();  
                     return value;  
                   }  
                 catch (ArrayIndexOutOfBoundsException e)  
                   {  
                     throw new NoSuchElementException ("no more content");  
                   }  
               }  
   
               public void remove()  
               {  
                 try  
                   {  
                     File[] newDirList = new File [dirList.length - 1];  
                     int realIndex = index - 1;  
                       
                     if (realIndex < 1)  
                       {  
                         System.arraycopy (dirList, 1, newDirList, 0,  
                                           dirList.length - 1);  
                         index--;  
                       }  
                     else  
                       {  
                         System.arraycopy (dirList, 0, newDirList, 0, realIndex);  
                           
                         if (index < dirList.length - 1)  
                           System.arraycopy (dirList, index,  
                                             newDirList, realIndex,  
                                             dirList.length - realIndex);  
                       }  
                     dirList = newDirList;  
                   }  
                 catch (ArrayIndexOutOfBoundsException e)  
                   {  
                     throw new NoSuchElementException("no more content");  
                   }  
               }  
             };  
         }  
       };  
   }  
282  }  }

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.8.2.1

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