Top Description Inners Fields Constructors Methods
org.python.core

public Class PyBytes

extends AbstractList<Integer>
implements CraftedPyObject
Class Inheritance
All Implemented Interfaces
org.python.core.CraftedPyObject
Imports
java.lang.invoke.MethodHandles, java.nio.ByteBuffer, java.util.AbstractList, .Arrays, .Iterator, .Spliterator, .Spliterators, java.util.function.IntConsumer, java.util.stream.IntStream, .StreamSupport, org.python.core.PyObjectUtil.NoConversion, .PySequence.Delegate, .PySlice.Indices, org.python.core.stringlib.ByteArrayBuilder

The Python bytes object.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class
PyBytes.BytesDelegate

A class to act as the delegate implementing __getitem__ and other index-related operations.

private class
PyBytes.BytesSpliterator

Inner class defining the return type of PyBytes#spliterator().

private static class
PyBytes.ByteStore

A consumer of primitive int values that stores them in an array given it at construction.

Field Summary

Modifier and TypeField and Description
private PyBytes.BytesDelegate
delegate

Helper to implement __getitem__ and other index-related operations.

pack-priv static final PyBytes
private static final byte[]
public static final PyType
TYPE

The type of Python object this class implements.

protected final PyType
type

The Python type of this instance.

pack-priv final byte[]
value

The elements of the bytes.

Inherited from java.util.AbstractList:
modCount

Constructor Summary

AccessConstructor and Description
private
PyBytes(PyType
sub-type for which this is being created
type
,
boolean
if true, the array becomes the implementation array, otherwise the constructor takes a copy.
iPromiseNotToModify
,
byte[]
the array of the bytes to contain
value
)

As PyBytes(byte[]) for Python sub-class specifying type.

protected
PyBytes(PyType
sub-type for which this is being created
type
,
byte[]
of the bytes
value
)

As PyBytes(byte[]) for Python sub-class specifying type.

protected
PyBytes(PyType
sub-type for which this is being created
type
,
int...
of the bytes
value
)

As PyBytes(int.

public
PyBytes(byte[]
of the bytes
value
)

Construct a Python bytes object from bytes treated as unsigned.

public
PyBytes(int...
of the bytes
value
)

Construct a Python bytes object from Java ints treated as unsigned.

public
PyBytes(ByteArrayBuilder
of the bytes
value
)

Construct a Python bytes object from a ByteArrayBuilder.

Method Summary

Modifier and TypeMethod and Description
private Object
__add__(Object other)

private Object
private int
private Object
private Object
private Object
pack-priv static PyBytes.BytesDelegate

Returns:

adapted to a sequence
adapt
(Object
to wrap or return
v
)

Adapt a Python object to a sequence of Java int values or throw an exception.

public byte[]

Returns:

array of byte
asByteArray
()

Return the contents of the object as an array of of byte.

public PySequence.OfInt

Returns:

sequence of int
asSequence
()

Expose the contents of the object as a read-only sequence of int.

private static PyBytes
public Integer
get(int
index of the element to return
i
)

Implements abstract java.util.AbstractList.get.

Implements java.util.List.get.

Returns the element at the specified position in this list.
public ByteBuffer

Returns:

a Java NIO buffer
getNIOByteBuffer
()

Expose the contents of the object as a read-only ByteBuffer.

public PyType
getType()

Implements org.python.core.CraftedPyObject.getType.

The Python type of this object.
public Iterator<Integer>
iterator()

Overrides java.util.AbstractList.iterator.

Implements java.util.List.iterator, java.util.Collection.iterator.

Returns an iterator over the elements in this list in proper sequence.
public int
size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.List.size, java.util.Collection.size.

Returns the number of elements in this list.
private static PyBytes

Returns:

the bytes
wrap
(byte[]
to wrap as a bytes
value
)

Unsafely wrap an array of bytes as a PyBytes.

Inherited from java.util.AbstractList:
addaddaddAllclearequalshashCodeindexOflastIndexOflistIteratorlistIteratorremoveremoveRangesetsubList

Field Detail

delegateback to summary
private PyBytes.BytesDelegate delegate

Helper to implement __getitem__ and other index-related operations.

EMPTYback to summary
pack-priv static final PyBytes EMPTY
EMPTY_BYTE_ARRAYback to summary
private static final byte[] EMPTY_BYTE_ARRAY
TYPEback to summary
public static final PyType TYPE

The type of Python object this class implements.

typeback to summary
protected final PyType type

The Python type of this instance.

valueback to summary
pack-priv final byte[] value

The elements of the bytes.

Constructor Detail

PyBytesback to summary
private PyBytes(PyType type, boolean iPromiseNotToModify, byte[] value)

As PyBytes(byte[]) for Python sub-class specifying type. Construct an instance of PyBytes or a sub-class, from a given array of bytes, with the option to re-use that array as the implementation. If the actual array is is re-used the caller must give up ownership and never modify it after the call. See concat(PySequenceInterface) for a correct use.

Parameters
type:PyType

sub-type for which this is being created

iPromiseNotToModify:boolean

if true, the array becomes the implementation array, otherwise the constructor takes a copy.

value:byte[]

the array of the bytes to contain

PyBytesback to summary
protected PyBytes(PyType type, byte[] value)

As PyBytes(byte[]) for Python sub-class specifying type.

Parameters
type:PyType

sub-type for which this is being created

value:byte[]

of the bytes

PyBytesback to summary
protected PyBytes(PyType type, int... value)

As PyBytes(int...) for Python sub-class specifying type.

Parameters
type:PyType

sub-type for which this is being created

value:int[]

of the bytes

PyBytesback to summary
public PyBytes(byte[] value)

Construct a Python bytes object from bytes treated as unsigned.

Parameters
value:byte[]

of the bytes

PyBytesback to summary
public PyBytes(int... value)

Construct a Python bytes object from Java ints treated as unsigned.

Parameters
value:int[]

of the bytes

PyBytesback to summary
public PyBytes(ByteArrayBuilder value)

Construct a Python bytes object from a ByteArrayBuilder. This provides a safe, zero-copy way to supply the contents from an algorithm.

Parameters
value:ByteArrayBuilder

of the bytes

Method Detail

__add__back to summary
private Object __add__(Object other) throws Throwable
Annotations
@SuppressWarnings:unused
__getitem__back to summary
private Object __getitem__(Object item) throws Throwable
Annotations
@SuppressWarnings:unused
__len__back to summary
private int __len__()
Annotations
@SuppressWarnings:unused
__mul__back to summary
private Object __mul__(Object n) throws Throwable
Annotations
@SuppressWarnings:unused
__radd__back to summary
private Object __radd__(Object other) throws Throwable
Annotations
@SuppressWarnings:unused
__rmul__back to summary
private Object __rmul__(Object n) throws Throwable
Annotations
@SuppressWarnings:unused
adaptback to summary
pack-priv static PyBytes.BytesDelegate adapt(Object v) throws NoConversion

Adapt a Python object to a sequence of Java int values or throw an exception. If the method throws the special exception NoConversion, the caller must catch it and deal with it, perhaps by throwing a TypeError. A binary operation will normally return Py#NotImplemented in that case.

Note that implementing PySequence.OfInt is not enough, which other types may, but be incompatible in Python.

Parameters
v:Object

to wrap or return

Returns:PyBytes.BytesDelegate

adapted to a sequence

Exceptions
NoConversion:
if v is not a Python str
asByteArrayback to summary
public byte[] asByteArray()

Return the contents of the object as an array of of byte.

Returns:byte[]

array of byte

asSequenceback to summary
public PySequence.OfInt asSequence()

Expose the contents of the object as a read-only sequence of int.

Returns:PySequence.OfInt

sequence of int

concatBytesback to summary
private static PyBytes concatBytes(PySequence.OfInt v, PySequence.OfInt w) throws OutOfMemoryError
getback to summary
public Integer get(int i)

Implements abstract java.util.AbstractList.get.

Implements java.util.List.get.

Doc from java.util.AbstractList.get.

Returns the element at the specified position in this list.

Parameters
i:int

Doc from java.util.List.get.

index of the element to return

Returns:Integer

Doc from java.util.List.get.

the element at the specified position in this list

Annotations
@Override
getNIOByteBufferback to summary
public ByteBuffer getNIOByteBuffer()

Expose the contents of the object as a read-only ByteBuffer. This is temporary API until we implement the buffer interface.

Returns:ByteBuffer

a Java NIO buffer

getTypeback to summary
public PyType getType()

Implements org.python.core.CraftedPyObject.getType.

Doc from org.python.core.CraftedPyObject.getType.

The Python type of this object.

Returns:PyType

type of this object

Annotations
@Override
iteratorback to summary
public Iterator<Integer> iterator()

Overrides java.util.AbstractList.iterator.

Implements java.util.List.iterator, java.util.Collection.iterator.

Doc from java.util.AbstractList.iterator.

Returns an iterator over the elements in this list in proper sequence.

Returns:Iterator<Integer>

an iterator over the elements in this list in proper sequence

Annotations
@Override
sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.List.size, java.util.Collection.size.

Doc from java.util.List.size.

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:int

the number of elements in this list

Annotations
@Override
wrapback to summary
private static PyBytes wrap(byte[] value)

Unsafely wrap an array of bytes as a PyBytes. The caller must not hold a reference to the argument array (and definitely not manipulate the contents).

Parameters
value:byte[]

to wrap as a bytes

Returns:PyBytes

the bytes

org.python.core back to summary

pack-priv Class PyBytes.BytesDelegate

extends PySequence.Delegate<Integer, PyBytes>
implements OfInt
Class Inheritance
All Implemented Interfaces
org.python.core.PySequence.OfInt, org.python.core.PySequence.Of, java.lang.Iterable

A class to act as the delegate implementing __getitem__ and other index-related operations. By inheriting PySequence.Delegate in this inner class, we obtain boilerplate implementation code for slice translation and range checks. We need only specify the work specific to PyBytes instances.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv PyBytes
add(Object
the right operand
ow
)

Implements abstract org.python.core.PySequence.Delegate.add.

Inner implementation of __add__ on the client sequence, called by __add__(Object).
public IntStream
asIntStream()

Implements org.python.core.PySequence.OfInt.asIntStream.

Provide a stream specialised to primitive int.
public int
compareTo(PySequence.Delegate<Integer, PyBytes>
the object to be compared.
other
)

Implements java.lang.Comparable.compareTo.

Compares this object with the specified object for order.
pack-priv boolean

Returns:

whether values equal
equals
(PyBytes.BytesDelegate
another
b
)

Compare for equality with a sequence.

public int
getInt(int
index
i
)

Implements org.python.core.PySequence.OfInt.getInt.

Get the int item from the sequence at a given index i.
public Integer
getItem(int
index of item to return
i
)

Implements abstract org.python.core.PySequence.Delegate.getItem.

Inner implementation of __getitem__, called by __getitem__(Object) when its argument is an integer.
public PyBytes
getSlice(PySlice.Indices
containing [start, stop, step, count] of the slice to return
slice
)

Implements abstract org.python.core.PySequence.Delegate.getSlice.

Inner implementation of __getitem__, called by __getitem__(Object) when its argument is a PySlice.
public PyType
getType()

Implements abstract org.python.core.PySequence.Delegate.getType.

Provide the type of client sequence, primarily for use in error messages e.g. "TYPE index out of bounds".
public Iterator<Integer>
iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator over elements of type T.
public int
length()

Implements abstract org.python.core.PySequence.Delegate.length.

Implements org.python.core.PySequence.Of.length.

The length of this sequence.
pack-priv PyBytes
radd(Object
the left operand
ov
)

Implements abstract org.python.core.PySequence.Delegate.radd.

Inner implementation of __radd__ on the client sequence, called by __radd__(Object).
pack-priv PyBytes
repeat(int
the number of repetitions
n
)

Implements abstract org.python.core.PySequence.Delegate.repeat.

Inner implementation of __mul__ on the client sequence, called by __mul__(Object).
public Spliterator.OfInt
spliterator()

Overrides default org.python.core.PySequence.Of.spliterator.

Implements org.python.core.PySequence.OfInt.spliterator.

Creates a Spliterator over the elements described by this Iterable.
Inherited from org.python.core.PySequence.Delegate:
__add____delitem____getitem____mul____radd____setitem__adjustGetadjustSetboundedIndexdelItemdelSlicegetTypeNameindexrangeIndexErrorsetItemsetSlice

Constructor Detail

BytesDelegateback to summary
pack-priv BytesDelegate()

Method Detail

addback to summary
pack-priv PyBytes add(Object ow) throws OutOfMemoryError, NoConversion, Throwable

Implements abstract org.python.core.PySequence.Delegate.add.

Doc from org.python.core.PySequence.Delegate.add.

Inner implementation of __add__ on the client sequence, called by __add__(Object).

The implementation of this method is responsible for validating the argument. If an __add__ is being attempted between incompatible types it should return Py#NotImplemented, or throw a NoConversion exception, which will cause __add__ to return NotImplemented.

Parameters
ow:Object

the right operand

Returns:PyBytes

concatenation self+ow or Py#NotImplemented

Annotations
@Override
Exceptions
OutOfMemoryError:
when allocating the result fails. __add__ will raise a Python OverflowError.
NoConversion:
(optionally) when the client does not support the type of ow.
Throwable:
from other causes in the implementation
asIntStreamback to summary
public IntStream asIntStream()

Implements org.python.core.PySequence.OfInt.asIntStream.

Doc from org.python.core.PySequence.OfInt.asIntStream.

Provide a stream specialised to primitive int.

Returns:IntStream

a stream of primitive int

Annotations
@Override
compareToback to summary
public int compareTo(PySequence.Delegate<Integer, PyBytes> other)

Implements java.lang.Comparable.compareTo.

Doc from java.lang.Comparable.compareTo.

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

The implementor must ensure signum(x.compareTo(y)) == -signum(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception if and only if y.compareTo(x) throws an exception.)

The implementor must also ensure that the relation is transitive: (x.compareTo(y) > 0 && y.compareTo(z) > 0) implies x.compareTo(z) > 0.

Finally, the implementor must ensure that x.compareTo(y)==0 implies that signum(x.compareTo(z)) == signum(y.compareTo(z)), for all z.

Parameters
other:PySequence.Delegate<Integer, PyBytes>

the object to be compared.

Returns:int

a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Annotations
@Override
equalsback to summary
pack-priv boolean equals(PyBytes.BytesDelegate b)

Compare for equality with a sequence. This is a little simpler than compareTo.

Parameters
b:PyBytes.BytesDelegate

another

Returns:boolean

whether values equal

getIntback to summary
public int getInt(int i)

Implements org.python.core.PySequence.OfInt.getInt.

Doc from org.python.core.PySequence.OfInt.getInt.

Get the int item from the sequence at a given index i.

Parameters
i:int

index

Returns:int

item at index i.

Annotations
@Override
getItemback to summary
public Integer getItem(int i)

Implements abstract org.python.core.PySequence.Delegate.getItem.

Doc from org.python.core.PySequence.Delegate.getItem.

Inner implementation of __getitem__, called by __getitem__(Object) when its argument is an integer. The argument is the equivalent int, adjusted and checked by adjustGet(int).

Parameters
i:int

index of item to return

Returns:Integer

the element from the client sequence

Annotations
@Override
getSliceback to summary
public PyBytes getSlice(PySlice.Indices slice)

Implements abstract org.python.core.PySequence.Delegate.getSlice.

Doc from org.python.core.PySequence.Delegate.getSlice.

Inner implementation of __getitem__, called by __getitem__(Object) when its argument is a PySlice. The argument is the return from PySlice#getIndices(int), which is guaranteed to be range-compatible with the sequence length length().

Parameters
slice:PySlice.Indices

containing [start, stop, step, count] of the slice to return

Returns:PyBytes

the slice from the client sequence

Annotations
@Override
getTypeback to summary
public PyType getType()

Implements abstract org.python.core.PySequence.Delegate.getType.

Doc from org.python.core.PySequence.Delegate.getType.

Provide the type of client sequence, primarily for use in error messages e.g. "TYPE index out of bounds".

Returns:PyType

the type of client sequence

Annotations
@Override
iteratorback to summary
public Iterator<Integer> iterator()

Implements java.lang.Iterable.iterator.

Doc from java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

Returns:Iterator<Integer>

an Iterator.

Annotations
@Override
lengthback to summary
public int length()

Implements abstract org.python.core.PySequence.Delegate.length.

Implements org.python.core.PySequence.Of.length.

Doc from org.python.core.PySequence.Of.length.

The length of this sequence.

Returns:int

the length of this sequence

Annotations
@Override
raddback to summary
pack-priv PyBytes radd(Object ov) throws OutOfMemoryError, NoConversion, Throwable

Implements abstract org.python.core.PySequence.Delegate.radd.

Doc from org.python.core.PySequence.Delegate.radd.

Inner implementation of __radd__ on the client sequence, called by __radd__(Object).

The implementation of this method is responsible for validating the argument. If an __radd__ is being attempted between incompatible types it should return Py#NotImplemented, or throw a NoConversion exception, which will cause __radd__ to return NotImplemented.

Parameters
ov:Object

the left operand

Returns:PyBytes

concatenation ov+self or Py#NotImplemented

Annotations
@Override
Exceptions
OutOfMemoryError:
when allocating the result fails. __radd__ will raise a Python OverflowError.
NoConversion:
(optionally) when the client does not support the type of ov.
Throwable:
from other causes in the implementation
repeatback to summary
pack-priv PyBytes repeat(int n) throws OutOfMemoryError, Throwable

Implements abstract org.python.core.PySequence.Delegate.repeat.

Doc from org.python.core.PySequence.Delegate.repeat.

Inner implementation of __mul__ on the client sequence, called by __mul__(Object).

Parameters
n:int

the number of repetitions

Returns:PyBytes

repetition self*n

Annotations
@Override
Exceptions
OutOfMemoryError:
when allocating the result fails. __mul__ will raise a Python OverflowError.
Throwable:
from other causes in the implementation
spliteratorback to summary
public Spliterator.OfInt spliterator()

Overrides default org.python.core.PySequence.Of.spliterator.

Implements org.python.core.PySequence.OfInt.spliterator.

Doc from org.python.core.PySequence.Of.spliterator.

Creates a Spliterator over the elements described by this Iterable. The characteristics SIZED and SUBSIZED are additionally reported.

Returns:Spliterator.OfInt

Doc from java.lang.Iterable.spliterator.

a Spliterator over the elements described by this Iterable.

Annotations
@Override
org.python.core back to summary

private Class PyBytes.BytesSpliterator

extends AbstractIntSpliterator
Class Inheritance

Inner class defining the return type of PyBytes#spliterator(). We need this only because tryAdvance deals in java ints, while our array is byte[]. There is no ready-made Spliterator.OfByte, and if there were, it would return signed values.

Field Summary

Modifier and TypeField and Description
pack-priv static final int
private int
i

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public boolean
tryAdvance(IntConsumer
The action
action
)

Implements java.util.Spliterator.OfInt.tryAdvance.

If a remaining element exists, performs the given action on it, returning true; else returns false.
Inherited from java.util.Spliterators.AbstractIntSpliterator:
characteristicsestimateSizetrySplit

Field Detail

flagsback to summary
pack-priv static final int flags
iback to summary
private int i

Constructor Detail

BytesSpliteratorback to summary
pack-priv BytesSpliterator()

Method Detail

tryAdvanceback to summary
public boolean tryAdvance(IntConsumer action)

Implements java.util.Spliterator.OfInt.tryAdvance.

Doc from java.util.Spliterator.OfPrimitive.tryAdvance.

If a remaining element exists, performs the given action on it, returning true; else returns false. If this Spliterator is ORDERED the action is performed on the next element in encounter order. Exceptions thrown by the action are relayed to the caller.

Subsequent behavior of a spliterator is unspecified if the action throws an exception.

Parameters
action:IntConsumer

The action

Returns:boolean

false if no remaining elements existed upon entry to this method, else true.

Annotations
@Override
org.python.core back to summary

private Class PyBytes.ByteStore

extends Object
implements IntConsumer
Class Inheritance
All Implemented Interfaces
java.util.function.IntConsumer

A consumer of primitive int values that stores them in an array given it at construction.

Field Summary

Modifier and TypeField and Description
private final byte[]
b

private int
i

Constructor Summary

AccessConstructor and Description
pack-priv
ByteStore(byte[] bytes, int start)

Method Summary

Modifier and TypeMethod and Description
public void
accept(int
the input argument
value
)

Implements java.util.function.IntConsumer.accept.

Performs this operation on the given argument.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bback to summary
private final byte[] b
iback to summary
private int i

Constructor Detail

ByteStoreback to summary
pack-priv ByteStore(byte[] bytes, int start)

Method Detail

acceptback to summary
public void accept(int value)

Implements java.util.function.IntConsumer.accept.

Doc from java.util.function.IntConsumer.accept.

Performs this operation on the given argument.

Parameters
value:int

the input argument

Annotations
@Override