1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
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:
65:
66:
67:
68: }
69:
70: public ImageDecoder (String filename)
71: {
72: this.filename = filename;
73: }
74:
75: public ImageDecoder (URL url)
76: {
77: this.url = url;
78: }
79:
80: public ImageDecoder (InputStream is)
81: {
82: this.input = is;
83: }
84:
85: public ImageDecoder (DataInput datainput)
86: {
87: this.datainput = datainput;
88: }
89:
90: public ImageDecoder (byte[] imagedata, int imageoffset, int imagelength)
91: {
92: data = imagedata;
93: offset = imageoffset;
94: length = imagelength;
95: }
96:
97: public void addConsumer (ImageConsumer ic)
98: {
99: consumers.addElement (ic);
100: }
101:
102: public boolean isConsumer (ImageConsumer ic)
103: {
104: return consumers.contains (ic);
105: }
106:
107: public void removeConsumer (ImageConsumer ic)
108: {
109: consumers.removeElement (ic);
110: }
111:
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:
121:
122:
123:
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:
161: public void requestTopDownLeftRightResend (ImageConsumer ic)
162: {
163: }
164:
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:
171: DataInputStreamWrapper(DataInput datainput)
172: {
173: this.datainput = datainput;
174: }
175:
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: }