public class Py2kBuffer extends PySequence implements BufferProtocol
buffer
type. buffer
is being superseded in
Python 2.7 by memoryview
, and is provided here to support legacy Python code. Use
memoryview
if you can.
buffer
and memoryview
both wrap the same Jython buffer API:
that designed for memoryview
. In CPython, a new C API (which Jython's resembles) was
introduced with memoryview
. Because of this, buffer
and
memoryview
may be supplied as arguments in the same places, and will accept as
arguments the same (one-dimensional byte-array) types. Their behaviour differs as detailed in the
documentation.
PySequence.DefaultIndexDelegate
PyObject.ConversionException
Modifier and Type | Field and Description |
---|---|
static PyType |
TYPE |
delegator
attributes, gcMonitorGlobal, objtype
Constructor and Description |
---|
Py2kBuffer(BufferProtocol object,
int offset,
int size)
Construct a Py2kBuffer from an object supporting the
BufferProtocol . |
Modifier and Type | Method and Description |
---|---|
PyObject |
__add__(PyObject other)
Equivalent to the standard Python __add__ method.
|
int |
__len__()
Equivalent to the standard Python __len__ method.
|
PyObject |
__mul__(PyObject o)
Equivalent to the standard Python __mul__ method.
|
PyString |
__repr__()
Equivalent to the standard Python
__repr__ method. |
PyObject |
__rmul__(PyObject o)
Equivalent to the standard Python __rmul__ method.
|
PyString |
__str__()
Equivalent to the standard Python __str__ method.
|
PyBuffer |
getBuffer(int flags)
Method by which the consumer requests the buffer from the exporter.
|
protected PyString |
getslice(int start,
int stop,
int step)
Returns a slice of elements from this sequence as a PyString.
|
protected PyString |
pyget(int index)
Gets the indexed element of the
buffer as a one byte string. |
void |
pyset(int index,
PyObject value)
Sets the indexed element of the
buffer to the given value, treating the
operation as assignment to a slice of length one. |
protected PyString |
repeat(int count)
buffer*int represents repetition in Python, and returns a str (
bytes ) object. |
protected void |
setslice(int start,
int stop,
int step,
PyObject value)
Sets the given range of elements according to Python slice assignment semantics.
|
java.lang.String |
toString() |
__delitem__, __delslice__, __eq__, __finditem__, __finditem__, __ge__, __getitem__, __getslice__, __gt__, __iter__, __le__, __lt__, __ne__, __nonzero__, __setitem__, __setitem__, __setslice__, __tojava__, boundToSequence, cmp, del, delRange, delslice, fastSequence, isMappingType, isNumberType, isSequenceType, isSubType, runsupportedopMessage, sliceLength, unsupportedopMessage
__abs__, __and__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __call__, __cmp__, __coerce__, __coerce_ex__, __complex__, __contains__, __delattr__, __delattr__, __delete__, __delitem__, __delslice__, __dir__, __div__, __divmod__, __ensure_finalizer__, __findattr__, __findattr__, __findattr_ex__, __finditem__, __float__, __floordiv__, __format__, __get__, __getattr__, __getattr__, __getitem__, __getnewargs__, __getslice__, __hash__, __hex__, __iadd__, __iand__, __idiv__, __idivmod__, __ifloordiv__, __ilshift__, __imod__, __imul__, __index__, __int__, __invert__, __ior__, __ipow__, __irshift__, __isub__, __iternext__, __itruediv__, __ixor__, __long__, __lshift__, __mod__, __neg__, __not__, __oct__, __or__, __pos__, __pow__, __pow__, __radd__, __rand__, __rawdir__, __rdiv__, __rdivmod__, __reduce__, __reduce_ex__, __reduce_ex__, __rfloordiv__, __rlshift__, __rmod__, __ror__, __rpow__, __rrshift__, __rshift__, __rsub__, __rtruediv__, __rxor__, __set__, __setattr__, __setattr__, __setitem__, __setslice__, __sub__, __truediv__, __trunc__, __unicode__, __xor__, _add, _and, _callextra, _cmp, _div, _divmod, _doget, _doget, _doset, _eq, _floordiv, _ge, _gt, _iadd, _iand, _idiv, _idivmod, _ifloordiv, _ilshift, _imod, _imul, _in, _ior, _ipow, _irshift, _is, _isnot, _isub, _itruediv, _ixor, _jcall, _jcallexc, _jthrow, _le, _lshift, _lt, _mod, _mul, _ne, _notin, _or, _pow, _rshift, _sub, _truediv, _unsupportedop, _xor, adaptToCoerceTuple, asDouble, asIndex, asIndex, asInt, asInt, asIterable, asLong, asLong, asName, asName, asString, asString, asStringOrNull, asStringOrNull, bit_length, conjugate, delDict, delType, dispatch__init__, equals, fastGetClass, fastGetDict, finalize, getDict, getJavaProxy, getType, hashCode, impAttr, implementsDescrDelete, implementsDescrGet, implementsDescrSet, invoke, invoke, invoke, invoke, invoke, invoke, isCallable, isDataDescr, isIndex, isInteger, mergeClassDict, mergeDictAttr, mergeListAttr, noAttributeError, object___subclasshook__, readonlyAttributeError, setDict, setType
public static final PyType TYPE
public Py2kBuffer(BufferProtocol object, int offset, int size)
BufferProtocol
. The
buffer
takes no lease on the PyBuffer
at present, but for each
action performed obtains a new one and releases it. (Major difference from
memoryview
.) Note that when size=-1
is given, the buffer reflects
the changing size of the underlying object.object
- the object on which this is to be a buffer.offset
- into the array exposed by the object (0 for start).size
- of the slice or -1 for all of the object.public int __len__()
PyObject
public PyString __repr__()
PyObject
__repr__
method. Each sub-class of
PyObject
is likely to re-define this method to provide for its own reproduction.public PyString __str__()
PyObject
PyObject
) calls PyObject.__repr__()
, making it unnecessary to override
__str__
in sub-classes of PyObject
where both forms are the same. A
common choice is to provide the same implementation to __str__
and
toString
, for consistency in the printed form of objects between Python and
Java.public PyObject __add__(PyObject other)
buffer
implements this as concatenation and returns a
str
(PyString
) result.public PyObject __mul__(PyObject o)
buffer
it returns a str
containing the buffer
contents n
times.public PyObject __rmul__(PyObject o)
buffer
it returns a str
containing the buffer
contents n
times.public PyBuffer getBuffer(int flags)
PyBuffer.release()
on the buffer it obtained, or PyBuffer.close()
using
try-with-resources, since some objects alter their behaviour while buffers are exported.
The PyBuffer
returned from this method is provided directly by the underlying object
on which this buffer was constructed, taking account of the slicing arguments (offset and
size), if these were given when the buffer was constructed.
getBuffer
in interface BufferProtocol
flags
- specifying features demanded and the navigational capabilities of the consumerprotected PyString pyget(int index)
buffer
as a one byte string. This is an
extension point called by PySequence in its implementation of PySequence.__getitem__(org.python.core.PyObject)
. It is
guaranteed by PySequence that the index is within the bounds of the buffer
.pyget
in class PySequence
index
- index of the element to get.protected PyString getslice(int start, int stop, int step)
getslice
in class PySequence
start
- the position of the first element.stop
- one more than the position of the last element.step
- the step size.protected PyString repeat(int count)
buffer*int
represents repetition in Python, and returns a str
(
bytes
) object.repeat
in class PySequence
count
- the number of times to repeat this.str
) that many timespublic void pyset(int index, PyObject value) throws PyException
buffer
to the given value, treating the
operation as assignment to a slice of length one. This is different from the same operation
on a byte array, where the assigned value must be an int: here it must have the buffer API
and length one. This is an extension point called by PySequence in its implementation of
PySequence.__setitem__(int, org.python.core.PyObject)
It is guaranteed by PySequence that the index is within the bounds of
the buffer
. Any other clients calling pyset(int, PyObject) must make
the same guarantee.pyset
in class PySequence
index
- index of the element to set.value
- to set this element to, regarded as a buffer of length one unit.PyException
- AttributeError
if value cannot be converted to an integerPyException
- ValueError
if value<0 or value%gt;255protected void setslice(int start, int stop, int step, PyObject value)
a = bytearray(b'abcdefghijklmnopqrst') m = buffer(a) m[2:7] = "ABCDE"Results in
a=bytearray(b'abABCDEhijklmnopqrst')
.
If the step size is one, but stop-start does not match the length of the right-hand-side a ValueError is thrown.
If the step size is not one, and start!=stop, the slice defines a certain number of elements to be replaced. This function is not available in Python 2.7 (but it is in Python 3.3).
a = bytearray(b'abcdefghijklmnopqrst') a[2:12:2] = iter( [65, 66, 67, long(68), "E"] )Results in
a=bytearray(b'abAdBfChDjElmnopqrst')
in Python 3.3.setslice
in class PySequence
start
- the position of the first element.stop
- one more than the position of the last element.step
- the step size.value
- an object consistent with the slice assignment