1 /* ImageDecoder.java --
2    Copyright (C) 1999, 2000, 2004  Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 package gnu.java.awt.image;
39 
40 import java.awt.image.ImageConsumer;
41 import java.awt.image.ImageProducer;
42 import java.io.ByteArrayInputStream;
43 import java.io.DataInput;
44 import java.io.EOFException;
45 import java.io.FileInputStream;
46 import java.io.IOException;
47 import java.io.InputStream;
48 import java.net.URL;
49 import java.util.Vector;
50 
51 public abstract class ImageDecoder implements ImageProducer
52 {
53   Vector consumers = new Vector ();
54   String filename;
55   URL url;
56   byte[] data;
57   int offset;
58   int length;
59   InputStream input;
60   DataInput datainput;
61 
62   static
63   {
64     // FIXME: there was some broken code here that looked like
65     // it wanted to rely on this property.  I don't have any idea
66     // what it was intended to do.
67     // String endian = System.getProperties ().getProperty ("gnu.cpu.endian");
68   }
69 
ImageDecoder(String filename)70   public ImageDecoder (String filename)
71   {
72     this.filename = filename;
73   }
74 
ImageDecoder(URL url)75   public ImageDecoder (URL url)
76   {
77     this.url = url;
78   }
79 
ImageDecoder(InputStream is)80   public ImageDecoder (InputStream is)
81   {
82     this.input = is;
83   }
84 
ImageDecoder(DataInput datainput)85   public ImageDecoder (DataInput datainput)
86   {
87     this.datainput = datainput;
88   }
89 
ImageDecoder(byte[] imagedata, int imageoffset, int imagelength)90   public ImageDecoder (byte[] imagedata, int imageoffset, int imagelength)
91   {
92     data = imagedata;
93     offset = imageoffset;
94     length = imagelength;
95   }
96 
addConsumer(ImageConsumer ic)97   public void addConsumer (ImageConsumer ic)
98   {
99     consumers.addElement (ic);
100   }
101 
isConsumer(ImageConsumer ic)102   public boolean isConsumer (ImageConsumer ic)
103   {
104     return consumers.contains (ic);
105   }
106 
removeConsumer(ImageConsumer ic)107   public void removeConsumer (ImageConsumer ic)
108   {
109     consumers.removeElement (ic);
110   }
111 
startProduction(ImageConsumer ic)112   public void startProduction (ImageConsumer ic)
113   {
114     if (!isConsumer(ic))
115       addConsumer(ic);
116 
117     Vector list = (Vector) consumers.clone ();
118     try
119       {
120         // Create the input stream here rather than in the
121         // ImageDecoder constructors so that exceptions cause
122         // imageComplete to be called with an appropriate error
123         // status.
124         if (input == null)
125           {
126             try
127               {
128                 if (url != null)
129                   input = url.openStream();
130                 else if (datainput != null)
131                   input = new DataInputStreamWrapper(datainput);
132                 else
133                   {
134                     if (filename != null)
135                       input = new FileInputStream (filename);
136                     else
137                       input = new ByteArrayInputStream (data, offset, length);
138                   }
139                 produce (list, input);
140               }
141             finally
142               {
143                 input = null;
144               }
145           }
146         else
147           {
148             produce (list, input);
149           }
150       }
151     catch (Exception e)
152       {
153         for (int i = 0; i < list.size (); i++)
154           {
155             ImageConsumer ic2 = (ImageConsumer) list.elementAt (i);
156             ic2.imageComplete (ImageConsumer.IMAGEERROR);
157           }
158       }
159   }
160 
requestTopDownLeftRightResend(ImageConsumer ic)161   public void requestTopDownLeftRightResend (ImageConsumer ic)
162   {
163   }
164 
produce(Vector v, InputStream is)165   public abstract void produce (Vector v, InputStream is) throws IOException;
166 
167   private static class DataInputStreamWrapper extends InputStream
168   {
169     private final DataInput datainput;
170 
DataInputStreamWrapper(DataInput datainput)171     DataInputStreamWrapper(DataInput datainput)
172     {
173       this.datainput = datainput;
174     }
175 
read()176     public int read() throws IOException
177     {
178       try
179         {
180           return datainput.readByte() & 0xFF;
181         }
182       catch (EOFException eofe)
183         {
184           return -1;
185         }
186     }
187   }
188 }
189