home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VPage / Java.bin / CLASSES.ZIP / sun / net / www / protocol / systemresource / SystemResourceManager.class (.txt) < prev   
Encoding:
Java Class File  |  1997-07-08  |  3.8 KB  |  154 lines

  1. package sun.net.www.protocol.systemresource;
  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.IOException;
  8. import java.io.InputStream;
  9. import java.net.MalformedURLException;
  10. import java.net.URL;
  11. import java.net.URLConnection;
  12. import sun.applet.AppletAudioClip;
  13. import sun.awt.image.ByteArrayImageSource;
  14. import sun.awt.image.FileImageSource;
  15.  
  16. public class SystemResourceManager {
  17.    private static boolean debug;
  18.    private URL url;
  19.    private String base;
  20.    private String member;
  21.    private boolean isZip;
  22.    private String compoundName;
  23.  
  24.    SystemResourceManager(URL var1) throws MalformedURLException {
  25.       this.url = var1;
  26.       ParseSystemURL var2 = new ParseSystemURL(var1);
  27.       this.base = var2.getBase();
  28.       this.member = var2.getMember();
  29.       this.isZip = var2.isZip();
  30.       if (this.base == null) {
  31.          this.compoundName = this.member;
  32.       } else {
  33.          this.compoundName = this.base + File.separator + this.member;
  34.       }
  35.  
  36.       if (!var2.isValid()) {
  37.          throw new MalformedURLException(var1 + " is not a valid system resource URL");
  38.       } else if (!validateSystemResource(this.isZip, this.base, this.member)) {
  39.          throw new SecurityException(var1 + " refers to a non system resource");
  40.       }
  41.    }
  42.  
  43.    synchronized Object getLocalResource() {
  44.       return this.isZip ? this.getFromZip() : this.getFromFile();
  45.    }
  46.  
  47.    InputStream getLocalResourceStream() {
  48.       debug("SystemResourceManager.getLocalResourceStream::");
  49.       if (this.isZip) {
  50.          debug("  isZip; base: " + this.base + " member: " + this.member);
  51.          byte[] var1 = getZipResourceByteArray(this.base, this.member);
  52.          return new ByteArrayInputStream(var1);
  53.       } else {
  54.          try {
  55.             return new BufferedInputStream(new FileInputStream(this.compoundName));
  56.          } catch (Exception var2) {
  57.             debug("Could not locate a resource base: " + this.base + " member: " + this.member);
  58.             return null;
  59.          }
  60.       }
  61.    }
  62.  
  63.    private Object getFromZip() {
  64.       byte[] var2 = getZipResourceByteArray(this.base, this.member);
  65.       ByteArrayInputStream var3 = new ByteArrayInputStream(var2);
  66.  
  67.       String var1;
  68.       try {
  69.          var1 = URLConnection.guessContentTypeFromStream(var3);
  70.       } catch (IOException var4) {
  71.          var1 = null;
  72.       }
  73.  
  74.       if (var1 != null) {
  75.          if (var1.startsWith("image")) {
  76.             return new ByteArrayImageSource(var2);
  77.          }
  78.  
  79.          if (var1.startsWith("audio")) {
  80.             return new AppletAudioClip(var2);
  81.          }
  82.       }
  83.  
  84.       return var3;
  85.    }
  86.  
  87.    private Object getFromFile() {
  88.       BufferedInputStream var2;
  89.       try {
  90.          var2 = new BufferedInputStream(new FileInputStream(this.compoundName));
  91.       } catch (Exception var5) {
  92.          debug("Could not locate a resource base: " + this.base + " member: " + this.member);
  93.          return null;
  94.       }
  95.  
  96.       String var1;
  97.       try {
  98.          var1 = URLConnection.guessContentTypeFromStream(var2);
  99.       } catch (IOException var4) {
  100.          debug("Exception while guessing Content Type; e: " + var4);
  101.          var1 = null;
  102.       }
  103.  
  104.       if (var1 != null) {
  105.          if (var1.startsWith("image")) {
  106.             debug("getFromFile:: base: " + this.base + " member: " + this.member);
  107.             return new FileImageSource(this.compoundName);
  108.          }
  109.  
  110.          if (var1.startsWith("audio")) {
  111.             byte[] var3 = byteArrayFromStream(var2);
  112.             return new AppletAudioClip(var3);
  113.          }
  114.       }
  115.  
  116.       return var2;
  117.    }
  118.  
  119.    private static byte[] byteArrayFromStream(InputStream var0) {
  120.       try {
  121.          int var1 = var0.available();
  122.          byte[] var2 = new byte[var1];
  123.  
  124.          int var4;
  125.          for(int var3 = 0; var3 < var1; var3 += var4) {
  126.             var4 = var0.read(var2, var3, var1 - var3);
  127.             if (var4 < 0) {
  128.                return null;
  129.             }
  130.          }
  131.  
  132.          return var2;
  133.       } catch (Exception var5) {
  134.          debug("Unexpected internal exception... " + var5);
  135.          throw new Error("Unexpected internal exception");
  136.       }
  137.    }
  138.  
  139.    static void debug(String var0) {
  140.       if (debug) {
  141.          System.out.println("SystemResourceManager:: " + var0);
  142.       }
  143.  
  144.    }
  145.  
  146.    private static native byte[] getZipResourceByteArray(String var0, String var1);
  147.  
  148.    private static native boolean validateSystemResource(boolean var0, String var1, String var2);
  149.  
  150.    static {
  151.       System.loadLibrary("sysresource");
  152.    }
  153. }
  154.