home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 4.9 KB | 145 lines |
- /*
- * @(#StreamImageSource.java
- *
- * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
- *
- */
-
- package symantec.itools.db.awt;
-
- import java.io.InputStream;
- import java.io.BufferedInputStream;
- import java.net.URL;
- import java.net.MalformedURLException;
-
-
- /**
- * An image producer which can produce the image data for Images from any
- * input stream. It currently supports GIF and JPEG formats.
- * <P>
- * For security reasons, whenever an instance of StreamInputSource is created,
- * a security check is performed against a URL object that must point to the
- * machine the input stream is pulling the image data from. If the constructor is
- * called without specifying a base URL, a default base maintained by the class
- * is used for the security check. By default, the base URL is set with a
- * protocol of type file. If an image is being created within an
- * applet that was delivered over a network, it will be necessary to change the
- * base URL reference to the code base of the applet; otherwise, a security
- * exception will result.
- * <P>
- * Here is an example of how to use the class to create an image within an applet:
- * <p><pre><blockquote>
- * public class ImageTest extends Applet {
- * Image image;
- * StreamImageSource sis;
- *
- * public void init() {
- * try {
- * StreamImageSource.setBaseUrl(getCodeBase());
- *
- * Toolkit tk = getToolkit();
- * URL url = new URL(getDocumentBase(), "test.jpg");
- * InputStream s = url.openStream();
- *
- * //create an image using StreamImageSource and applet's toolkit
- * is = new StreamImageSource(is, StreamImageSource.JPEG);
- * image = tk.createImage(sis);
- * } catch(Exception e) { e.printStackTrace(); }
- * }
- *
- * public void paint(Graphics g) {
- * g.drawImage(image, 0, 0, this);
- * }
- * }
- * </blockquote></pre>
- */
- public class StreamImageSource extends sun.awt.image.URLImageSource
- implements java.awt.image.ImageProducer
- {
- InputStream stream;
- int type;
- URL base;
-
- static URL baseUrl;
-
- /**
- * Constant indicating image is in GIF format.
- */
- public static final int GIF = 0;
- /**
- * Constant indicating image is in JPEG format.
- */
- public static final int JPEG = 1;
-
- static {
- //initialize the base url with the file protocol
- try {
- baseUrl = new URL("file://");
- } catch(MalformedURLException e) {}
- }
-
- /**
- * Costructs a StreamImageSource using the specified stream that will deliver
- * an image of the specified type. A security check is performed against
- * the base URL.
- * @param is an input stream connected to the data source for the image
- * @param type the type of image (one of GIF or JPEG)
- */
- public StreamImageSource(InputStream is, int type) {
- this(baseUrl , is, type);
- }
-
- /**
- * Costructs a StreamImageSource using the specified stream that will deliver
- * an image of the specified type. A security check is performed against
- * the URL reference passed to the constructor.
- * @param base the URL from which the input stream is gathering data
- * @param is the input stream connected to the data source for the image
- * @param type the type of image (one of GIF or JPEG)
- */
- public StreamImageSource(URL base, InputStream is, int type) {
- super(base);
-
- if (type != GIF && type != JPEG) {
- throw new IllegalArgumentException("Unsupported image type:" + type);
- }
-
- stream = is;
- this.type = type;
- }
-
- /**
- * Sets the default URL that security checks are performed against.
- * This URL is used when an instance of StreamImageSource is created without
- * specifying a base URL.
- * @param base the URL for security checks
- */
- public static void setBaseUrl(URL base) { baseUrl = base; }
-
- /**
- * Gets the default URL that security checks are performed against.
- * This URL is used when an instance of StreamImageSource is created without
- * specifying a base URL.
- * @return the URL for security checks
- */
- public static URL getBaseUrl() { return baseUrl; }
-
- /**
- * Creates the proper decoder based on the type of image specified
- * in the constructor.
- */
- protected sun.awt.image.ImageDecoder getDecoder() {
- InputStream is;
- is = new BufferedInputStream(stream);
-
- switch (type) {
- case GIF:
- return new sun.awt.image.GifImageDecoder(this, is);
- case JPEG:
- return new sun.awt.image.JPEGImageDecoder(this, is);
- default:
- throw new IllegalArgumentException("Unsupported image type:" + type);
- }
- }
- }
-