home *** CD-ROM | disk | FTP | other *** search
- package sun.awt.image;
-
- import java.awt.image.ColorModel;
- import java.awt.image.ImageConsumer;
- import java.awt.image.ImageProducer;
- import java.io.BufferedInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Hashtable;
-
- abstract class InputStreamImageSource implements ImageProducer, ImageFetchable {
- PixelStore pixelstore;
- private ConsumerQueue consumers;
- private boolean awaitingFetch = false;
- private Thread latestFetcher;
- private ImageDecoder decoder;
-
- abstract boolean checkSecurity(Object var1, boolean var2);
-
- synchronized int countConsumers() {
- ConsumerQueue cq = this.consumers;
-
- int i;
- for(i = 0; cq != null; cq = cq.next) {
- ++i;
- }
-
- return i;
- }
-
- public void addConsumer(ImageConsumer ic) {
- this.addConsumer(ic, false);
- }
-
- synchronized void addConsumer(ImageConsumer ic, boolean produce) {
- this.checkSecurity((Object)null, false);
-
- ConsumerQueue cq;
- for(cq = this.consumers; cq != null && cq.consumer != ic; cq = cq.next) {
- }
-
- if (cq == null) {
- cq = new ConsumerQueue(this, ic);
- cq.next = this.consumers;
- this.consumers = cq;
- } else {
- if (!cq.secure) {
- Object context = System.getSecurityManager().getSecurityContext();
- if (cq.securityContext == null) {
- cq.securityContext = context;
- } else if (cq.securityContext != context) {
- this.errorConsumer(cq);
- throw new SecurityException("Applets are trading image data!");
- }
- }
-
- cq.interested = true;
- }
-
- if (produce && cq.feeder == null && !this.awaitingFetch && this.latestFetcher == null) {
- this.startProduction();
- }
-
- }
-
- public synchronized boolean isConsumer(ImageConsumer ic) {
- for(ConsumerQueue cq = this.consumers; cq != null; cq = cq.next) {
- if (cq.consumer == ic) {
- return true;
- }
- }
-
- return false;
- }
-
- synchronized void errorConsumer(ConsumerQueue cq) {
- cq.consumer.imageComplete(1);
- this.removeConsumer(cq.consumer);
- }
-
- public synchronized void removeConsumer(ImageConsumer ic) {
- ConsumerQueue cq = this.consumers;
-
- for(ConsumerQueue cqprev = null; cq != null; cq = cq.next) {
- if (cq.consumer == ic) {
- if (cqprev == null) {
- this.consumers = cq.next;
- } else {
- cqprev.next = cq.next;
- }
-
- cq.interested = false;
- return;
- }
-
- cqprev = cq;
- }
-
- }
-
- public void startProduction(ImageConsumer ic) {
- this.addConsumer(ic, true);
- }
-
- private synchronized void startProduction() {
- if (!this.awaitingFetch) {
- ImageFetcher.add(this);
- this.awaitingFetch = true;
- }
-
- }
-
- public void requestTopDownLeftRightResend(ImageConsumer ic) {
- synchronized(this){}
-
- PixelStore store;
- try {
- store = this.pixelstore;
- } catch (Throwable var5) {
- throw var5;
- }
-
- if (store != null) {
- store.replay(this, ic, false);
- }
-
- }
-
- protected abstract ImageDecoder getDecoder();
-
- protected ImageDecoder decoderForType(InputStream is, String content_type) {
- if (content_type.equals("image/gif")) {
- return new GifImageDecoder(this, is);
- } else if (content_type.equals("image/jpeg")) {
- return new JPEGImageDecoder(this, is);
- } else {
- return content_type.equals("image/x-xbitmap") ? new XbmImageDecoder(this, is) : null;
- }
- }
-
- protected ImageDecoder getDecoder(InputStream is) {
- if (!is.markSupported()) {
- is = new BufferedInputStream(is);
- }
-
- try {
- is.mark(1000);
- int c1 = is.read();
- int c2 = is.read();
- int c3 = is.read();
- int c4 = is.read();
- is.read();
- is.read();
- is.reset();
- if (c1 == 71 && c2 == 73 && c3 == 70 && c4 == 56) {
- return new GifImageDecoder(this, is);
- }
-
- if (c1 == 255 && c2 == 216 && c3 == 255 && c4 == 224 || c1 == 255 && c2 == 216 && c3 == 255 && c4 == 238) {
- return new JPEGImageDecoder(this, is);
- }
-
- if (c1 == 35 && c2 == 100 && c3 == 101 && c4 == 102) {
- return new XbmImageDecoder(this, is);
- }
- } catch (IOException var6) {
- }
-
- return null;
- }
-
- public void doFetch() {
- Thread me = Thread.currentThread();
- ImageDecoder imgd = null;
-
- try {
- if (this.latchConsumers(me) <= 0) {
- return;
- }
-
- if (!this.updateFromStore(me)) {
- imgd = this.getDecoder();
- if (imgd != null) {
- this.setDecoder(imgd, me);
-
- try {
- imgd.produceImage();
- } catch (IOException e) {
- ((Throwable)e).printStackTrace();
- } catch (ImageFormatException e) {
- ((Throwable)e).printStackTrace();
- }
-
- return;
- }
-
- return;
- }
- } finally {
- this.imageComplete(1);
- this.unlatchConsumers(imgd, me);
- }
-
- }
-
- private boolean updateFromStore(Thread me) {
- synchronized(this){}
-
- PixelStore store;
- ConsumerQueue cq;
- try {
- store = this.pixelstore;
- if (store == null) {
- boolean var6 = false;
- return var6;
- }
-
- cq = this.consumers;
- } catch (Throwable var18) {
- throw var18;
- }
-
- while(cq != null) {
- synchronized(this){}
-
- try {
- if (cq.feeder == null) {
- if (!this.checkSecurity(cq.securityContext, true)) {
- this.errorConsumer(cq);
- } else {
- cq.feeder = me;
- }
- }
-
- if (cq.feeder != me) {
- cq = cq.next;
- continue;
- }
- } catch (Throwable var17) {
- throw var17;
- }
-
- if (!store.replay(this, cq.consumer)) {
- return false;
- }
-
- synchronized(this){}
-
- try {
- cq = cq.next;
- } catch (Throwable var16) {
- throw var16;
- }
- }
-
- return true;
- }
-
- private synchronized void setDecoder(ImageDecoder decoder, Thread me) {
- for(ConsumerQueue cq = this.consumers; cq != null; cq = cq.next) {
- if (cq.feeder == me) {
- if (!this.checkSecurity(cq.securityContext, true)) {
- this.errorConsumer(cq);
- } else {
- cq.decoder = decoder;
- }
- }
- }
-
- if (this.latestFetcher == me) {
- this.decoder = decoder;
- }
-
- }
-
- private synchronized int latchConsumers(Thread me) {
- this.latestFetcher = me;
- this.awaitingFetch = false;
- ConsumerQueue cq = this.consumers;
-
- int count;
- for(count = 0; cq != null; cq = cq.next) {
- if (cq.feeder == null) {
- if (!this.checkSecurity(cq.securityContext, true)) {
- this.errorConsumer(cq);
- } else {
- cq.feeder = me;
- ++count;
- }
- }
- }
-
- return count;
- }
-
- private synchronized void unlatchConsumers(ImageDecoder imgd, Thread me) {
- if (this.latestFetcher == me) {
- this.latestFetcher = null;
- }
-
- ConsumerQueue cq = this.consumers;
-
- for(ConsumerQueue cqprev = null; cq != null; cq = cq.next) {
- if (cq.feeder == me) {
- if (cqprev == null) {
- this.consumers = cq.next;
- } else {
- cqprev.next = cq.next;
- }
- } else {
- cqprev = cq;
- }
- }
-
- }
-
- private ConsumerQueue nextConsumer(ConsumerQueue cq, Thread me) {
- synchronized(this){}
-
- try {
- if (cq == null) {
- cq = this.consumers;
- } else {
- cq = cq.next;
- }
-
- while(cq != null) {
- if (cq.interested) {
- if (cq.feeder == me) {
- ConsumerQueue var5 = cq;
- return var5;
- }
-
- if (cq.feeder == null && this.latestFetcher == me) {
- break;
- }
-
- cq = cq.next;
- }
- }
- } catch (Throwable var17) {
- throw var17;
- }
-
- if (cq != null) {
- if (this.decoder != null && this.decoder.catchupConsumer(this, cq.consumer)) {
- synchronized(this){}
-
- ConsumerQueue var19;
- try {
- if (!cq.interested) {
- return this.nextConsumer(cq, me);
- }
-
- if (!this.checkSecurity(cq.securityContext, true)) {
- this.errorConsumer(cq);
- return this.nextConsumer(cq, me);
- }
-
- cq.feeder = me;
- cq.decoder = this.decoder;
- var19 = cq;
- } catch (Throwable var16) {
- throw var16;
- }
-
- return var19;
- }
-
- synchronized(this){}
-
- try {
- this.latestFetcher = null;
- this.startProduction();
- } catch (Throwable var15) {
- throw var15;
- }
- }
-
- return cq;
- }
-
- synchronized void flush() {
- ConsumerQueue cq = this.consumers;
-
- for(this.consumers = null; cq != null; cq = cq.next) {
- cq.interested = false;
-
- try {
- if (cq.feeder != null) {
- cq.feeder.interrupt();
- }
- } catch (NoSuchMethodError var2) {
- break;
- }
-
- if (cq.decoder != null) {
- cq.decoder.close();
- }
- }
-
- this.pixelstore = null;
- }
-
- synchronized void setPixelStore(PixelStore storage) {
- this.pixelstore = storage;
- }
-
- int setDimensions(int w, int h) {
- ConsumerQueue cq = null;
- Thread me = Thread.currentThread();
-
- int count;
- for(count = 0; (cq = this.nextConsumer(cq, me)) != null; ++count) {
- cq.consumer.setDimensions(w, h);
- }
-
- return count;
- }
-
- int setProperties(Hashtable props) {
- ConsumerQueue cq = null;
- Thread me = Thread.currentThread();
-
- int count;
- for(count = 0; (cq = this.nextConsumer(cq, me)) != null; ++count) {
- cq.consumer.setProperties(props);
- }
-
- return count;
- }
-
- int setColorModel(ColorModel model) {
- ConsumerQueue cq = null;
- Thread me = Thread.currentThread();
-
- int count;
- for(count = 0; (cq = this.nextConsumer(cq, me)) != null; ++count) {
- cq.consumer.setColorModel(model);
- }
-
- return count;
- }
-
- int setHints(int hints) {
- ConsumerQueue cq = null;
- Thread me = Thread.currentThread();
-
- int count;
- for(count = 0; (cq = this.nextConsumer(cq, me)) != null; ++count) {
- cq.consumer.setHints(hints);
- }
-
- return count;
- }
-
- int setPixels(int x, int y, int w, int h, ColorModel model, byte[] pix, int off, int scansize) {
- ConsumerQueue cq = null;
- Thread me = Thread.currentThread();
- if (me.getPriority() > 3) {
- SecurityManager.setScopePermission();
- me.setPriority(3);
- SecurityManager.resetScopePermission();
- }
-
- int count;
- for(count = 0; (cq = this.nextConsumer(cq, me)) != null; ++count) {
- cq.consumer.setPixels(x, y, w, h, model, pix, off, scansize);
- }
-
- return count;
- }
-
- int setPixels(int x, int y, int w, int h, ColorModel model, int[] pix, int off, int scansize) {
- ConsumerQueue cq = null;
- Thread me = Thread.currentThread();
- if (me.getPriority() > 3) {
- SecurityManager.setScopePermission();
- me.setPriority(3);
- SecurityManager.resetScopePermission();
- }
-
- int count;
- for(count = 0; (cq = this.nextConsumer(cq, me)) != null; ++count) {
- cq.consumer.setPixels(x, y, w, h, model, pix, off, scansize);
- }
-
- return count;
- }
-
- int imageComplete(int status) {
- ConsumerQueue cq = null;
- Thread me = Thread.currentThread();
-
- int count;
- for(count = 0; (cq = this.nextConsumer(cq, me)) != null; ++count) {
- cq.consumer.imageComplete(status);
- }
-
- return count;
- }
- }
-