com.devexperts.io

Class Chunk

    • Field Detail

      • bytes

        protected final byte[] bytes
      • offset

        protected int offset
      • length

        protected int length
    • Constructor Detail

      • Chunk

        protected Chunk(ChunkPool pool,
             byte[] bytes,
             Object owner)
        Constructs a new chunk of specified owner that wraps given byte array.
        Parameters:
        pool - pool that will be used to recycle this chunk
        bytes - byte array that will be wrapped by this chunk
        owner - owner of the new chunk
        Throws:
        NullPointerException - if bytes is null
    • Method Detail

      • wrap

        public static Chunk wrap(byte[] bytes,
                 Object owner)
        Constructs new chunk of specified owner that wraps given byte array and does not correspond to any pool.

        Note: wrapping must be used only with 'throw-away' arrays that will not be used by source code after wrapping.

        Parameters:
        bytes - the byte array to wrap
        owner - owner of the new chunk
        Returns:
        new chunk
        Throws:
        NullPointerException - if bytes is null.
      • wrap

        public static Chunk wrap(byte[] bytes,
                 int offset,
                 int length,
                 Object owner)
        Constructs a new chunk of specified owner that wraps given byte array with specified range of data and does not correspond to any pool.

        Note: wrapping must be used only with 'throw-away' arrays that will not be used by source code after wrapping.

        Parameters:
        bytes - the byte array to wrap
        offset - position of the first meaningful byte
        length - total number of meaningful bytes
        owner - owner of the new chunk
        Returns:
        new chunk
        Throws:
        NullPointerException - if bytes is null.
        IndexOutOfBoundsException - if the region defined by offset and length is out of underlying byte array range
      • getPool

        public ChunkPool getPool()
        Returns chunk pool that this chunk was allocated from or null if none.
        Returns:
        chunk pool that this chunk was allocated from or null if none.
      • getBytes

        public byte[] getBytes()
        Returns byte array, wrapped by this chunk.

        Note: although this method can access underlying bytes of a chunk regardless of its owner and "read-only" status, it is strictly forbidden to modify the bytes if the chunk is read-only or is owned by someone else. It is neither allowed to recycle the chunk or hand it over to someone else while you are using its underlying byte array. A caution is required when passing this byte array into various routines in order to be sure, that these routines won't store a reference to the array to access it later.

        Returns:
        byte array, wrapped by this chunk
      • getLength

        public int getLength()
        Returns the total number of meaningful bytes stored in this chunk's underlying byte array.
        Returns:
        total number of meaningful bytes stored in this chunk's underlying byte array
      • setLength

        public void setLength(int length,
                     Object owner)
        Sets the total number of meaningful bytes stored in this chunk's underlying byte array.
        Parameters:
        length - total number of meaningful bytes stored in this chunk's underlying byte array
        owner - owner of the chunk
        Throws:
        IndexOutOfBoundsException - if length is negative or if offset+length exceeds underlying array length
        IllegalStateException - if the chunk's owner differs from the one specified (in particular, if the chunk is read-only).
      • setRange

        public void setRange(int offset,
                    int length,
                    Object owner)
        Sets the range occupied by meaningful data within this chunk's underlying byte array.
        Parameters:
        offset - position of the first meaningful byte
        length - total number of meaningful bytes
        owner - owner of the chunk
        Throws:
        IndexOutOfBoundsException - if the region defined by offset and length is out of underlying byte array range
        IllegalStateException - if the chunk's owner differs from the one specified (in particular, if the chunk is read-only).
      • isReadOnly

        public boolean isReadOnly()
        Checks whether the chunk is read-only.
        Returns:
        true if this chunk is read-only.
        See Also:
        markReadOnly(Object)
      • markReadOnly

        public void markReadOnly(Object owner)
        Marks this chunk as read-only. Does nothing if the chunk is already read-only.
        Parameters:
        owner - current owner of the chunk
        Throws:
        IllegalStateException - if the chunk is not read-only and its current owner differs from the one specified
      • handOver

        public void handOver(Object oldOwner,
                    Object newOwner)
        Hands over the chunk to another owner. Does nothing if the chunk is read-only.
        Parameters:
        oldOwner - old (current) owner of the chunk
        newOwner - new owner of the chunk
        Throws:
        IllegalStateException - if the chunk is not read-only and its current owner differs from the one specified
      • recycle

        public void recycle(Object owner)
        Returns the chunk into the pool (or invalidates it if it does not correspond to any pool). This method has no effect if this chunk is read-only. A reference to this chunk is considered to become invalid after invocation of this method and may no longer be used, unless the chunk was read-only.
        Parameters:
        owner - current owner of the chunk
        Throws:
        IllegalStateException - if the chunk is not read-only and its current owner differs from the one specified
      • checkOwner

        protected void checkOwner(Object owner)
        Throws an exception if the chunk is not owned by specified owner.
        Parameters:
        owner - expected owner of the chunk
        Throws:
        IllegalStateException - if the chunk's current owner differs from the one specified
      • toString

        public String toString()
        Returns a string representation of the object.
        Overrides:
        toString in class Object
        Returns:
        a string representation of the object.

Copyright © 2015 Devexperts. All Rights Reserved.