home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 4.5 KB | 133 lines |
- 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 support 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 the
- * machine the input stream is pulling the image data. 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:
- * <pre>
- 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 is = 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);
- }
- }</pre>
- */
- public class StreamImageSource extends sun.awt.image.URLImageSource
- implements java.awt.image.ImageProducer
- {
- InputStream stream;
- int type;
- URL base;
-
- static URL baseUrl;
-
- /**
- * Constant for GIF format
- */
- public static final int GIF = 0;
- /**
- * Constant for 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) {}
- }
-
- /**
- * Create StreamImageSource using the specified stream that will deliver
- * an image of the specified type. Security check is performed against
- * the base URL.
- * @param is 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);
- }
-
- /**
- * Create StreamImageSource using the specified stream that will deliver
- * an image of the specified type. Security check is performed against
- * the URL reference passed to the constructor.
- * @param base url from which the input stream is gathering data
- * @param is 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.
- */
- public static void setBaseUrl(URL base) { baseUrl = base; }
-
- /**
- * Gets the URL for which secrity checks are performed when an
- * instance of StreamImageSource is created without specifying
- * a base URL.
- */
- 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);
- }
- }
- }
-