home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / prosrc.bin / StreamImageSource.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  4.9 KB  |  145 lines

  1. /*
  2.  * @(#StreamImageSource.java
  3.  *
  4.  * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
  5.  *
  6.  */
  7.  
  8. package symantec.itools.db.awt;
  9.  
  10. import java.io.InputStream;
  11. import java.io.BufferedInputStream;
  12. import java.net.URL;
  13. import java.net.MalformedURLException;
  14.  
  15.  
  16. /**
  17.  * An image producer which can produce the image data for Images from any
  18.  * input stream.  It currently supports GIF and JPEG formats.
  19.  * <P>
  20.  * For security reasons, whenever an instance of StreamInputSource is created,
  21.  * a security check is performed against a URL object that must point to the
  22.  * machine the input stream is pulling the image data from.  If the constructor is
  23.  * called without specifying a base URL, a default base maintained by the class
  24.  * is used for the security check. By default, the base URL is set with a
  25.  * protocol of type file.  If an image is being created within an
  26.  * applet that was delivered over a network, it will be necessary to change the
  27.  * base URL reference to the code base of the applet; otherwise, a security
  28.  * exception will result.
  29.  * <P>
  30.  * Here is an example of how to use the class to create an image within an applet:
  31.  * <p><pre><blockquote>
  32.  * public class ImageTest extends Applet {
  33.  *   Image       image;
  34.  *   StreamImageSource   sis;
  35.  *
  36.  *   public void init() {
  37.  *     try {
  38.  *       StreamImageSource.setBaseUrl(getCodeBase());
  39.  *
  40.  *       Toolkit     tk = getToolkit();
  41.  *       URL         url = new URL(getDocumentBase(), "test.jpg");
  42.  *       InputStream s = url.openStream();
  43.  *
  44.  *       //create an image using StreamImageSource and applet's toolkit
  45.  *       is = new StreamImageSource(is, StreamImageSource.JPEG);
  46.  *       image = tk.createImage(sis);
  47.  *     } catch(Exception e) { e.printStackTrace(); }
  48.  *   }
  49.  *
  50.  *   public void paint(Graphics g) {
  51.  *     g.drawImage(image, 0, 0, this);
  52.  *   }
  53.  * }
  54.  * </blockquote></pre>
  55.  */
  56. public class StreamImageSource extends sun.awt.image.URLImageSource
  57.                                implements java.awt.image.ImageProducer
  58. {
  59.     InputStream     stream;
  60.     int             type;
  61.     URL             base;
  62.  
  63.     static URL      baseUrl;
  64.  
  65.     /**
  66.      * Constant indicating image is in GIF format.
  67.      */
  68.     public static final int GIF = 0;
  69.     /**
  70.      * Constant indicating image is in JPEG format.
  71.      */
  72.     public static final int JPEG = 1;
  73.  
  74.     static {
  75.         //initialize the base url with the file protocol
  76.         try {
  77.             baseUrl = new URL("file://");
  78.         } catch(MalformedURLException e) {}
  79.     }
  80.  
  81.     /**
  82.      * Costructs a StreamImageSource using the specified stream that will deliver
  83.      * an image of the specified type.  A security check is performed against
  84.      * the base URL.
  85.      * @param is   an input stream connected to the data source for the image
  86.      * @param type the type of image (one of GIF or JPEG)
  87.      */
  88.     public StreamImageSource(InputStream is, int type) {
  89.         this(baseUrl , is, type);
  90.     }
  91.  
  92.     /**
  93.      * Costructs a StreamImageSource using the specified stream that will deliver
  94.      * an image of the specified type.  A security check is performed against
  95.      * the URL reference passed to the constructor.
  96.      * @param base the URL from which the input stream is gathering data
  97.      * @param is the input stream connected to the data source for the image
  98.      * @param type the type of image (one of GIF or JPEG)
  99.      */
  100.     public StreamImageSource(URL base, InputStream is, int type) {
  101.         super(base);
  102.  
  103.         if (type != GIF && type != JPEG) {
  104.             throw new IllegalArgumentException("Unsupported image type:" + type);
  105.         }
  106.  
  107.         stream = is;
  108.         this.type = type;
  109.     }
  110.  
  111.     /**
  112.      * Sets the default URL that security checks are performed against.
  113.      * This URL is used when an instance of StreamImageSource is created without
  114.      * specifying a base URL.
  115.      * @param base the URL for security checks
  116.      */
  117.     public static void setBaseUrl(URL base) { baseUrl = base; }
  118.  
  119.     /**
  120.      * Gets the default URL that security checks are performed against.
  121.      * This URL is used when an instance of StreamImageSource is created without
  122.      * specifying a base URL.
  123.      * @return the URL for security checks
  124.      */
  125.     public static URL getBaseUrl() { return baseUrl; }
  126.  
  127.     /**
  128.      * Creates the proper decoder based on the type of image specified
  129.      * in the constructor.
  130.      */
  131.     protected sun.awt.image.ImageDecoder getDecoder() {
  132.         InputStream is;
  133.         is = new BufferedInputStream(stream);
  134.  
  135.         switch (type) {
  136.             case GIF:
  137.                 return new sun.awt.image.GifImageDecoder(this, is);
  138.             case JPEG:
  139.                 return new sun.awt.image.JPEGImageDecoder(this, is);
  140.             default:
  141.                 throw new IllegalArgumentException("Unsupported image type:" + type);
  142.         }
  143.     }
  144. }
  145.