home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &n…he Search for Life DVD 2 / DVD-ROM.iso / install / jre1_3 / lib / rt.jar / sun / net / www / protocol / file / FileURLConnection.class (.txt) next >
Encoding:
Java Class File  |  1979-12-31  |  4.9 KB  |  203 lines

  1. package sun.net.www.protocol.file;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.FilePermission;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.net.FileNameMap;
  12. import java.net.URL;
  13. import java.security.AccessController;
  14. import java.security.Permission;
  15. import java.text.Collator;
  16. import java.util.Arrays;
  17. import java.util.Collections;
  18. import java.util.List;
  19. import sun.net.www.MessageHeader;
  20. import sun.net.www.MimeEntry;
  21. import sun.net.www.MimeTable;
  22. import sun.net.www.ParseUtil;
  23. import sun.net.www.URLConnection;
  24. import sun.security.action.GetBooleanAction;
  25. import sun.security.action.GetIntegerAction;
  26. import sun.security.action.GetPropertyAction;
  27.  
  28. public class FileURLConnection extends URLConnection {
  29.    static String CONTENT_LENGTH = "content-length";
  30.    static String CONTENT_TYPE = "content-type";
  31.    static String TEXT_HTML = "text/html";
  32.    String contentType;
  33.    // $FF: renamed from: is java.io.InputStream
  34.    InputStream field_0;
  35.    File file;
  36.    String filename;
  37.    boolean isDirectory = false;
  38.    boolean exists = false;
  39.    List files;
  40.    long length = 0L;
  41.    private boolean initializedHeaders = false;
  42.    Permission permission;
  43.  
  44.    FileURLConnection(URL var1) {
  45.       super(var1);
  46.    }
  47.  
  48.    public void connect() throws IOException {
  49.       if (!super.connected) {
  50.          try {
  51.             String var1 = ParseUtil.decode(super.url.getPath());
  52.             this.file = new File(var1.replace('/', File.separatorChar));
  53.             this.filename = this.file.toString();
  54.             this.isDirectory = this.file.isDirectory();
  55.             if (this.isDirectory) {
  56.                this.files = Arrays.asList(this.file.list());
  57.             } else {
  58.                this.field_0 = new BufferedInputStream(new FileInputStream(this.filename));
  59.             }
  60.          } catch (IOException var2) {
  61.             throw var2;
  62.          }
  63.  
  64.          super.connected = true;
  65.       }
  66.  
  67.    }
  68.  
  69.    private void initializeHeaders() {
  70.       try {
  71.          this.connect();
  72.          this.exists = this.file.exists();
  73.       } catch (IOException var2) {
  74.       }
  75.  
  76.       if (!this.initializedHeaders || !this.exists) {
  77.          this.length = this.file.length();
  78.          if (!this.isDirectory) {
  79.             FileNameMap var1 = java.net.URLConnection.getFileNameMap();
  80.             this.contentType = var1.getContentTypeFor(this.filename);
  81.             if (this.contentType != null) {
  82.                super.properties.add(CONTENT_TYPE, this.contentType);
  83.             }
  84.  
  85.             super.properties.add(CONTENT_LENGTH, String.valueOf(this.length));
  86.          } else {
  87.             super.properties.add(CONTENT_LENGTH, TEXT_HTML);
  88.          }
  89.  
  90.          this.initializedHeaders = true;
  91.       }
  92.  
  93.    }
  94.  
  95.    public String getHeaderField(String var1) {
  96.       this.initializeHeaders();
  97.       return super.getHeaderField(var1);
  98.    }
  99.  
  100.    public String getHeaderField(int var1) {
  101.       this.initializeHeaders();
  102.       return super.getHeaderField(var1);
  103.    }
  104.  
  105.    public int getContentLength() {
  106.       this.initializeHeaders();
  107.       return super.getContentLength();
  108.    }
  109.  
  110.    public String getHeaderFieldKey(int var1) {
  111.       this.initializeHeaders();
  112.       return super.getHeaderFieldKey(var1);
  113.    }
  114.  
  115.    public MessageHeader getProperties() {
  116.       this.initializeHeaders();
  117.       return super.getProperties();
  118.    }
  119.  
  120.    public synchronized InputStream getInputStream() throws IOException {
  121.       this.connect();
  122.       if (this.field_0 == null) {
  123.          int var1 = (Integer)AccessController.doPrivileged(new GetIntegerAction("hotjava.file.iconheight", 32));
  124.          int var2 = (Integer)AccessController.doPrivileged(new GetIntegerAction("hotjava.file.iconwidth", 32));
  125.          if (!this.isDirectory) {
  126.             throw new FileNotFoundException(this.filename);
  127.          }
  128.  
  129.          FileNameMap var3 = java.net.URLConnection.getFileNameMap();
  130.          StringBuffer var4 = new StringBuffer();
  131.          if (this.files == null) {
  132.             throw new FileNotFoundException(this.filename);
  133.          }
  134.  
  135.          Collections.sort(this.files, Collator.getInstance());
  136.          var4.append("<title>");
  137.          var4.append((String)AccessController.doPrivileged(new GetPropertyAction("file.dir.title", "Directory Listing")));
  138.          var4.append("</title>\n");
  139.          var4.append("<base href=\"file://localhost/");
  140.          var4.append(this.filename.substring(this.filename.charAt(0) == '/' ? 1 : 0));
  141.          if (this.filename.endsWith("/")) {
  142.             var4.append("\">");
  143.          } else {
  144.             var4.append("/\">");
  145.          }
  146.  
  147.          var4.append("<h1>");
  148.          var4.append(this.filename);
  149.          var4.append("</h1>\n");
  150.          var4.append("<hr>\n");
  151.          Boolean var5 = (Boolean)AccessController.doPrivileged(new GetBooleanAction("file.hidedotfiles"));
  152.          boolean var6 = var5;
  153.  
  154.          for(int var7 = 0; var7 < this.files.size(); ++var7) {
  155.             String var8 = (String)this.files.get(var7);
  156.             if (!var6 || var8.indexOf(46) != 0) {
  157.                var4.append("<img align=middle src=\"");
  158.                if ((new File(this.filename + "/" + var8)).isDirectory()) {
  159.                   var4.append("doc:/lib/images/ftp/directory.gif\" width=" + var2 + " height=" + var1 + ">\n");
  160.                } else {
  161.                   String var9 = "doc:/lib/images/ftp/file.gif";
  162.                   if (var3 instanceof MimeTable) {
  163.                      MimeEntry var10 = ((MimeTable)var3).findByFileName(var8);
  164.                      if (var10 != null) {
  165.                         String var11 = var10.getImageFileName();
  166.                         if (var11 != null) {
  167.                            var9 = var11;
  168.                         }
  169.                      }
  170.                   }
  171.  
  172.                   var4.append(var9);
  173.                   var4.append("\" width=" + var2 + " height=" + var1 + ">\n");
  174.                }
  175.  
  176.                var4.append("<a href=\"");
  177.                var4.append(var8);
  178.                var4.append("\">");
  179.                var4.append(var8);
  180.                var4.append("</a><br>");
  181.             }
  182.          }
  183.  
  184.          this.field_0 = new ByteArrayInputStream(var4.toString().getBytes());
  185.       }
  186.  
  187.       return this.field_0;
  188.    }
  189.  
  190.    public Permission getPermission() throws IOException {
  191.       if (this.permission == null) {
  192.          String var1 = ParseUtil.decode(super.url.getPath());
  193.          if (File.separatorChar == '/') {
  194.             this.permission = new FilePermission(var1, "read");
  195.          } else {
  196.             this.permission = new FilePermission(var1.replace('/', File.separatorChar), "read");
  197.          }
  198.       }
  199.  
  200.       return this.permission;
  201.    }
  202. }
  203.