FileDocCategorySizeDatePackage
DecodingImageSource.javaAPI DocAndroid 1.5 API7809Wed May 06 22:41:54 BST 2009org.apache.harmony.awt.gl.image

DecodingImageSource

public abstract class DecodingImageSource extends Object implements ImageProducer
This is an abstract class that encapsulates a main part of ImageProducer functionality for the images being decoded by the native decoders, like PNG, JPEG and GIF. It helps to integrate image decoders into producer/consumer model. It provides functionality for working with several decoder instances and several image consumers simultaneously.

Fields Summary
List
consumers
List
decoders
boolean
loading
ImageDecoder
decoder
Constructors Summary
Methods Summary
private voidabortAllConsumers(java.util.List consumersList)
This method stops sending data to the list of consumers.

param
consumersList - list of consumers

        for (ImageConsumer imageConsumer : consumersList) {
            abortConsumer(imageConsumer);
        }
    
private voidabortConsumer(java.awt.image.ImageConsumer ic)
This method stops sending data to the given consumer

param
ic - consumer

        ic.imageComplete(ImageConsumer.IMAGEERROR);
        consumers.remove(ic);
    
public synchronized voidaddConsumer(java.awt.image.ImageConsumer ic)


       

       

         
        if (!checkConnection()) { // No permission for this consumer
            ic.imageComplete(ImageConsumer.IMAGEERROR);
            return;
        }

        ImageConsumer cons = findConsumer(consumers, ic);

        if (cons == null) { // Try to look in the decoders
            ImageDecoder d = null;

            // Check for all existing decoders
            for (Iterator<ImageDecoder> i = decoders.iterator(); i.hasNext();) {
                d = i.next();
                cons = findConsumer(d.consumers, ic);
                if (cons != null) {
                    break;
                }
            }
        }

        if (cons == null) { // Not found, add this consumer
            consumers.add(ic);
        }
    
protected abstract booleancheckConnection()

private ImageDecodercreateDecoder()
Tries to find an appropriate decoder for the input stream and adds it to the list of decoders

return
created decoder

        InputStream is = getInputStream();

        ImageDecoder decoder;

        if (is == null) {
            decoder = null;
        } else {
            decoder = ImageDecoder.createDecoder(this, is);
        }

        if (decoder != null) {
            synchronized (this) {
                decoders.add(decoder);
                this.decoder = decoder;
                loading = false;
                consumers = new ArrayList<ImageConsumer>(5); // Reset queue
            }

            return decoder;
        }
        // We were not able to find appropriate decoder
        List<ImageConsumer> cs;
        synchronized (this) {
            cs = consumers;
            consumers = new ArrayList<ImageConsumer>(5);
            loading = false;
        }
        abortAllConsumers(cs);

        return null;
    
private static java.awt.image.ImageConsumerfindConsumer(java.util.List consumersList, java.awt.image.ImageConsumer ic)
Checks if the consumer is in the list and returns it it is there

param
consumersList - list of consumers
param
ic - consumer
return
consumer if found, null otherwise

        ImageConsumer res = null;

        for (Iterator<ImageConsumer> i = consumersList.iterator(); i.hasNext();) {
            res = i.next();
            if (res.equals(ic)) {
                return res;
            }
        }

        return null;
    
protected abstract java.io.InputStreamgetInputStream()

public synchronized booleanisConsumer(java.awt.image.ImageConsumer ic)

        ImageDecoder d = null;

        // Check for all existing decoders
        for (Iterator<ImageDecoder> i = decoders.iterator(); i.hasNext();) {
            d = i.next();
            if (findConsumer(d.consumers, ic) != null) {
                return true;
            }
        }

        // Check current queue of consumers
        return findConsumer(consumers, ic) != null;
    
public voidload()
This method serves as an entry point. It starts the decoder and loads the image data.

        synchronized (this) {
            if (consumers.size() == 0) {
                loading = false;
                return;
            }
        }

        ImageDecoder d = createDecoder();
        if (d != null) {
            try {
                decoder.decodeImage();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                removeDecoder(d);
                abortAllConsumers(d.consumers);
            }
        }
    
synchronized voidlockDecoder(ImageDecoder d)
Use this method to finish decoding or lock the list of consumers for a particular decoder

param
d - decoder

        if (d == decoder) {
            decoder = null;
            startProduction(null);
        }
    
public synchronized voidremoveConsumer(java.awt.image.ImageConsumer ic)

        ImageDecoder d = null;

        // Remove in all existing decoders
        for (Iterator<ImageDecoder> i = decoders.iterator(); i.hasNext();) {
            d = i.next();
            removeConsumer(d.consumers, ic);
            if (d.consumers.size() <= 0) {
                d.terminate();
            }
        }

        // Remove in the current queue of consumers
        removeConsumer(consumers, ic);
    
private static voidremoveConsumer(java.util.List consumersList, java.awt.image.ImageConsumer ic)
Static implementation of removeConsumer method

param
consumersList - list of consumers
param
ic - consumer to be removed

        ImageConsumer cons = null;

        for (Iterator<ImageConsumer> i = consumersList.iterator(); i.hasNext();) {
            cons = i.next();
            if (cons.equals(ic)) {
                i.remove();
            }
        }
    
private synchronized voidremoveDecoder(ImageDecoder dr)
Stop the given decoder and remove it from the list

param
dr - decoder

        lockDecoder(dr);
        decoders.remove(dr);
    
public voidrequestTopDownLeftRightResend(java.awt.image.ImageConsumer consumer)

        // Do nothing
    
public synchronized voidstartProduction(java.awt.image.ImageConsumer ic)

        if (ic != null) {
            addConsumer(ic);
        }

        if (!loading && consumers.size() > 0) {
            ImageLoader.addImageSource(this);
            loading = true;
        }