Class SimpleNIOBuffer

All Implemented Interfaces:
AutoCloseable, BufferProtocol, PyBUF, PyBuffer

public class SimpleNIOBuffer extends BaseNIOBuffer
Buffer API over a read-only one-dimensional java.nio.ByteBuffer of one-byte items.
  • Constructor Details

    • SimpleNIOBuffer

      public SimpleNIOBuffer(int flags, BufferProtocol obj, ByteBuffer storage, int index0, int size) throws PyException, ArrayIndexOutOfBoundsException, NullPointerException
      Provide an instance of SimpleNIOBuffer, on a slice of a ByteBuffer, meeting the consumer's expectations as expressed in the flags argument, which is checked against the capabilities of the buffer type. No reference will be kept to the ByteBuffer passed in. (It is duplicated.)
      Parameters:
      flags - consumer requirements
      obj - exporting object (or null)
      storage - the ByteBuffer wrapping the exported object state
      index0 - offset where the data starts in that buffer (item[0])
      size - the number of bytes occupied
      Throws:
      NullPointerException - if storage is null
      ArrayIndexOutOfBoundsException - if index0 and size are inconsistent with storage.length
      PyException - BufferError when expectations do not correspond with the type
    • SimpleNIOBuffer

      public SimpleNIOBuffer(int flags, BufferProtocol obj, ByteBuffer storage) throws PyException, NullPointerException
      Provide an instance of SimpleNIOBuffer, on the entirety of a ByteBuffer, meeting the consumer's expectations as expressed in the flags argument, which is checked against the capabilities of the buffer type. No reference will be kept to the ByteBuffer passed in. (It is duplicated.)
      Parameters:
      flags - consumer requirements
      obj - exporting object (or null)
      storage - the ByteBuffer wrapping the exported object state
      Throws:
      NullPointerException - if storage is null
      PyException - BufferError when expectations do not correspond with the type
  • Method Details

    • getLen

      public int getLen()
      The total number of bytes represented by the view, which will be the product of the elements of the shape array, and the item size in bytes.

      SimpleNIOBuffer provides an implementation optimised for contiguous bytes in one-dimension.

      Specified by:
      getLen in interface PyBUF
      Overrides:
      getLen in class Base1DBuffer
      Returns:
      the total number of bytes represented.
    • byteIndex

      public final int byteIndex(int index) throws IndexOutOfBoundsException
      Description copied from interface: PyBuffer
      Convert an item index (for a one-dimensional buffer) to an absolute byte index in the storage shared by the exporter. The storage exported as a PyBuffer is a linearly-indexed sequence of bytes, although it may not actually be a heap-allocated Java byte[] object. The purpose of this method is to allow the exporter to define the relationship between the item index (as used in PyBuffer.byteAt(int)) and the byte-index (as used with the ByteBuffer returned by PyBuffer.getNIOByteBuffer()). See PyBuffer.byteIndex(int[]) for discussion of the multi-dimensional case.
      Specified by:
      byteIndex in interface PyBuffer
      Overrides:
      byteIndex in class BaseBuffer
      Parameters:
      index - item-index from consumer
      Returns:
      corresponding byte-index in actual storage
      Throws:
      IndexOutOfBoundsException
    • getBufferSlice

      public PyBuffer getBufferSlice(int flags, int start, int count)
      Description copied from interface: PyBuffer
      Equivalent to PyBuffer.getBufferSlice(int, int, int, int) with stride 1.
      Specified by:
      getBufferSlice in interface PyBuffer
      Overrides:
      getBufferSlice in class BaseBuffer
      Parameters:
      flags - specifying features demanded and the navigational capabilities of the consumer
      start - index in the current buffer
      count - number of items in the required slice
      Returns:
      a buffer representing the slice
    • getBufferSlice

      public PyBuffer getBufferSlice(int flags, int start, int count, int stride)
      Get a PyBuffer that represents a slice of the current one described in terms of a start index, number of items to include in the slice, and the stride in the current buffer. A consumer that obtains a PyBuffer with getBufferSlice must release it with PyBuffer.release() just as if it had been obtained with PyBuffer.getBuffer(int)

      Suppose that x(i) denotes the ith element of the current buffer, that is, the byte retrieved by this.byteAt(i) or the unit indicated by this.getPointer(i). A request for a slice where start = s, count = N and stride = m, results in a buffer y such that y(k) = x(s+km) where k=0..(N-1). In Python terms, this is the slice x[s : s+(N-1)m+1 : m] (if m>0) or the slice x[s : s+(N-1)m-1 : m] (if m<0). Implementations should check that this range is entirely within the current buffer.

      In a simple buffer backed by a contiguous byte array, the result is a strided PyBuffer on the same storage but where the offset is adjusted by s and the stride is as supplied. If the current buffer is already strided and/or has an item size larger than single bytes, the new start index, count and stride will be translated from the arguments given, through this buffer's stride and item size. The caller always expresses start and strides in terms of the abstract view of this buffer.

      SimpleNIOBuffer provides an implementation for slicing contiguous bytes in one dimension. In that case, x(i) = u(r+i) for i = 0..L-1 where u is the underlying buffer, and r and L are the start and count with which x was created from u. Thus y(k) = u(r+s+km), that is, the composite offset is r+s and the stride is m.

      Parameters:
      flags - specifying features demanded and the navigational capabilities of the consumer
      start - index in the current buffer
      count - number of items in the required slice
      stride - index-distance in the current buffer between consecutive items in the slice
      Returns:
      a buffer representing the slice