Class Box

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected byte[] data  
      protected long extraLength  
      protected int length
      Box length, extra length, type and content data array
      protected int type  
    • Constructor Summary

      Constructors 
      Constructor Description
      Box​(int length, int type, byte[] data)
      Constructs a Box instance using the provided the box type and the box content in byte array format.
      Box​(int length, int type, long extraLength, byte[] data)
      Constructs a Box instance using the provided the box type, the box extra length, and the box content in byte array format.
      Box​(javax.imageio.stream.ImageInputStream iis, int pos)
      Constructs a Box instance from the provided ImageInputStream at the specified position.
      Box​(org.w3c.dom.Node node)
      Constructs a Box from an "unknown" Node.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void compose()
      Composes the content byte array from the data elements.
      static void copyInt​(byte[] data, int pos, int value)
      Copies that four bytes of an integer into the byte array.
      static Box createBox​(int type, org.w3c.dom.Node node)
      Creates a Box object with the provided type based on the provided Node object based on reflection.
      static java.lang.Object getAttribute​(org.w3c.dom.Node node, java.lang.String name)
      Extracts the value of the attribute from name.
      static java.lang.Class getBoxClass​(int type)
      Returns the Box class for the box with the provided type.
      protected static byte[] getByteArrayElementValue​(org.w3c.dom.Node node)
      Gets the byte array from an IIOMetadataNode.
      protected static byte getByteElementValue​(org.w3c.dom.Node node)
      Gets its byte value from an IIOMetadataNode.
      byte[] getContent()
      Returns the box content in byte array.
      long getExtraLength()
      Returns the box extra length.
      protected static int[] getIntArrayElementValue​(org.w3c.dom.Node node)
      Gets the integer array from an IIOMetadataNode.
      protected static int getIntElementValue​(org.w3c.dom.Node node)
      Gets its integer value from an IIOMetadataNode.
      int getLength()
      Returns the box length.
      static java.lang.String getName​(int type)
      Returns the XML tag name defined in JP2 XML xsd/dtd for the box with the provided type.
      javax.imageio.metadata.IIOMetadataNode getNativeNode()
      Creates an IIOMetadataNode from this box.
      protected javax.imageio.metadata.IIOMetadataNode getNativeNodeForSimpleBox()
      Creates an IIOMetadataNode from this box.
      protected static short getShortElementValue​(org.w3c.dom.Node node)
      Gets its short value from an IIOMetadataNode.
      protected static java.lang.String getStringElementValue​(org.w3c.dom.Node node)
      Gets its String value from an IIOMetadataNode.
      int getType()
      Returns the box type.
      static java.lang.String getTypeByName​(java.lang.String name)
      Returns the type String based on the provided name.
      static int getTypeInt​(java.lang.String s)
      Converts the box type from integer to string.
      static java.lang.String getTypeString​(int type)
      Converts the box type from integer to string.
      protected void parse​(byte[] data)
      Parses the data elements from the byte array.
      static byte[] parseByteArray​(java.lang.String value)
      Parses the byte array expressed by a string.
      protected static int[] parseIntArray​(java.lang.String value)
      Parses the integer array expressed a string.
      void read​(javax.imageio.stream.ImageInputStream iis, int pos)
      Reads a box from the ImageInputStream
      void setContent​(byte[] data)
      Sets the box content.
      protected void setDefaultAttributes​(javax.imageio.metadata.IIOMetadataNode node)
      Sets the default attributes, "Length", "Type", and "ExtraLength", to the provided IIOMetadataNode.
      void setExtraLength​(long extraLength)
      Sets the box extra length length to the provided value.
      void setLength​(int length)
      Sets the box length to the provided value.
      void write​(javax.imageio.stream.ImageOutputStream ios)
      Writes this box instance into a ImageOutputStream.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • length

        protected int length
        Box length, extra length, type and content data array
      • extraLength

        protected long extraLength
      • type

        protected int type
      • data

        protected byte[] data
    • Constructor Detail

      • Box

        public Box​(int length,
                   int type,
                   byte[] data)
        Constructs a Box instance using the provided the box type and the box content in byte array format.
        Parameters:
        length - The provided box length.
        type - The provided box type.
        data - The provided box content in a byte array.
        Throws:
        java.lang.IllegalArgumentException - If the length of the content byte array is not length - 8.
      • Box

        public Box​(int length,
                   int type,
                   long extraLength,
                   byte[] data)
        Constructs a Box instance using the provided the box type, the box extra length, and the box content in byte array format. In this case, the length of the box is set to 1, which indicates the extra length is meaningful.
        Parameters:
        length - The provided box length.
        type - The provided box type.
        extraLength - The provided box extra length.
        data - The provided box content in a byte array.
        Throws:
        java.lang.IllegalArgumentException - If the length of the content byte array is not extra length - 16.
      • Box

        public Box​(javax.imageio.stream.ImageInputStream iis,
                   int pos)
            throws java.io.IOException
        Constructs a Box instance from the provided ImageInputStream at the specified position.
        Parameters:
        iis - The ImageInputStream contains the box.
        pos - The position from where to read the box.
        Throws:
        java.io.IOException - If any IOException is thrown in the called read methods.
      • Box

        public Box​(org.w3c.dom.Node node)
            throws javax.imageio.metadata.IIOInvalidTreeException
        Constructs a Box from an "unknown" Node. This node has at least the attribute "Type", and may have the attribute "Length", "ExtraLength" and a child "Content". The child node content is a IIOMetaDataNode with a byte[] user object.
        Throws:
        javax.imageio.metadata.IIOInvalidTreeException
    • Method Detail

      • getName

        public static java.lang.String getName​(int type)
        Returns the XML tag name defined in JP2 XML xsd/dtd for the box with the provided type. If the type is not known, the string "unknown" is returned.
      • getBoxClass

        public static java.lang.Class getBoxClass​(int type)
        Returns the Box class for the box with the provided type.
      • getTypeByName

        public static java.lang.String getTypeByName​(java.lang.String name)
        Returns the type String based on the provided name.
      • createBox

        public static Box createBox​(int type,
                                    org.w3c.dom.Node node)
                             throws javax.imageio.metadata.IIOInvalidTreeException
        Creates a Box object with the provided type based on the provided Node object based on reflection.
        Throws:
        javax.imageio.metadata.IIOInvalidTreeException
      • getAttribute

        public static java.lang.Object getAttribute​(org.w3c.dom.Node node,
                                                    java.lang.String name)
        Extracts the value of the attribute from name.
      • parseByteArray

        public static byte[] parseByteArray​(java.lang.String value)
        Parses the byte array expressed by a string.
      • parseIntArray

        protected static int[] parseIntArray​(java.lang.String value)
        Parses the integer array expressed a string.
      • getStringElementValue

        protected static java.lang.String getStringElementValue​(org.w3c.dom.Node node)
        Gets its String value from an IIOMetadataNode.
      • getByteElementValue

        protected static byte getByteElementValue​(org.w3c.dom.Node node)
        Gets its byte value from an IIOMetadataNode.
      • getIntElementValue

        protected static int getIntElementValue​(org.w3c.dom.Node node)
        Gets its integer value from an IIOMetadataNode.
      • getShortElementValue

        protected static short getShortElementValue​(org.w3c.dom.Node node)
        Gets its short value from an IIOMetadataNode.
      • getByteArrayElementValue

        protected static byte[] getByteArrayElementValue​(org.w3c.dom.Node node)
        Gets the byte array from an IIOMetadataNode.
      • getIntArrayElementValue

        protected static int[] getIntArrayElementValue​(org.w3c.dom.Node node)
        Gets the integer array from an IIOMetadataNode.
      • copyInt

        public static void copyInt​(byte[] data,
                                   int pos,
                                   int value)
        Copies that four bytes of an integer into the byte array. Necessary for the subclasses to compose the content array from the data elements
      • getTypeString

        public static java.lang.String getTypeString​(int type)
        Converts the box type from integer to string. This is necessary because type is defined as String in xsd/dtd and integer in the box classes.
      • getTypeInt

        public static int getTypeInt​(java.lang.String s)
        Converts the box type from integer to string. This is necessary because type is defined as String in xsd/dtd and integer in the box classes.
      • getNativeNode

        public javax.imageio.metadata.IIOMetadataNode getNativeNode()
        Creates an IIOMetadataNode from this box. The format of this node is defined in the XML dtd and xsd for the JP2 image file.
      • getNativeNodeForSimpleBox

        protected javax.imageio.metadata.IIOMetadataNode getNativeNodeForSimpleBox()
        Creates an IIOMetadataNode from this box. The format of this node is defined in the XML dtd and xsd for the JP2 image file. This method is designed for the types of boxes whose XML tree only has 2 levels.
      • setDefaultAttributes

        protected void setDefaultAttributes​(javax.imageio.metadata.IIOMetadataNode node)
        Sets the default attributes, "Length", "Type", and "ExtraLength", to the provided IIOMetadataNode.
      • getLength

        public int getLength()
        Returns the box length.
      • getType

        public int getType()
        Returns the box type.
      • getExtraLength

        public long getExtraLength()
        Returns the box extra length.
      • getContent

        public byte[] getContent()
        Returns the box content in byte array.
      • setLength

        public void setLength​(int length)
        Sets the box length to the provided value.
      • setExtraLength

        public void setExtraLength​(long extraLength)
        Sets the box extra length length to the provided value.
      • setContent

        public void setContent​(byte[] data)
        Sets the box content. If the content length is not length -8 or extra length - 16, IllegalArgumentException will be thrown.
      • write

        public void write​(javax.imageio.stream.ImageOutputStream ios)
                   throws java.io.IOException
        Writes this box instance into a ImageOutputStream.
        Throws:
        java.io.IOException
      • read

        public void read​(javax.imageio.stream.ImageInputStream iis,
                         int pos)
                  throws java.io.IOException
        Reads a box from the ImageInputStream
        Throws:
        java.io.IOException
      • parse

        protected void parse​(byte[] data)
        Parses the data elements from the byte array. The subclasses should override this method.
      • compose

        protected void compose()
        Composes the content byte array from the data elements.