home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / sun / awt / image / URLImageSource.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  2.9 KB  |  101 lines

  1. package sun.awt.image;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8.  
  9. public class URLImageSource extends InputStreamImageSource {
  10.    URL url;
  11.    URLConnection conn;
  12.    String actualHost;
  13.    int actualPort;
  14.  
  15.    public URLImageSource(URL u) {
  16.       SecurityManager sm = System.getSecurityManager();
  17.       if (sm != null) {
  18.          sm.checkConnect(u.getHost(), u.getPort());
  19.       }
  20.  
  21.       this.url = u;
  22.    }
  23.  
  24.    public URLImageSource(String href) throws MalformedURLException {
  25.       this(new URL((URL)null, href));
  26.    }
  27.  
  28.    public URLImageSource(URL u, URLConnection uc) {
  29.       this(u);
  30.       this.conn = uc;
  31.    }
  32.  
  33.    public URLImageSource(URLConnection uc) {
  34.       this(uc.getURL(), uc);
  35.    }
  36.  
  37.    final boolean checkSecurity(Object context, boolean quiet) {
  38.       if (this.actualHost != null) {
  39.          try {
  40.             System.getSecurityManager().checkConnect(this.actualHost, this.actualPort, context);
  41.          } catch (SecurityException e) {
  42.             if (!quiet) {
  43.                throw e;
  44.             }
  45.  
  46.             return false;
  47.          }
  48.       }
  49.  
  50.       return true;
  51.    }
  52.  
  53.    private synchronized URLConnection getConnection() throws IOException {
  54.       URLConnection c;
  55.       if (this.conn != null) {
  56.          c = this.conn;
  57.          this.conn = null;
  58.       } else {
  59.          c = this.url.openConnection();
  60.       }
  61.  
  62.       return c;
  63.    }
  64.  
  65.    protected ImageDecoder getDecoder() {
  66.       InputStream is = null;
  67.       String type = null;
  68.  
  69.       try {
  70.          URLConnection c = this.getConnection();
  71.          is = c.getInputStream();
  72.          type = c.getContentType();
  73.          URL u = c.getURL();
  74.          if (u != this.url && (u.getHost() != this.url.getHost() || u.getPort() != this.url.getPort())) {
  75.             if (this.actualHost != null && (this.actualHost != u.getHost() || this.actualPort != u.getPort())) {
  76.                throw new SecurityException("image moved!");
  77.             }
  78.  
  79.             this.actualHost = u.getHost();
  80.             this.actualPort = u.getPort();
  81.          }
  82.       } catch (IOException var6) {
  83.          if (is != null) {
  84.             try {
  85.                is.close();
  86.             } catch (IOException var5) {
  87.             }
  88.          }
  89.  
  90.          return null;
  91.       }
  92.  
  93.       ImageDecoder id = ((InputStreamImageSource)this).decoderForType(is, type);
  94.       if (id == null) {
  95.          id = ((InputStreamImageSource)this).getDecoder(is);
  96.       }
  97.  
  98.       return id;
  99.    }
  100. }
  101.