FileDocCategorySizeDatePackage
MarkerSegment.javaAPI DocJava SE 5 API6867Fri Aug 26 14:54:42 BST 2005com.sun.imageio.plugins.jpeg

MarkerSegment

public class MarkerSegment extends Object implements Cloneable
All metadata is stored in MarkerSegments. Marker segments that we know about are stored in subclasses of this basic class, which used for unrecognized APPn marker segments. XXX break out UnknownMarkerSegment as a subclass and make this abstract, avoiding unused data field.

Fields Summary
protected static final int
LENGTH_SIZE
int
tag
int
length
byte[]
data
boolean
unknown
Constructors Summary
MarkerSegment(JPEGBuffer buffer)
Constructor for creating MarkerSegments by reading from an ImageInputStream.

 // Set to true if the tag is not recognized

                  
        

        buffer.loadBuf(3);  // tag plus length
        tag = buffer.buf[buffer.bufPtr++] & 0xff;
        length = (buffer.buf[buffer.bufPtr++] & 0xff) << 8;
        length |= buffer.buf[buffer.bufPtr++] & 0xff;
        length -= 2;  // JPEG length includes itself, we don't
        buffer.bufAvail -= 3;
        // Now that we know the true length, ensure that we've got it,
        // or at least a bufferful if length is too big.
        buffer.loadBuf(length);
    
MarkerSegment(int tag)
Constructor used when creating segments other than by reading them from a stream.

        this.tag = tag;
        length = 0;
    
MarkerSegment(Node node)
Construct a MarkerSegment from an "unknown" DOM Node.

        // The type of node should have been verified already.
        // get the attribute and assign it to the tag
        tag = getAttributeValue(node, 
                                null, 
                                "MarkerTag", 
                                0, 255, 
                                true);
        length = 0;
        // get the user object and clone it to the data
        if (node instanceof IIOMetadataNode) {
            IIOMetadataNode iioNode = (IIOMetadataNode) node;
            try {
                data = (byte []) iioNode.getUserObject();
            } catch (Exception e) {
                IIOInvalidTreeException newGuy = 
                    new IIOInvalidTreeException
                    ("Can't get User Object", node);
                newGuy.initCause(e);
                throw newGuy;
            }
        } else {
            throw new IIOInvalidTreeException
                ("Node must have User Object", node);
        }
    
Methods Summary
protected java.lang.Objectclone()
Deep copy of data array.

        MarkerSegment newGuy = null;
        try {
            newGuy = (MarkerSegment) super.clone();
        } catch (CloneNotSupportedException e) {} // won't happen
        if (this.data != null) {
            newGuy.data = (byte[]) data.clone();
        }
        return newGuy;
    
static intgetAttributeValue(org.w3c.dom.Node node, org.w3c.dom.NamedNodeMap attrs, java.lang.String name, int min, int max, boolean required)

        if (attrs == null) {
            attrs = node.getAttributes();
        }
        String valueString = attrs.getNamedItem(name).getNodeValue();
        int value = -1;
        if (valueString == null) {
            if (required) {
                throw new IIOInvalidTreeException
                    (name + " attribute not found", node);
            }
        } else {
              value = Integer.parseInt(valueString);
              if ((value < min) || (value > max)) {
                  throw new IIOInvalidTreeException
                      (name + " attribute out of range", node);
              }
        }
        return value;
    
javax.imageio.metadata.IIOMetadataNodegetNativeNode()

        IIOMetadataNode node = new IIOMetadataNode("unknown");
        node.setAttribute("MarkerTag", Integer.toString(tag));
        node.setUserObject(data);
        
        return node;
    
voidloadData(com.sun.imageio.plugins.jpeg.JPEGBuffer buffer)
We have determined that we don't know the type, so load the data using the length parameter.

        data = new byte[length];
        buffer.readData(data);
    
voidprint()

        printTag("Unknown");
        if (length > 10) {
            System.out.print("First 5 bytes:");
            for (int i=0;i<5;i++) {
                System.out.print(" Ox" 
                                 + Integer.toHexString((int)data[i]));
            }
            System.out.print("\nLast 5 bytes:");
            for (int i=data.length-5;i<data.length;i++) {
                System.out.print(" Ox" 
                                 + Integer.toHexString((int)data[i]));
            }
        } else {
            System.out.print("Data:");
            for (int i=0;i<data.length;i++) {
                System.out.print(" Ox" 
                                 + Integer.toHexString((int)data[i]));
            }
        }
        System.out.println();
    
voidprintTag(java.lang.String prefix)

        System.out.println(prefix + " marker segment - marker = 0x"
                           + Integer.toHexString(tag));
        System.out.println("length: " + length);
    
voidwrite(javax.imageio.stream.ImageOutputStream ios)
Writes the data for this segment to the stream in valid JPEG format.

        length = 2 + ((data != null) ? data.length : 0);
        writeTag(ios);
        if (data != null) {
            ios.write(data);
        }
    
static voidwrite2bytes(javax.imageio.stream.ImageOutputStream ios, int value)

        ios.write((value >> 8) & 0xff);
        ios.write(value & 0xff);
            
    
voidwriteTag(javax.imageio.stream.ImageOutputStream ios)
Writes the marker, tag, and length. Note that length should be verified by the caller as a correct JPEG length, i.e it includes itself.

        ios.write(0xff);
        ios.write(tag);
        write2bytes(ios, length);