Class UnsignedByteBuffer


public final class UnsignedByteBuffer extends UnsignedBufferAsInt
Wraps a ByteBuffer but automatically performs conversion to int.

The conversion applies to ByteBuffer.get() and ByteBuffer.put(byte) of single elements, but not to any mass get or put operations.

The user has a choice of getting/setting using raw (getRaw(), putRaw(byte) etc.) or unsigned-conversion (getUnsigned(), putUnsigned(int) etc.) methods. The raw methods are always more efficient, and so should be preferred when conversion is not needed.

Author:
Owen Feehan
  • Method Summary

    Modifier and Type
    Method
    Description
    allocate(int capacity)
    Allocates a new buffer of unsigned-bytes.
    final byte[]
    The array of the buffer ala ByteBuffer.array().
    protected boolean
     
    boolean
     
    The underlying storage buffer, to which calls are delegated with our without conversion.
    byte
    Gets an unsigned-byte (represented as a byte) at the current buffer position.
    byte
    getRaw(int index)
    Gets an unsigned-byte (represented as a byte) at a particular buffer position.
    int
    Gets an unsigned-byte (represented as a int) at the current buffer position.
    int
    getUnsigned(int index)
    Gets an unsigned-byte (represented as a int) at a particular buffer position.
    int
     
    void
    put(ByteBuffer source)
    Relative put-method from an unsigned byte buffer, represented by a ByteBuffer.
    void
    Relative put-method from an unsigned byte buffer, represented by a UnsignedByteBuffer.
    void
    putDouble(double value)
    Puts a double at the current buffer position.
    void
    putDouble(int index, double value)
    Puts a double at a particular buffer position.
    void
    putDoubleClamped(double value)
    Puts a double at the current buffer position, clamping to ensure the value is within the range (0,255).
    void
    putFloat(float value)
    Puts a float at the current buffer position.
    void
    putFloat(int index, float value)
    Puts a float at a particular buffer position.
    void
    putFloatClamped(float value)
    Puts a float at the current buffer position, clamping to ensure the value is within the range (0,255).
    void
    putLong(long value)
    Puts a long at the current buffer position.
    void
    putRaw(byte value)
    Puts an unsigned-byte (represented as a byte) at the current buffer position.
    void
    putRaw(int index, byte value)
    Puts an unsigned-byte (represented as a byte) a particular buffer position.
    void
    putUnsigned(int value)
    Puts an unsigned-byte (represented by an int) at current buffer position.
    void
    putUnsigned(int index, int value)
    Puts an unsigned-byte (represented as a int) a particular buffer position.
    void
    putUnsignedInt(int value)
    Puts an unsigned-int (represented by a int) at current buffer position.
    final void
    Rewinds the buffer ala ByteBuffer.rewind().
    wrapRaw(byte[] array)
    Exposes a raw byte-array as a buffer with unsigned-bytes.
    wrapRaw(ByteBuffer bufferRaw)
    Exposes a raw ByteBuffer as a buffer with unsigned-bytes.

    Methods inherited from class org.anchoranalysis.image.voxel.buffer.primitive.UnsignedBufferAsInt

    toString

    Methods inherited from class org.anchoranalysis.image.voxel.buffer.primitive.UnsignedBuffer

    capacity, clear, hasArray, hasRemaining, isDirect, position, position

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • allocate

      public static UnsignedByteBuffer allocate(int capacity)
      Allocates a new buffer of unsigned-bytes.
      Parameters:
      capacity - size of buffer.
      Returns:
      newly created buffer (non-direct, i.e. backed by an array).
    • wrapRaw

      public static UnsignedByteBuffer wrapRaw(byte[] array)
      Exposes a raw byte-array as a buffer with unsigned-bytes.
      Parameters:
      array - the byte-array
      Returns:
      a new instance of UnsignedByteBuffer that reuses array internally.
    • wrapRaw

      public static UnsignedByteBuffer wrapRaw(ByteBuffer bufferRaw)
      Exposes a raw ByteBuffer as a buffer with unsigned-bytes.
      Parameters:
      bufferRaw - the raw-buffer
      Returns:
      a new instance of UnsignedByteBuffer that reuses bufferRaw internally.
    • getRaw

      public byte getRaw()
      Gets an unsigned-byte (represented as a byte) at the current buffer position.

      No further conversion occurs, so this method is more efficient than getUnsigned().

      Returns:
      unsigned-byte (represented by a byte)
    • getRaw

      public byte getRaw(int index)
      Gets an unsigned-byte (represented as a byte) at a particular buffer position.

      No further conversion occurs, so this method is more efficient than getUnsigned(int).

      Parameters:
      index - the buffer position
      Returns:
      unsigned-byte (represented by a byte)
    • getUnsigned

      public int getUnsigned()
      Description copied from class: UnsignedBufferAsInt
      Gets an unsigned-byte (represented as a int) at the current buffer position.
      Specified by:
      getUnsigned in class UnsignedBufferAsInt
      Returns:
      unsigned-byte (represented by a int)
    • getUnsigned

      public int getUnsigned(int index)
      Description copied from class: UnsignedBufferAsInt
      Gets an unsigned-byte (represented as a int) at a particular buffer position.
      Specified by:
      getUnsigned in class UnsignedBufferAsInt
      Parameters:
      index - the buffer position
      Returns:
      unsigned-byte (represented by a int)
    • putRaw

      public void putRaw(byte value)
      Puts an unsigned-byte (represented as a byte) at the current buffer position.

      No further conversion occurs, so this method is more efficient than putUnsigned(int).

      Parameters:
      value - unsigned-byte (represented by a byte)
    • putRaw

      public void putRaw(int index, byte value)
      Puts an unsigned-byte (represented as a byte) a particular buffer position.

      No further conversion occurs, so this method is more efficient than putUnsigned(int,int).

      Parameters:
      index - the buffer position
      value - the unsigned-byte (represented by an int)
    • putUnsigned

      public void putUnsigned(int value)
      Description copied from class: UnsignedBufferAsInt
      Puts an unsigned-byte (represented by an int) at current buffer position.

      A conversion occurs from int to byte.

      Specified by:
      putUnsigned in class UnsignedBufferAsInt
      Parameters:
      value - the unsigned-byte (represented by an int)
    • putUnsigned

      public void putUnsigned(int index, int value)
      Description copied from class: UnsignedBufferAsInt
      Puts an unsigned-byte (represented as a int) a particular buffer position.

      A conversion occurs from int to byte.

      Specified by:
      putUnsigned in class UnsignedBufferAsInt
      Parameters:
      index - the buffer position
      value - the unsigned-byte (represented by an int)
    • putUnsignedInt

      public void putUnsignedInt(int value)
      Puts an unsigned-int (represented by a int) at current buffer position.

      A conversion occurs from int to long and then to byte.

      Parameters:
      value - the unsigned-int (represented by an int)
    • putLong

      public void putLong(long value)
      Description copied from class: UnsignedBuffer
      Puts a long at the current buffer position.
      Specified by:
      putLong in class UnsignedBuffer
      Parameters:
      value - the long.
    • putFloat

      public void putFloat(float value)
      Description copied from class: UnsignedBuffer
      Puts a float at the current buffer position.

      A conversion occurs from float to short.

      Specified by:
      putFloat in class UnsignedBuffer
      Parameters:
      value - the float.
    • putFloat

      public void putFloat(int index, float value)
      Description copied from class: UnsignedBuffer
      Puts a float at a particular buffer position.

      A conversion occurs from float to byte.

      Specified by:
      putFloat in class UnsignedBuffer
      Parameters:
      index - the buffer position
      value - the float.
    • putFloatClamped

      public void putFloatClamped(float value)
      Puts a float at the current buffer position, clamping to ensure the value is within the range (0,255).
      Parameters:
      value - the float
    • putDouble

      public void putDouble(double value)
      Description copied from class: UnsignedBuffer
      Puts a double at the current buffer position.
      Specified by:
      putDouble in class UnsignedBuffer
      Parameters:
      value - the double
    • putDoubleClamped

      public void putDoubleClamped(double value)
      Puts a double at the current buffer position, clamping to ensure the value is within the range (0,255).
      Parameters:
      value - the double
    • putDouble

      public void putDouble(int index, double value)
      Description copied from class: UnsignedBuffer
      Puts a double at a particular buffer position.
      Specified by:
      putDouble in class UnsignedBuffer
      Parameters:
      index - the buffer position
      value - the double
    • put

      public void put(ByteBuffer source)
      Relative put-method from an unsigned byte buffer, represented by a ByteBuffer.

      This is identical to ByteBuffer.put(ByteBuffer), with no return value.

      Parameters:
      source - source of bytes to put
    • put

      public void put(UnsignedByteBuffer source)
      Relative put-method from an unsigned byte buffer, represented by a UnsignedByteBuffer.

      This is identical to ByteBuffer.put(ByteBuffer), with no return value.

      Parameters:
      source - source of bytes to put
    • array

      public final byte[] array()
      The array of the buffer ala ByteBuffer.array().

      Unlike ByteBuffer.array() an array will always be returned, copying it into a newly created array, if it cannot be directly accessed.

      Returns:
      the array
    • rewind

      public final void rewind()
      Rewinds the buffer ala ByteBuffer.rewind().
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class UnsignedBufferAsInt
    • canEqual

      protected boolean canEqual(Object other)
      Overrides:
      canEqual in class UnsignedBufferAsInt
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class UnsignedBufferAsInt
    • getDelegate

      public ByteBuffer getDelegate()
      The underlying storage buffer, to which calls are delegated with our without conversion.