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

public Class SimpleStringBuffer

extends SimpleBuffer
Class Inheritance
Known Direct Subclasses
org.python.core.buffer.SimpleStringBuffer.SimpleStringView
Imports
java.nio.ByteBuffer, org.python.core.BufferProtocol, .PyBuffer, org.python.core.util.StringUtil

Buffer API that appears to be a one-dimensional array of one-byte items providing read-only API, but which is actually backed by a Java String. Some of the buffer API absolutely needs access to the data as a byte array (those parts that involve a java.nio.ByteBuffer or org.python.core.PyBuffer.Pointer result), and therefore this class must create a byte array from the String for them. However, it defers creation of a byte array until that part of the API is actually used. Where possible, this class overrides those methods in SimpleBuffer that would otherwise access the byte array attribute to use the String instead.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
SimpleStringBuffer.SimpleStringView

A SimpleStringBuffer.SimpleStringView represents a contiguous subsequence of another SimpleStringBuffer.

Field Summary

Modifier and TypeField and Description
private String
bufString

The string backing this PyBuffer.

Constructor Summary

AccessConstructor and Description
public
SimpleStringBuffer(int
consumer requirements
flags
,
BufferProtocol
exporting object (or null)
obj
,
String
storing the implementation of the object
bufString
)

Provide an instance of SimpleStringBuffer meeting the consumer's expectations as expressed in the flags argument.

Method Summary

Modifier and TypeMethod and Description
public final byte
byteAtImpl(int
byte-index of location to retrieve
index
)

Overrides org.python.core.buffer.BaseArrayBuffer.byteAtImpl.

Retrieve the byte at the given index in the underlying storage treated as a flat sequence of bytes.

public final int
byteIndex(int
item-index from consumer
index
)

Overrides org.python.core.buffer.SimpleBuffer.byteIndex.

Implements org.python.core.PyBuffer.byteIndex.

Convert an item index (for a one-dimensional buffer) to an absolute byte index in the storage shared by the exporter.

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.

private void
ensureHaveBytes()

This method creates an actual byte array from the underlying String if none yet exists.

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

Implements org.python.core.PyBuffer.getBufferSlice.

Equivalent to getBufferSlice(int, int, int, int) with stride 1.

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
)

Overrides org.python.core.buffer.SimpleBuffer.getBufferSlice.

Implements org.python.core.PyBuffer.getBufferSlice.

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.

public int
getLen()

Overrides org.python.core.buffer.SimpleBuffer.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.

protected ByteBuffer
getNIOByteBufferImpl()

Overrides org.python.core.buffer.BaseArrayBuffer.getNIOByteBufferImpl.

Create a new java.nio.ByteBuffer on the underlying storage, such that positioning this buffer to a particular byte using byteIndex(int) or byteIndex(int[]) positions it at the first byte of the item so indexed.

public PyBuffer.Pointer
getPointer(int
in the buffer to position the pointer
index
)

Overrides org.python.core.buffer.SimpleBuffer.getPointer.

Implements org.python.core.PyBuffer.getPointer.

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

Return a structure describing the position in a byte array of a single item from the data being exported to the consumer.

public PyBuffer.Pointer
getPointer(int...
multidimensional index at which to position the pointer
indices
)

Overrides org.python.core.buffer.SimpleBuffer.getPointer.

Implements org.python.core.PyBuffer.getPointer.

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

Return a structure describing the position in a byte array of a single item from the data being exported to the consumer, in the case that array may be multi-dimensional.

public String
toString()

Overrides org.python.core.buffer.SimpleBuffer.toString.

Implements org.python.core.PyBuffer.toString.

The toString() method of a SimpleStringBuffer simply produces the underlying String.

Field Detail

bufStringback to summary
private String bufString

The string backing this PyBuffer. A substitute for buf until we can no longer avoid creating it.

Constructor Detail

SimpleStringBufferback to summary
public SimpleStringBuffer(int flags, BufferProtocol obj, String bufString)

Provide an instance of SimpleStringBuffer meeting the consumer's expectations as expressed in the flags argument.

Parameters
flags:int

consumer requirements

obj:BufferProtocol

exporting object (or null)

bufString:String

storing the implementation of the object

Method Detail

byteAtImplback to summary
public final byte byteAtImpl(int index)

Overrides org.python.core.buffer.BaseArrayBuffer.byteAtImpl.

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

Retrieve the byte at the given index in the underlying storage treated as a flat sequence of bytes. This byte-index will have been computed from the item index (which may have been multi-dimensional), taking into account index0, shape, strides, and the item size. The caller is responsible for validating the original item-index and raising (typically) an IndexOutOfBoundsException. Misuse of this method may still result in unchecked exceptions characteristic of the storage implementation.

This method uses String#charAt(int) rather than create an actual byte buffer.

Parameters
index:int

byte-index of location to retrieve

Returns:byte

the byte at byteIndex

Annotations
@Override

byteIndexback to summary
public final int byteIndex(int index)

Overrides org.python.core.buffer.SimpleBuffer.byteIndex.

Implements org.python.core.PyBuffer.byteIndex.

Doc from org.python.core.buffer.SimpleBuffer.byteIndex.

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 byteAt(int)) and the byte-index (as used with the ByteBuffer returned by getNIOByteBuffer()). See byteIndex(int[]) for discussion of the multi-dimensional case.

In SimpleBuffer the calculation is specialised for one dimension, no striding, and an item size of 1.

In SimpleStringBuffer we can simply return the argument.

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

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

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.

This method uses String#charAt(int) rather than create an actual byte buffer.

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

ensureHaveBytesback to summary
private void ensureHaveBytes()

This method creates an actual byte array from the underlying String if none yet exists.

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.

This method creates an actual byte array from the underlying String if none yet exists.

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

Implements org.python.core.PyBuffer.getBufferSlice.

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

Equivalent to getBufferSlice(int, int, int, int) with stride 1.

The SimpleStringBuffer implementation avoids creation of a byte buffer.

Parameters
flags:int

specifying features demanded and the navigational capabilities of the consumer

start:int

index in the current buffer

count:int

number of items in the required slice

Returns:PyBuffer

a buffer representing the slice

Annotations
@Override

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

Overrides org.python.core.buffer.SimpleBuffer.getBufferSlice.

Implements org.python.core.PyBuffer.getBufferSlice.

Doc from org.python.core.buffer.SimpleBuffer.getBufferSlice.

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.

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

The SimpleStringBuffer implementation creates an actual byte buffer.

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

Implements org.python.core.PyBUF.getLen.

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

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

This method uses String#length() rather than create an actual byte buffer.

Returns:int

Doc from org.python.core.PyBUF.getLen.

the total number of bytes represented.

Annotations
@Override

getNIOByteBufferImplback to summary
protected ByteBuffer getNIOByteBufferImpl()

Overrides org.python.core.buffer.BaseArrayBuffer.getNIOByteBufferImpl.

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

Create a new java.nio.ByteBuffer on the underlying storage, such that positioning this buffer to a particular byte using byteIndex(int) or byteIndex(int[]) positions it at the first byte of the item so indexed.

Annotations
@Override
getPointerback to summary
public PyBuffer.Pointer getPointer(int index)

Overrides org.python.core.buffer.SimpleBuffer.getPointer.

Implements org.python.core.PyBuffer.getPointer.

Doc from org.python.core.PyBuffer.getPointer.

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 position in a byte array of a single item from the data being exported to the consumer. For a one-dimensional contiguous buffer, assuming the following client code where obj has type BufferProtocol:

int k = ... ;
PyBuffer a = obj.getBuffer(PyBUF.FULL);
int itemsize = a.getItemsize();
PyBuffer.Pointer b = a.getPointer(k);
the item with index k is in the array b.storage at index [b.offset] to [b.offset + itemsize - 1] inclusive. And if itemsize==1, the item is simply the byte b.storage[b.offset]

Essentially this is a method for computing the offset of a particular index. The client is free to navigate the underlying buffer b.storage without respecting these boundaries.

This method creates an actual byte array from the underlying String if none yet exists.

Parameters
index:int

in the buffer to position the pointer

Returns:PyBuffer.Pointer

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

Annotations
@SuppressWarnings:deprecation
@Override

getPointerback to summary
public PyBuffer.Pointer getPointer(int... indices)

Overrides org.python.core.buffer.SimpleBuffer.getPointer.

Implements org.python.core.PyBuffer.getPointer.

Doc from org.python.core.PyBuffer.getPointer.

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 position in a byte array of a single item from the data being exported to the consumer, in the case that array may be multi-dimensional. For a 3-dimensional contiguous buffer, assuming the following client code where obj has type BufferProtocol:

int i, j, k;
// ... calculation that assigns i, j, k
PyBuffer a = obj.getBuffer(PyBUF.FULL);
int itemsize = a.getItemsize();
PyBuffer.Pointer b = a.getPointer(i,j,k);
the item with index [i,j,k] is in the array b.storage at index [b.offset] to [b.offset + itemsize - 1] inclusive. And if itemsize==1, the item is simply the byte b.storage[b.offset]

Essentially this is a method for computing the offset of a particular index. The client is free to navigate the underlying buffer b.storage without respecting these boundaries. If the buffer is non-contiguous, the above description is still valid (since a multi-byte item must itself be contiguously stored), but in any additional navigation of b.storage[] to other items, the client must use the shape, strides and sub-offsets provided by the API. Normally one starts b = a.getBuf() in order to establish the offset of index [0,...,0].

This method creates an actual byte array from the underlying String if none yet exists.

Parameters
indices:int[]

multidimensional index at which to position the pointer

Returns:PyBuffer.Pointer

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

Annotations
@SuppressWarnings:deprecation
@Override

toStringback to summary
public String toString()

Overrides org.python.core.buffer.SimpleBuffer.toString.

Implements org.python.core.PyBuffer.toString.

The toString() method of a SimpleStringBuffer simply produces the underlying 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 SimpleStringBuffer.SimpleStringView

extends SimpleStringBuffer
Class Inheritance

A SimpleStringBuffer.SimpleStringView represents a contiguous subsequence of another SimpleStringBuffer.

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
SimpleStringView(PyBuffer
buffer which will be acquired and must be released ultimately
root
,
int
the request flags of the consumer that requested the slice
flags
,
String
becomes the buffer of bytes for this object
bufString
)

Construct a slice of a SimpleStringBuffer.

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.SimpleStringBuffer:
byteAtImplbyteIndexcopyTogetBufgetBufferSlicegetBufferSlicegetLengetNIOByteBufferImplgetPointergetPointertoString

Field Detail

rootback to summary
pack-priv PyBuffer root

The buffer on which this is a slice view

Constructor Detail

SimpleStringViewback to summary
public SimpleStringView(PyBuffer root, int flags, String bufString)

Construct a slice of a SimpleStringBuffer.

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

bufString:String

becomes the buffer of bytes for this object

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