public class ZeroByteBuffer extends BaseBuffer
IndexOutOfBoundsException
. This class exists mostly to represent zero-length arrays,
and particularly, zero-length slices for which implementations of
PyBuffer.getBufferSlice(int, int, int, int)
in any case need special logic. Bulk
operations like copyTo(byte[], int)
) and toString()
efficiently do nothing,
instead of calling complicated logic that finally does nothing.PyBuffer.Pointer
ANY_CONTIGUOUS, AS_ARRAY, C_CONTIGUOUS, CONTIG, CONTIG_RO, CONTIGUITY, F_CONTIGUOUS, FORMAT, FULL, FULL_RO, INDIRECT, IS_C_CONTIGUOUS, IS_F_CONTIGUOUS, MAX_NDIM, NAVIGATION, ND, RECORDS, RECORDS_RO, SIMPLE, STRIDED, STRIDED_RO, STRIDES, WRITABLE
Constructor and Description |
---|
ZeroByteBuffer(int flags,
boolean readonly)
Construct an instance of a zero-length buffer, choosing whether it should report itself to be
read-only through
BaseBuffer.isReadonly() . |
Modifier and Type | Method and Description |
---|---|
void |
copyFrom(byte[] src,
int srcPos,
int destIndex,
int length)
In a ZeroByteBuffer, there is no room for anything, so this throws unless the source length
is zero.
|
void |
copyFrom(PyBuffer src)
In a ZeroByteBuffer, there is no room for anything, so this throws unless the source length
is zero.
|
void |
copyTo(byte[] dest,
int destPos)
Copy the contents of the buffer to the destination byte array.
|
void |
copyTo(int srcIndex,
byte[] dest,
int destPos,
int length)
Copy a simple slice of the buffer to the destination byte array, defined by a starting index
and length in the source buffer.
|
PyBuffer.Pointer |
getBuf()
Return a structure describing the slice of a byte array that holds the data being exported to
the consumer.
|
PyBuffer |
getBufferSlice(int flags,
int start,
int length)
Only a zero-length slice at zero is valid (in which case, the present buffer will do nicely
as a result, with the export count incremented.
|
PyBuffer |
getBufferSlice(int flags,
int start,
int length,
int stride)
Only a zero-length slice at zero is valid (in which case, the present buffer will do nicely
as a result, with the export count incremented.
|
int |
getLen()
The total number of units (bytes) stored, which will be the product of the elements of the
shape array, and the item size in units. |
String |
toString()
For a ZeroByteBuffer, it's the empty string.
|
byteAt, byteAt, close, getBuffer, getBufferAgain, getFormat, getItemsize, getNdim, getNIOByteBuffer, getPointer, getPointer, getShape, getStrides, getSuboffsets, hasArray, intAt, intAt, isContiguous, isReadonly, isReleased, release, storeAt, storeAt
public ZeroByteBuffer(int flags, boolean readonly) throws PyException
BaseBuffer.isReadonly()
. This is moot, as any attempt to write to it produces
an IndexOutOfBoundsException
, but it is less surprising for client code that may ask,
if the readability follows that of the object from which the buffer is derived.flags
- consumer requirementsreadonly
- set true if readonlyPyException
- (BufferError) when expectations do not correspond with the typepublic int getLen()
BaseBuffer
shape
array, and the item size in units.
The default implementation in BaseBuffer
deals with the general one-dimensional
case, with any item size and stride.
getLen
in interface PyBUF
getLen
in class BaseBuffer
public void copyTo(byte[] dest, int destPos) throws IndexOutOfBoundsException
PyBUF.getLen()
, and the order is the storage order in the exporter.
(Note: Correct ordering for multidimensional arrays, including those with indirection needs
further study.)
The default implementation in BaseBuffer
deals with the general one-dimensional
case of arbitrary item size and stride.
In a ZeroByteBuffer, there is simply nothing to copy.
copyTo
in interface PyBuffer
copyTo
in class BaseBuffer
dest
- destination byte arraydestPos
- index in the destination array of the byte [0]IndexOutOfBoundsException
- if the destination cannot hold itpublic void copyTo(int srcIndex, byte[] dest, int destPos, int length) throws IndexOutOfBoundsException, PyException
length*itemsize
bytes will be occupied
in the destination.
The default implementation in BaseBuffer
deals with the general one-dimensional
case of arbitrary item size and stride.
In a ZeroByteBuffer, there is simply nothing to copy.
copyTo
in interface PyBuffer
copyTo
in class BaseBuffer
srcIndex
- starting index in the source bufferdest
- destination byte arraydestPos
- index in the destination array of the item [0,...]length
- number of items to copyIndexOutOfBoundsException
- if access out of bounds in source or destinationPyException
public void copyFrom(byte[] src, int srcPos, int destIndex, int length) throws IndexOutOfBoundsException, PyException
copyFrom
in interface PyBuffer
copyFrom
in class BaseBuffer
src
- source byte arraysrcPos
- location in source of first byte to copydestIndex
- starting index in the destination (i.e. this
)length
- number of bytes to copy inIndexOutOfBoundsException
- if access out of bounds in source or destinationPyException
- (TypeError) if read-only bufferpublic void copyFrom(PyBuffer src) throws IndexOutOfBoundsException, PyException
copyFrom
in interface PyBuffer
copyFrom
in class BaseBuffer
src
- source bufferIndexOutOfBoundsException
- if access out of bounds in source or destinationPyException
- (TypeError) if read-only bufferpublic PyBuffer getBufferSlice(int flags, int start, int length)
getBufferSlice
in interface PyBuffer
getBufferSlice
in class BaseBuffer
flags
- specifying features demanded and the navigational capabilities of the consumerstart
- index in the current bufferlength
- number of items in the required slicepublic PyBuffer getBufferSlice(int flags, int start, int length, int stride)
flags
- specifying features demanded and the navigational capabilities of the consumerstart
- index in the current bufferlength
- number of items in the required slicestride
- index-distance in the current buffer between consecutive items in the slicepublic PyBuffer.Pointer getBuf()
obj
has type BufferProtocol
:
PyBuffer a = obj.getBuffer(PyBUF.SIMPLE); int itemsize = a.getItemsize(); PyBuffer.Pointer b = a.getBuf();the item with index
k
is in the array b.storage
at index
[b.offset + k*itemsize]
to [b.offset + (k+1)*itemsize - 1]
inclusive. And if itemsize==1
, the item is simply the byte
b.storage[b.offset + k]
If the buffer is multidimensional or non-contiguous, storage[offset]
is still
the (first byte of) the item at index [0] or [0,...,0]. However, it is necessary to navigate
b.storage
using the shape
, strides
and maybe
suboffsets
provided by the API.
The implementation in ZeroByteBuffer
efficiently returns an empty buffer.
getBuf
in interface PyBuffer
getBuf
in class BaseBuffer
public String toString()
toString
in interface PyBuffer
toString
in class BaseBuffer