home *** CD-ROM | disk | FTP | other *** search
- package sun.net.www.protocol.systemresource;
-
- import java.io.BufferedInputStream;
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import sun.applet.AppletAudioClip;
- import sun.awt.image.ByteArrayImageSource;
- import sun.awt.image.FileImageSource;
-
- public class SystemResourceManager {
- private static boolean debug;
- private URL url;
- private String base;
- private String member;
- private boolean isZip;
- private String compoundName;
-
- SystemResourceManager(URL var1) throws MalformedURLException {
- this.url = var1;
- ParseSystemURL var2 = new ParseSystemURL(var1);
- this.base = var2.getBase();
- this.member = var2.getMember();
- this.isZip = var2.isZip();
- if (this.base == null) {
- this.compoundName = this.member;
- } else {
- this.compoundName = this.base + File.separator + this.member;
- }
-
- if (!var2.isValid()) {
- throw new MalformedURLException(var1 + " is not a valid system resource URL");
- } else if (!validateSystemResource(this.isZip, this.base, this.member)) {
- throw new SecurityException(var1 + " refers to a non system resource");
- }
- }
-
- synchronized Object getLocalResource() {
- return this.isZip ? this.getFromZip() : this.getFromFile();
- }
-
- InputStream getLocalResourceStream() {
- debug("SystemResourceManager.getLocalResourceStream::");
- if (this.isZip) {
- debug(" isZip; base: " + this.base + " member: " + this.member);
- byte[] var1 = getZipResourceByteArray(this.base, this.member);
- return new ByteArrayInputStream(var1);
- } else {
- try {
- return new BufferedInputStream(new FileInputStream(this.compoundName));
- } catch (Exception var2) {
- debug("Could not locate a resource base: " + this.base + " member: " + this.member);
- return null;
- }
- }
- }
-
- private Object getFromZip() {
- byte[] var2 = getZipResourceByteArray(this.base, this.member);
- ByteArrayInputStream var3 = new ByteArrayInputStream(var2);
-
- String var1;
- try {
- var1 = URLConnection.guessContentTypeFromStream(var3);
- } catch (IOException var4) {
- var1 = null;
- }
-
- if (var1 != null) {
- if (var1.startsWith("image")) {
- return new ByteArrayImageSource(var2);
- }
-
- if (var1.startsWith("audio")) {
- return new AppletAudioClip(var2);
- }
- }
-
- return var3;
- }
-
- private Object getFromFile() {
- BufferedInputStream var2;
- try {
- var2 = new BufferedInputStream(new FileInputStream(this.compoundName));
- } catch (Exception var5) {
- debug("Could not locate a resource base: " + this.base + " member: " + this.member);
- return null;
- }
-
- String var1;
- try {
- var1 = URLConnection.guessContentTypeFromStream(var2);
- } catch (IOException var4) {
- debug("Exception while guessing Content Type; e: " + var4);
- var1 = null;
- }
-
- if (var1 != null) {
- if (var1.startsWith("image")) {
- debug("getFromFile:: base: " + this.base + " member: " + this.member);
- return new FileImageSource(this.compoundName);
- }
-
- if (var1.startsWith("audio")) {
- byte[] var3 = byteArrayFromStream(var2);
- return new AppletAudioClip(var3);
- }
- }
-
- return var2;
- }
-
- private static byte[] byteArrayFromStream(InputStream var0) {
- try {
- int var1 = var0.available();
- byte[] var2 = new byte[var1];
-
- int var4;
- for(int var3 = 0; var3 < var1; var3 += var4) {
- var4 = var0.read(var2, var3, var1 - var3);
- if (var4 < 0) {
- return null;
- }
- }
-
- return var2;
- } catch (Exception var5) {
- debug("Unexpected internal exception... " + var5);
- throw new Error("Unexpected internal exception");
- }
- }
-
- static void debug(String var0) {
- if (debug) {
- System.out.println("SystemResourceManager:: " + var0);
- }
-
- }
-
- private static native byte[] getZipResourceByteArray(String var0, String var1);
-
- private static native boolean validateSystemResource(boolean var0, String var1, String var2);
-
- static {
- System.loadLibrary("sysresource");
- }
- }
-