Top Description Inners Fields Constructors Methods
org.python.core.buffer

public Class ZeroByteBuffer

extends BaseArrayBuffer
Class Inheritance
Known Direct Subclasses
org.python.core.buffer.ZeroByteBuffer.View
Imports
org.python.core.BufferProtocol, .PyBuffer, .PyException

Buffer API over a zero length, one-dimensional array of one-byte items. The buffer is nominally writable, but since there is nowhere to write to, any attempt to write or read throws an 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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
ZeroByteBuffer.View

A ZeroByteBuffer.View represents a contiguous subsequence of another PyBuffer.

Field Summary

Modifier and TypeField and Description
private static final byte[]
EMPTY

Shared instance of a zero-length storage.

Inherited from org.python.core.buffer.BaseArrayBuffer:
storage

Constructor Summary

AccessConstructor and Description
public
ZeroByteBuffer(int
consumer requirements
flags
,
BufferProtocol
exporting object (or null)
obj
,
boolean
set true if not to be considered writable
readonly
,
boolean
set true if to be considered as backed by an array
hasArray
)

Construct an instance of a zero-length buffer, choosing whether it should report itself to be read-only through isReadonly() or as having a backing array through hasArray().

Method Summary

Modifier and TypeMethod and Description
public int
byteIndex(int
item-index from consumer
index
)

Overrides org.python.core.buffer.BaseBuffer.byteIndex.

Implements org.python.core.PyBuffer.byteIndex.

In a ZeroByteBuffer, the index is always out of bounds.

public int
byteIndex(int...
n-dimensional item-index from consumer
indices
)

Overrides org.python.core.buffer.BaseArrayBuffer.byteIndex.

Implements org.python.core.PyBuffer.byteIndex.

In a ZeroByteBuffer, if the dimensions are right, the index is out of bounds anyway.

public void
copyFrom(byte[]
source byte array
src
,
int
location in source of first byte to copy
srcPos
,
int
starting item-index in the destination (i.e. this)
destIndex
,
int
number of items to copy in
count
)

Overrides org.python.core.buffer.BaseArrayBuffer.copyFrom.

Implements org.python.core.PyBuffer.copyFrom.

In a ZeroByteBuffer, there is no room for anything, so this throws unless the source count is zero.

public void
copyFrom(PyBuffer
source buffer
src
)

Overrides org.python.core.buffer.BaseArrayBuffer.copyFrom.

Implements org.python.core.PyBuffer.copyFrom.

In a ZeroByteBuffer, there is no room for anything, so this throws unless the source count is zero.

public void
copyTo(byte[]
destination byte array
dest
,
int
byte-index in the destination array of the byte [0]
destPos
)

Overrides org.python.core.buffer.BaseBuffer.copyTo.

Implements org.python.core.PyBuffer.copyTo.

Copy the contents of the buffer to the destination byte array.

public void
copyTo(int
starting item-index in the source buffer
srcIndex
,
byte[]
destination byte array
dest
,
int
byte-index in the destination array of the source item [0,...]
destPos
,
int
number of items to copy
count
)

Overrides org.python.core.buffer.BaseArrayBuffer.copyTo.

Implements org.python.core.PyBuffer.copyTo.

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.

public PyBuffer.Pointer
getBuf()

Overrides org.python.core.buffer.BaseArrayBuffer.getBuf.

Implements org.python.core.PyBuffer.getBuf.

References Deprecated PyBuffer.Pointer is deprecated or references (maybe indirectly) at least one deprecated element.

Return a structure describing the slice of a byte array that holds the data being exported to the consumer.

public PyBuffer
getBufferSlice(int
specifying features demanded and the navigational capabilities of the consumer
flags
,
int
index in the current buffer
start
,
int
number of items in the required slice
count
)

Overrides org.python.core.buffer.BaseBuffer.getBufferSlice.

Implements org.python.core.PyBuffer.getBufferSlice.

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.

public PyBuffer
getBufferSlice(int
specifying features demanded and the navigational capabilities of the consumer
flags
,
int
index in the current buffer
start
,
int
number of items in the required slice
count
,
int
index-distance in the current buffer between consecutive items in the slice
stride
)

Implements org.python.core.PyBuffer.getBufferSlice.

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.

public int
getLen()

Overrides org.python.core.buffer.Base1DBuffer.getLen.

Implements org.python.core.PyBUF.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.

public String
toString()

Overrides org.python.core.buffer.BaseBuffer.toString.

Implements org.python.core.PyBuffer.toString.

For a ZeroByteBuffer, it's the empty string.

Inherited from org.python.core.buffer.BaseArrayBuffer:
byteAtImplcopyFromgetNIOByteBufferImplstoreAtImpl

Field Detail

EMPTYback to summary
private static final byte[] EMPTY

Shared instance of a zero-length storage.

Constructor Detail

ZeroByteBufferback to summary
public ZeroByteBuffer(int flags, BufferProtocol obj, boolean readonly, boolean hasArray) throws PyException

Construct an instance of a zero-length buffer, choosing whether it should report itself to be read-only through isReadonly() or as having a backing array through 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.

Parameters
flags:int

consumer requirements

obj:BufferProtocol

exporting object (or null)

readonly:boolean

set true if not to be considered writable

hasArray:boolean

set true if to be considered as backed by an array

Exceptions
PyException:
BufferError when client expectations do not correspond with the type

Method Detail

byteIndexback to summary
public int byteIndex(int index) throws IndexOutOfBoundsException

Overrides org.python.core.buffer.BaseBuffer.byteIndex.

Implements org.python.core.PyBuffer.byteIndex.

In a ZeroByteBuffer, the index is always out of bounds.

Parameters
index:int

Doc from org.python.core.PyBuffer.byteIndex.

item-index from consumer

Returns:int

Doc from org.python.core.PyBuffer.byteIndex.

corresponding byte-index in actual storage

Annotations
@Override
byteIndexback to summary
public int byteIndex(int... indices) throws IndexOutOfBoundsException

Overrides org.python.core.buffer.BaseArrayBuffer.byteIndex.

Implements org.python.core.PyBuffer.byteIndex.

In a ZeroByteBuffer, if the dimensions are right, the index is out of bounds anyway.

Parameters
indices:int[]

Doc from org.python.core.PyBuffer.byteIndex.

n-dimensional item-index from consumer

Returns:int

Doc from org.python.core.PyBuffer.byteIndex.

corresponding byte-index in actual storage

Annotations
@Override
copyFromback to summary
public void copyFrom(byte[] src, int srcPos, int destIndex, int count) throws IndexOutOfBoundsException, PyException

Overrides org.python.core.buffer.BaseArrayBuffer.copyFrom.

Implements org.python.core.PyBuffer.copyFrom.

In a ZeroByteBuffer, there is no room for anything, so this throws unless the source count is zero.

Parameters
src:byte[]

Doc from org.python.core.PyBuffer.copyFrom.

source byte array

srcPos:int

Doc from org.python.core.PyBuffer.copyFrom.

location in source of first byte to copy

destIndex:int

Doc from org.python.core.PyBuffer.copyFrom.

starting item-index in the destination (i.e. this)

count:int

Doc from org.python.core.PyBuffer.copyFrom.

number of items to copy in

Annotations
@Override
Exceptions
IndexOutOfBoundsException:

Doc from org.python.core.PyBuffer.copyFrom.

if access out of bounds in source or destination

PyException:

Doc from org.python.core.PyBuffer.copyFrom.

TypeError if read-only buffer

copyFromback to summary
public void copyFrom(PyBuffer src) throws IndexOutOfBoundsException, PyException

Overrides org.python.core.buffer.BaseArrayBuffer.copyFrom.

Implements org.python.core.PyBuffer.copyFrom.

In a ZeroByteBuffer, there is no room for anything, so this throws unless the source count is zero.

Parameters
src:PyBuffer

Doc from org.python.core.PyBuffer.copyFrom.

source buffer

Annotations
@Override
Exceptions
IndexOutOfBoundsException:

Doc from org.python.core.PyBuffer.copyFrom.

if access out of bounds in source or destination

PyException:

Doc from org.python.core.PyBuffer.copyFrom.

TypeError if read-only buffer

copyToback to summary
public void copyTo(byte[] dest, int destPos) throws IndexOutOfBoundsException

Overrides org.python.core.buffer.BaseBuffer.copyTo.

Implements org.python.core.PyBuffer.copyTo.

Doc from org.python.core.buffer.BaseBuffer.copyTo.

Copy the contents of the buffer to the destination byte array. The number of bytes will be that returned by 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.

Parameters
dest:byte[]

Doc from org.python.core.PyBuffer.copyTo.

destination byte array

destPos:int

Doc from org.python.core.PyBuffer.copyTo.

byte-index in the destination array of the byte [0]

Annotations
@Override
Exceptions
IndexOutOfBoundsException:

Doc from org.python.core.PyBuffer.copyTo.

if the destination cannot hold it

copyToback to summary
public void copyTo(int srcIndex, byte[] dest, int destPos, int count) throws IndexOutOfBoundsException, PyException

Overrides org.python.core.buffer.BaseArrayBuffer.copyTo.

Implements org.python.core.PyBuffer.copyTo.

Doc from org.python.core.buffer.BaseArrayBuffer.copyTo.

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. 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.

Parameters
srcIndex:int

Doc from org.python.core.PyBuffer.copyTo.

starting item-index in the source buffer

dest:byte[]

Doc from org.python.core.PyBuffer.copyTo.

destination byte array

destPos:int

Doc from org.python.core.PyBuffer.copyTo.

byte-index in the destination array of the source item [0,...]

count:int

Doc from org.python.core.PyBuffer.copyTo.

number of items to copy

Annotations
@Override
Exceptions
IndexOutOfBoundsException:

Doc from org.python.core.PyBuffer.copyTo.

if access out of bounds in source or destination

getBufback to summary
public PyBuffer.Pointer getBuf()

Overrides org.python.core.buffer.BaseArrayBuffer.getBuf.

Implements org.python.core.PyBuffer.getBuf.

Doc from org.python.core.buffer.BaseArrayBuffer.getBuf.

References Deprecated

PyBuffer.Pointer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Return a structure describing the slice of a byte array that holds the data being exported to the consumer. For a one-dimensional contiguous buffer, assuming the following client code where 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.

Returns:PyBuffer.Pointer

Doc from org.python.core.PyBuffer.getBuf.

structure defining the byte[] slice that is the shared data

Annotations
@SuppressWarnings:deprecation
@Override
getBufferSliceback to summary
public PyBuffer getBufferSlice(int flags, int start, int count)

Overrides org.python.core.buffer.BaseBuffer.getBufferSlice.

Implements org.python.core.PyBuffer.getBufferSlice.

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.

Parameters
flags:int

Doc from org.python.core.PyBuffer.getBufferSlice.

specifying features demanded and the navigational capabilities of the consumer

start:int

Doc from org.python.core.PyBuffer.getBufferSlice.

index in the current buffer

count:int

Doc from org.python.core.PyBuffer.getBufferSlice.

number of items in the required slice

Returns:PyBuffer

Doc from org.python.core.PyBuffer.getBufferSlice.

a buffer representing the slice

Annotations
@Override
getBufferSliceback to summary
public PyBuffer getBufferSlice(int flags, int start, int count, int stride)

Implements org.python.core.PyBuffer.getBufferSlice.

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.

Parameters
flags:int

Doc from org.python.core.PyBuffer.getBufferSlice.

specifying features demanded and the navigational capabilities of the consumer

start:int

Doc from org.python.core.PyBuffer.getBufferSlice.

index in the current buffer

count:int

Doc from org.python.core.PyBuffer.getBufferSlice.

number of items in the required slice

stride:int

Doc from org.python.core.PyBuffer.getBufferSlice.

index-distance in the current buffer between consecutive items in the slice

Returns:PyBuffer

Doc from org.python.core.PyBuffer.getBufferSlice.

a buffer representing the slice

Annotations
@Override
getLenback to summary
public int getLen()

Overrides org.python.core.buffer.Base1DBuffer.getLen.

Implements org.python.core.PyBUF.getLen.

Doc from org.python.core.PyBUF.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.

Returns:int

the total number of bytes represented.

Annotations
@Override
toStringback to summary
public String toString()

Overrides org.python.core.buffer.BaseBuffer.toString.

Implements org.python.core.PyBuffer.toString.

For a ZeroByteBuffer, it's the empty string.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object

Annotations
@Override
org.python.core.buffer back to summary

pack-priv Class ZeroByteBuffer.View

extends ZeroByteBuffer
Class Inheritance

A ZeroByteBuffer.View represents a contiguous subsequence of another PyBuffer. We don't need it to make slices of the ZeroByteBuffer itself, but it is useful for making zero-length slices of anything else. Lock-release semantics must still be observed. In Python, a zero-length slice obtained from the memoryview of a bytearray still counts as an export from the bytearray.

Field Summary

Modifier and TypeField and Description
pack-priv PyBuffer
root

The buffer on which this is a slice view

Constructor Summary

AccessConstructor and Description
public
View(PyBuffer
buffer which will be acquired and must be released ultimately
root
,
int
the request flags of the consumer that requested the slice
flags
)

Construct a slice of a ZeroByteBuffer, which it goes without saying is of zero length at position zero.

Method Summary

Modifier and TypeMethod and Description
protected PyBuffer
getRoot()

Overrides org.python.core.buffer.BaseBuffer.getRoot.

Some PyBuffers, those created by slicing a PyBuffer, are related to a root PyBuffer.

Inherited from org.python.core.buffer.ZeroByteBuffer:
byteIndexbyteIndexcopyFromcopyFromcopyTocopyTogetBufgetBufferSlicegetBufferSlicegetLentoString

Field Detail

rootback to summary
pack-priv PyBuffer root

The buffer on which this is a slice view

Constructor Detail

Viewback to summary
public View(PyBuffer root, int flags)

Construct a slice of a ZeroByteBuffer, which it goes without saying is of zero length at position zero.

Parameters
root:PyBuffer

buffer which will be acquired and must be released ultimately

flags:int

the request flags of the consumer that requested the slice

Method Detail

getRootback to summary
protected PyBuffer getRoot()

Overrides org.python.core.buffer.BaseBuffer.getRoot.

Doc from org.python.core.buffer.BaseBuffer.getRoot.

Some PyBuffers, those created by slicing a PyBuffer, are related to a root PyBuffer. During creation of such a slice, we need to supply a value for this root. If the present object is not itself a slice, this root is the object itself; if the buffer is already a slice, it is the root it was given at creation time. Often this is the only difference between a slice-view and a directly-exported buffer. Override this method in slices to return the root buffer of the slice.

Returns:PyBuffer

this buffer (or the root buffer if this is a sliced view)

Annotations
@Override