1 /*
2  * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.awt.image;
27 
28 import java.util.Hashtable;
29 import java.io.InputStream;
30 import java.io.IOException;
31 import java.awt.image.*;
32 
33 public abstract class ImageDecoder {
34     InputStreamImageSource source;
35     InputStream input;
36     Thread feeder;
37 
38     protected boolean aborted;
39     protected boolean finished;
40     ImageConsumerQueue queue;
41     ImageDecoder next;
42 
ImageDecoder(InputStreamImageSource src, InputStream is)43     public ImageDecoder(InputStreamImageSource src, InputStream is) {
44         source = src;
45         input = is;
46         feeder = Thread.currentThread();
47     }
48 
isConsumer(ImageConsumer ic)49     public boolean isConsumer(ImageConsumer ic) {
50         return ImageConsumerQueue.isConsumer(queue, ic);
51     }
52 
removeConsumer(ImageConsumer ic)53     public void removeConsumer(ImageConsumer ic) {
54         queue = ImageConsumerQueue.removeConsumer(queue, ic, false);
55         if (!finished && queue == null) {
56             abort();
57         }
58     }
59 
nextConsumer(ImageConsumerQueue cq)60     protected ImageConsumerQueue nextConsumer(ImageConsumerQueue cq) {
61         synchronized (source) {
62             if (aborted) {
63                 return null;
64             }
65             cq = ((cq == null) ? queue : cq.next);
66             while (cq != null) {
67                 if (cq.interested) {
68                     return cq;
69                 }
70                 cq = cq.next;
71             }
72         }
73         return null;
74     }
75 
setDimensions(int w, int h)76     protected int setDimensions(int w, int h) {
77         ImageConsumerQueue cq = null;
78         int count = 0;
79         while ((cq = nextConsumer(cq)) != null) {
80             cq.consumer.setDimensions(w, h);
81             count++;
82         }
83         return count;
84     }
85 
setProperties(Hashtable<?,?> props)86     protected int setProperties(Hashtable<?,?> props) {
87         ImageConsumerQueue cq = null;
88         int count = 0;
89         while ((cq = nextConsumer(cq)) != null) {
90             cq.consumer.setProperties(props);
91             count++;
92         }
93         return count;
94     }
95 
setColorModel(ColorModel model)96     protected int setColorModel(ColorModel model) {
97         ImageConsumerQueue cq = null;
98         int count = 0;
99         while ((cq = nextConsumer(cq)) != null) {
100             cq.consumer.setColorModel(model);
101             count++;
102         }
103         return count;
104     }
105 
setHints(int hints)106     protected int setHints(int hints) {
107         ImageConsumerQueue cq = null;
108         int count = 0;
109         while ((cq = nextConsumer(cq)) != null) {
110             cq.consumer.setHints(hints);
111             count++;
112         }
113         return count;
114     }
115 
headerComplete()116     protected void headerComplete() {
117         feeder.setPriority(ImageFetcher.LOW_PRIORITY);
118     }
119 
setPixels(int x, int y, int w, int h, ColorModel model, byte[] pix, int off, int scansize)120     protected int setPixels(int x, int y, int w, int h, ColorModel model,
121                             byte[] pix, int off, int scansize) {
122         source.latchConsumers(this);
123         ImageConsumerQueue cq = null;
124         int count = 0;
125         while ((cq = nextConsumer(cq)) != null) {
126             cq.consumer.setPixels(x, y, w, h, model, pix, off, scansize);
127             count++;
128         }
129         return count;
130     }
131 
setPixels(int x, int y, int w, int h, ColorModel model, int[] pix, int off, int scansize)132     protected int setPixels(int x, int y, int w, int h, ColorModel model,
133                             int[] pix, int off, int scansize) {
134         source.latchConsumers(this);
135         ImageConsumerQueue cq = null;
136         int count = 0;
137         while ((cq = nextConsumer(cq)) != null) {
138             cq.consumer.setPixels(x, y, w, h, model, pix, off, scansize);
139             count++;
140         }
141         return count;
142     }
143 
imageComplete(int status, boolean done)144     protected int imageComplete(int status, boolean done) {
145         source.latchConsumers(this);
146         if (done) {
147             finished = true;
148             source.doneDecoding(this);
149         }
150         ImageConsumerQueue cq = null;
151         int count = 0;
152         while ((cq = nextConsumer(cq)) != null) {
153             cq.consumer.imageComplete(status);
154             count++;
155         }
156         return count;
157     }
158 
produceImage()159     public abstract void produceImage() throws IOException,
160                                                ImageFormatException;
161 
abort()162     public void abort() {
163         aborted = true;
164         source.doneDecoding(this);
165         close();
166         java.security.AccessController.doPrivileged(
167             new java.security.PrivilegedAction<Object>() {
168             public Object run() {
169                 feeder.interrupt();
170                 return null;
171             }
172         });
173     }
174 
close()175     public synchronized void close() {
176         if (input != null) {
177             try {
178                 input.close();
179             } catch (IOException e) {
180             }
181         }
182     }
183 }
184