public class ZeroByteBuffer extends BaseArrayBuffer
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,
BufferProtocol obj,
boolean readonly,
boolean hasArray)
Construct an instance of a zero-length buffer, choosing whether it should report itself to be
read-only through
BaseBuffer.isReadonly() or as having a backing array through
BaseBuffer.hasArray() . |
Modifier and Type | Method and Description |
---|---|
int |
byteIndex(int... indices)
In a ZeroByteBuffer, if the dimensions are right, the index is out of bounds anyway.
|
int |
byteIndex(int index)
In a ZeroByteBuffer, the index is always out of bounds.
|
void |
copyFrom(byte[] src,
int srcPos,
int destIndex,
int count)
In a ZeroByteBuffer, there is no room for anything, so this throws unless the source count is
zero.
|
void |
copyFrom(PyBuffer src)
In a ZeroByteBuffer, there is no room for anything, so this throws unless the source count 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 count)
Copy a simple slice of the buffer-view to the destination byte array, defined by a starting
item-index in the source buffer and the
count of items to copy. |
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 count)
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 count,
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 bytes represented by the view, which will be the product of the elements of the
shape array, and the item size in bytes. |
String |
toString()
For a ZeroByteBuffer, it's the empty string.
|
isContiguous
byteAt, byteAt, close, getBuffer, getBufferAgain, getFormat, getItemsize, getNdim, getNIOByteBuffer, getObj, getPointer, getPointer, getShape, getStrides, getSuboffsets, hasArray, intAt, intAt, isReadonly, isReleased, release, storeAt, storeAt
public ZeroByteBuffer(int flags, BufferProtocol obj, boolean readonly, boolean hasArray) throws PyException
BaseBuffer.isReadonly()
or as having a backing array through
BaseBuffer.hasArray()
. These properties are moot, as any attempt to write to the pretended
backing array produces an IndexOutOfBoundsException
, but it is less surprising for
client code that may ask, if the results are customary for the exporting object.flags
- consumer requirementsobj
- exporting object (or null
)readonly
- set true if not to be considered writablehasArray
- set true if to be considered as backed by an arrayPyException
- (BufferError) when client expectations do not correspond with the typepublic int getLen()
PyBUF
shape
array, and the item size in bytes.getLen
in interface PyBUF
getLen
in class Base1DBuffer
public int byteIndex(int index) throws IndexOutOfBoundsException
byteIndex
in interface PyBuffer
byteIndex
in class BaseBuffer
index
- item-index from consumerIndexOutOfBoundsException
public int byteIndex(int... indices) throws IndexOutOfBoundsException
byteIndex
in interface PyBuffer
byteIndex
in class BaseArrayBuffer
indices
- n-dimensional item-index from consumerIndexOutOfBoundsException
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
- byte-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 count) throws IndexOutOfBoundsException, PyException
count
of items to copy. This may validly
be done only for a one-dimensional buffer, as the meaning of the starting item-index is
otherwise not defined. count*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, but is unable to optimise access to sequential bytes.
The implementation in BaseArrayBuffer
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 BaseArrayBuffer
srcIndex
- starting item-index in the source bufferdest
- destination byte arraydestPos
- byte-index in the destination array of the source item [0,...]count
- number of items to copyIndexOutOfBoundsException
- if access out of bounds in source or destinationPyException
public void copyFrom(byte[] src, int srcPos, int destIndex, int count) throws IndexOutOfBoundsException, PyException
copyFrom
in interface PyBuffer
copyFrom
in class BaseArrayBuffer
src
- source byte arraysrcPos
- location in source of first byte to copydestIndex
- starting item-index in the destination (i.e. this
)count
- number of items 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 BaseArrayBuffer
src
- source bufferIndexOutOfBoundsException
- if access out of bounds in source or destinationPyException
- (TypeError) if read-only bufferpublic PyBuffer getBufferSlice(int flags, int start, int count)
getBufferSlice
in interface PyBuffer
getBufferSlice
in class BaseBuffer
flags
- specifying features demanded and the navigational capabilities of the consumerstart
- index in the current buffercount
- number of items in the required slicepublic PyBuffer getBufferSlice(int flags, int start, int count, int stride)
flags
- specifying features demanded and the navigational capabilities of the consumerstart
- index in the current buffercount
- 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.
BaseArrayBuffer
provides a reference to the storage array even when the buffer
is intended not to be writable. There can be no enforcement of read-only character once a
reference to the byte array has been handed out.
The implementation in ZeroByteBuffer
efficiently returns an empty buffer.
getBuf
in interface PyBuffer
getBuf
in class BaseArrayBuffer
public String toString()
toString
in interface PyBuffer
toString
in class BaseBuffer