Class Chunk

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Chunk.Type
      Types of chunks that can exist.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int chunkSize
      headerSize + dataSize.
      protected int headerSize
      Size of the chunk header in bytes.
      static int METADATA_SIZE
      The number of bytes in every chunk that describes chunk type, header size, and chunk size.
      protected int offset
      Offset of this chunk from the start of the file.
      static int PAD_BOUNDARY
      The byte boundary to pad chunks on.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Chunk​(java.nio.ByteBuffer buffer, Chunk parent)  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      int getHeaderSize()
      Returns the size of this chunk's header.
      int getOriginalChunkSize()
      Returns the size of this chunk when it was first read from a buffer.
      Chunk getParent()
      Returns the parent to this chunk, if any.
      protected abstract Chunk.Type getType()  
      protected void init​(java.nio.ByteBuffer buffer)
      Finishes initialization of a chunk.
      static Chunk newInstance​(java.nio.ByteBuffer buffer)
      Creates a new chunk whose contents start at buffer's current position.
      static Chunk newInstance​(java.nio.ByteBuffer buffer, Chunk parent)
      Creates a new chunk whose contents start at buffer's current position.
      byte[] toByteArray()
      Converts this resource into an array of bytes representation.
      byte[] toByteArray​(boolean shrink)
      Converts this chunk into an array of bytes representation.
      protected void writeHeader​(java.nio.ByteBuffer output)
      Writes the remaining header (after the type, headerSize, and chunkSize).
      protected void writeHeader​(java.nio.ByteBuffer output, int chunkSize)
      Writes the type and header size.
      protected int writePad​(java.io.DataOutput output, int currentLength)
      Pads output until currentLength is on a 4-byte boundary.
      protected void writePayload​(java.io.DataOutput output, java.nio.ByteBuffer header, boolean shrink)
      Writes the chunk payload.
      • Methods inherited from class java.lang.Object

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

      • PAD_BOUNDARY

        public static final int PAD_BOUNDARY
        The byte boundary to pad chunks on.
        See Also:
        Constant Field Values
      • METADATA_SIZE

        public static final int METADATA_SIZE
        The number of bytes in every chunk that describes chunk type, header size, and chunk size.
        See Also:
        Constant Field Values
      • headerSize

        protected final int headerSize
        Size of the chunk header in bytes.
      • chunkSize

        protected final int chunkSize
        headerSize + dataSize. The total size of this chunk.
      • offset

        protected final int offset
        Offset of this chunk from the start of the file.
    • Constructor Detail

      • Chunk

        protected Chunk​(java.nio.ByteBuffer buffer,
                        @Nullable
                        Chunk parent)
    • Method Detail

      • init

        protected void init​(java.nio.ByteBuffer buffer)
        Finishes initialization of a chunk. This should be called immediately after the constructor. This is separate from the constructor so that the header of a chunk can be fully initialized before the payload of that chunk is initialized for chunks that require such behavior.
        Parameters:
        buffer - The buffer that the payload will be initialized from.
      • getParent

        @Nullable
        public Chunk getParent()
        Returns the parent to this chunk, if any. A parent is a chunk whose payload contains this chunk. If there's no parent, null is returned.
      • getType

        protected abstract Chunk.Type getType()
      • getHeaderSize

        public final int getHeaderSize()
        Returns the size of this chunk's header.
      • getOriginalChunkSize

        public final int getOriginalChunkSize()
        Returns the size of this chunk when it was first read from a buffer. A chunk's size can deviate from this value when its data is modified (e.g. adding an entry, changing a string).

        A chunk's current size can be determined from the length of the byte array returned from toByteArray().

      • writeHeader

        protected final void writeHeader​(java.nio.ByteBuffer output,
                                         int chunkSize)
        Writes the type and header size. We don't know how big this chunk will be (it could be different since the last time we checked), so this needs to be passed in.
        Parameters:
        output - The buffer that will be written to.
        chunkSize - The total size of this chunk in bytes, including the header.
      • writeHeader

        protected void writeHeader​(java.nio.ByteBuffer output)
        Writes the remaining header (after the type, headerSize, and chunkSize).
        Parameters:
        output - The buffer that the header will be written to.
      • writePayload

        protected void writePayload​(java.io.DataOutput output,
                                    java.nio.ByteBuffer header,
                                    boolean shrink)
                             throws java.io.IOException
        Writes the chunk payload. The payload is data in a chunk which is not in the first headerSize bytes of the chunk.
        Parameters:
        output - The stream that the payload will be written to.
        header - The already-written header. This can be modified to fix payload offsets.
        shrink - True if this payload should be optimized for size.
        Throws:
        java.io.IOException - Thrown if output could not be written to (out of memory).
      • writePad

        protected int writePad​(java.io.DataOutput output,
                               int currentLength)
                        throws java.io.IOException
        Pads output until currentLength is on a 4-byte boundary.
        Parameters:
        output - The DataOutput that will be padded.
        currentLength - The current length, in bytes, of output
        Returns:
        The new length of output
        Throws:
        java.io.IOException - Thrown if output could not be written to.
      • toByteArray

        public final byte[] toByteArray()
                                 throws java.io.IOException
        Description copied from interface: SerializableResource
        Converts this resource into an array of bytes representation.
        Specified by:
        toByteArray in interface SerializableResource
        Returns:
        An array of bytes representing this resource.
        Throws:
        java.io.IOException
      • toByteArray

        public final byte[] toByteArray​(boolean shrink)
                                 throws java.io.IOException
        Converts this chunk into an array of bytes representation. Normally you will not need to override this method unless your header changes based on the contents / size of the payload.
        Specified by:
        toByteArray in interface SerializableResource
        Parameters:
        shrink - True if, when converting to a byte array, this resource can modify the returned bytes in an effort to reduce the size.
        Returns:
        An array of bytes representing this resource.
        Throws:
        java.io.IOException
      • newInstance

        public static Chunk newInstance​(java.nio.ByteBuffer buffer)
        Creates a new chunk whose contents start at buffer's current position.
        Parameters:
        buffer - A buffer positioned at the start of a chunk.
        Returns:
        new chunk
      • newInstance

        public static Chunk newInstance​(java.nio.ByteBuffer buffer,
                                        @Nullable
                                        Chunk parent)
        Creates a new chunk whose contents start at buffer's current position.
        Parameters:
        buffer - A buffer positioned at the start of a chunk.
        parent - The parent to this chunk (or null if there's no parent).
        Returns:
        new chunk