Class ByteBuffer


  • public class ByteBuffer
    extends Object
    Represents a buffer of native memory with a specific size/capacity which may contains a specific number of bytes of valid data. If the memory represented by a ByteBuffer instance has been allocated by the ByteBuffer instance itself, the native memory will automatically be freed upon finalization.
    Author:
    Lyubomir Marinov
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteBuffer​(int capacity)
      Initializes a new ByteBuffer instance with a specific capacity of native memory.
      ByteBuffer​(long ptr)
      Initializes a new ByteBuffer instance which is to represent a specific block of native memory.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void finalize()
      Frees the native memory represented by this instance if the native memory has been allocated by this instance and has not been freed yet i.e.
      void free()
      Frees the native memory represented by this instance if the native memory has been allocated by this instance and has not been freed yet.
      int getCapacity()
      Gets the maximum number of bytes which may be written into the native memory represented by this instance.
      int getLength()
      Gets the number of bytes of valid data that the native memory represented by this instance contains.
      long getPtr()
      Gets the pointer to the native memory represented by this instance.
      void setLength​(int length)
      Sets the number of bytes of valid data that the native memory represented by this instance contains.
    • Constructor Detail

      • ByteBuffer

        public ByteBuffer​(int capacity)
        Initializes a new ByteBuffer instance with a specific capacity of native memory. The new instance allocates the native memory and automatically frees it upon finalization.
        Parameters:
        capacity - the maximum number of bytes which can be written into the native memory represented by the new instance
      • ByteBuffer

        public ByteBuffer​(long ptr)
        Initializes a new ByteBuffer instance which is to represent a specific block of native memory. Since the specified native memory has been allocated outside the new instance, the new instance will not automatically free it.
        Parameters:
        ptr - a pointer to the block of native memory to be represented by the new instance
    • Method Detail

      • finalize

        protected void finalize()
                         throws Throwable
        Frees the native memory represented by this instance if the native memory has been allocated by this instance and has not been freed yet i.e. ensures that free() is invoked on this instance.
        Overrides:
        finalize in class Object
        Throws:
        Throwable
        See Also:
        Object.finalize()
      • free

        public void free()
        Frees the native memory represented by this instance if the native memory has been allocated by this instance and has not been freed yet.
      • getCapacity

        public int getCapacity()
        Gets the maximum number of bytes which may be written into the native memory represented by this instance. If 0, this instance has been initialized to provide read-only access to the native memory it represents and will not deallocate it upon finalization.
        Returns:
        the maximum number of bytes which may be written into the native memory represented by this instance
      • getLength

        public int getLength()
        Gets the number of bytes of valid data that the native memory represented by this instance contains.
        Returns:
        the number of bytes of valid data that the native memory represented by this instance contains
      • getPtr

        public long getPtr()
        Gets the pointer to the native memory represented by this instance.
        Returns:
        the pointer to the native memory represented by this instance
      • setLength

        public void setLength​(int length)
        Sets the number of bytes of valid data that the native memory represented by this instance contains.
        Parameters:
        length - the number of bytes of valid data that the native memory represented by this instance contains
        Throws:
        IllegalArgumentException - if length is a negative value