Class BaseBytes
- All Implemented Interfaces:
Serializable
,Iterable<PyInteger>
,Collection<PyInteger>
,List<PyInteger>
- Direct Known Subclasses:
PyByteArray
bytearray
(and bytes
in due course) that provides most of
the Java API, including Java List
behaviour. Attempts to modify the contents through this
API will throw a TypeError
if the actual type of the object is not mutable. It is
possible for a Java client to treat this class as a List<PyInteger>
, obtaining equivalent
functionality to the Python interface in a Java paradigm.
Subclasses must define (from PySequence
):
getslice(int, int, int)
repeat(int)
PySequence.pyset(int, PyObject)
PySequence.setslice(int, int, int, PyObject)
PySequence.del(int)
PySequence.delRange(int, int)
Many of the methods implemented here are inherited or thinly wrapped by PyByteArray
,
which offers them as Java API, or exposes them as Python methods. These prototype Python methods
mostly accept a PyObject
as argument, where you might have expected a byte[]
or
BaseBytes
, in order to accommodate the full range of types accepted by the Python
equivalent: usually, any PyObject
that implements BufferProtocol
, providing a
one-dimensional array of bytes, is an acceptable argument. In the documentation, the reader will
often see the terms "byte array" or "object viewable as bytes" instead of BaseBytes
when
this broader scope is intended.
Where the methods return a BaseBytes
, this is will normally be an instance of the class
of the object on which the method was actually called. For example capitalize()
, defined
in BaseBytes
to return a BaseBytes, actually returns a PyByteArray
when applied
to a bytearray
. Or it may be that the method returns a PyList
of instances of the
target type, for example rpartition(PyObject)
. This is achieved by the sub-class
defining getslice(int, int, int)
and getResult(Builder)
to return instances of
its own type. See the documentation of the particular methods for more information.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.python.core.PyObject
PyObject.ConversionException
-
Field Summary
Fields inherited from class org.python.core.PyObject
gcMonitorGlobal, TYPE
-
Constructor Summary
ConstructorDescriptionConstructs a zero-lengthBaseBytes
of explicitly-specified sub-type.Constructs a zero-filled array of defined size and type.Constructs a byte array of defined type by copying values from int[]. -
Method Summary
Modifier and TypeMethodDescriptionint
__len__()
Equivalent to the standard Python __len__ method.Support for pickling byte arrays: reduce a byte array to the actual type, arguments for (re-)construction of the object, and the dictionary of any user-defined sub-class.void
boolean
boolean
addAll
(int index, Collection<? extends PyInteger> c) boolean
addAll
(Collection<? extends PyInteger> c) asString()
Present the bytes of a byte array, with no decoding, as a Java String.Java API equivalent of Pythoncapitalize()
.void
clear()
boolean
Returns true if this list contains the specified value.boolean
containsAll
(Collection<?> c) decode()
Decode the byte array to a Unicode string according to the default encoding.Decode the byte array to a Unicode string according to the specified encoding and default error policy.Decode the byte array to a Unicode string according to the specified encoding and error policy.boolean
Test for the equality of (the value of) this byte array to the objectother
.get
(int index) int
hashCode()
int
int
intAt
(int index) Return the Python byte (in range 0 to 255 inclusive) at the given index.boolean
isalnum()
Java API equivalent of Pythonisalnum()
.boolean
isalpha()
Java API equivalent of Pythonisalpha()
.boolean
isdigit()
Java API equivalent of Pythonisdigit()
.boolean
isEmpty()
boolean
islower()
Java API equivalent of Pythonislower()
.boolean
isspace()
Java API equivalent of Pythonisspace()
.boolean
istitle()
Java API equivalent of Pythonistitle()
.boolean
isupper()
Java API equivalent of Pythonisupper()
.iterator()
int
listIterator
(int index) lower()
Java API equivalent of Pythonlower()
.Implementation of Pythonpartition(sep)
, returning a 3-tuple of byte arrays (of the same type asthis
).void
Insert the element (interpreted as a Python byte value) at the given index.remove
(int index) boolean
boolean
removeAll
(Collection<?> c) boolean
retainAll
(Collection<?> c) rpartition
(PyObject sep) Implementation of Pythonrpartition(sep)
, returning a 3-tuple of byte arrays (of the same type asthis
).rsplit()
Implementation of Pythonrsplit()
, that returns a list of the words in the byte array, using whitespace as the delimiter.Implementation of Pythonrsplit(sep)
, that returns a list of the words in the byte array, usingsep
as the delimiter.Implementation of Pythonrsplit(sep, maxsplit)
, that returns a list of the words in the byte array, usingsep
as the delimiter.int
size()
Number of bytes inbytearray
(orbytes
) object.split()
Implementation of Pythonsplit()
, that returns a list of the words in the byte array, using whitespace as the delimiter.Implementation of Pythonsplit(sep)
, that returns a list of the words in the byte array, usingsep
as the delimiter.Implementation of Pythonsplit(sep, maxsplit)
, that returns a list of the words in the byte array, usingsep
as the delimiter.Implementation of Pythonsplitlines()
, returning a list of the lines in the byte array, breaking at line boundaries.splitlines
(boolean keepends) Implementation of Pythonsplitlines(keepends)
, returning a list of the lines in the string, breaking at line boundaries.subList
(int fromIndex, int toIndex) swapcase()
Java API equivalent of Pythonswapcase()
.title()
Java API equivalent of Pythontitle()
.Object[]
toArray()
<T> T[]
toArray
(T[] a) upper()
Java API equivalent of Pythonupper()
.Methods inherited from class org.python.core.PySequence
__delitem__, __delslice__, __eq__, __finditem__, __finditem__, __ge__, __getitem__, __getslice__, __gt__, __iter__, __le__, __lt__, __ne__, __nonzero__, __setitem__, __setitem__, __setslice__, __tojava__, isMappingType, isNumberType, isSequenceType
Methods inherited from class org.python.core.PyObject
__abs__, __add__, __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__, __mul__, __neg__, __not__, __oct__, __or__, __pos__, __pow__, __pow__, __radd__, __rand__, __rdiv__, __rdivmod__, __reduce_ex__, __reduce_ex__, __repr__, __rfloordiv__, __rlshift__, __rmod__, __rmul__, __ror__, __rpow__, __rrshift__, __rshift__, __rsub__, __rtruediv__, __rxor__, __set__, __setattr__, __setattr__, __setitem__, __setslice__, __str__, __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, _xor, asDouble, asIndex, asIndex, asInt, asInt, asIterable, asLong, asLong, asName, asName, asString, asStringOrNull, asStringOrNull, bit_length, conjugate, delDict, delType, dispatch__init__, fastGetClass, fastGetDict, getDict, getType, implementsDescrDelete, implementsDescrGet, implementsDescrSet, invoke, invoke, invoke, invoke, invoke, invoke, isCallable, isDataDescr, isIndex, isInteger, noAttributeError, object___subclasshook__, readonlyAttributeError, setDict, setType, toString
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.List
replaceAll, sort, spliterator
-
Constructor Details
-
BaseBytes
Constructs a zero-lengthBaseBytes
of explicitly-specified sub-type.- Parameters:
type
- explicit Jython type
-
BaseBytes
Constructs a zero-filled array of defined size and type.- Parameters:
size
- requiredtype
- explicit Jython type
-
BaseBytes
Constructs a byte array of defined type by copying values from int[].- Parameters:
type
- explicit Jython typevalue
- source of values (and size)
-
-
Method Details
-
pyinsert
Insert the element (interpreted as a Python byte value) at the given index. The default implementation produces a Python TypeError, for the benefit of immutable types. Mutable types must override it.- Parameters:
index
- to insert atelement
- to insert (by value)- Throws:
PyException
-IndexError
if the index is outside the array boundsPyException
-ValueError
if element<0 or element>255PyException
-TypeError
if the subclass is immutable
-
__len__
public int __len__()Description copied from class:PyObject
Equivalent to the standard Python __len__ method. Part of the mapping discipline. -
asString
Present the bytes of a byte array, with no decoding, as a Java String. The bytes are treated as unsigned character codes, and copied to the to the characters of a String with no change in ordinal value. This could also be described as 'latin-1' or 'ISO-8859-1' decoding of the byte array to a String, since this character encoding is numerically equal to Unicode. -
decode
Decode the byte array to a Unicode string according to the default encoding. The returned PyObject should be aPyUnicode
, since the default codec is well-behaved.- Returns:
- object containing the decoded characters
-
decode
Decode the byte array to a Unicode string according to the specified encoding and default error policy. The returned PyObject will usually be aPyUnicode
, but in practice it is whatever thedecode
method of the codec decides.- Parameters:
encoding
- the name of the codec (uses default codec ifnull
)- Returns:
- object containing the decoded characters
-
decode
Decode the byte array to a Unicode string according to the specified encoding and error policy. The returned PyObject will usually be aPyUnicode
, but in practice it is whatever thedecode
method of the codec decides.- Parameters:
encoding
- the name of the codec (uses default codec ifnull
)errors
- the name of the error policy (uses 'strict' ifnull
)- Returns:
- object containing the decoded characters
-
__reduce__
Support for pickling byte arrays: reduce a byte array to the actual type, arguments for (re-)construction of the object, and the dictionary of any user-defined sub-class.- Overrides:
__reduce__
in classPyObject
- Returns:
- PyTuple that is first stage in pickling byte array
-
partition
Implementation of Pythonpartition(sep)
, returning a 3-tuple of byte arrays (of the same type asthis
). Split the string at the first occurrence ofsep
, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty byte arrays.The elements of the
PyTuple
returned by this method are instances of the same actual type asthis
.- Parameters:
sep
- the separator on which to partition this byte array- Returns:
- a tuple of (head, separator, tail)
-
rpartition
Implementation of Pythonrpartition(sep)
, returning a 3-tuple of byte arrays (of the same type asthis
). Split the string at the rightmost occurrence ofsep
, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty byte arrays, followed by the byte array itself.The elements of the
PyTuple
returned by this method are instances of the same actual type asthis
.- Parameters:
sep
- the separator on which to partition this byte array- Returns:
- a tuple of (head, separator, tail)
-
rsplit
Implementation of Pythonrsplit()
, that returns a list of the words in the byte array, using whitespace as the delimiter. Seersplit(PyObject, int)
.The elements of the
PyList
returned by this method are instances of the same actual type asthis
.- Returns:
- PyList of byte arrays that result from the split
-
rsplit
Implementation of Pythonrsplit(sep)
, that returns a list of the words in the byte array, usingsep
as the delimiter. Seersplit(PyObject, int)
for the semantics of the separator.The elements of the
PyList
returned by this method are instances of the same actual type asthis
.- Parameters:
sep
-bytes
, or object viewable as bytes, defining the separator- Returns:
- PyList of byte arrays that result from the split
-
rsplit
Implementation of Pythonrsplit(sep, maxsplit)
, that returns a list of the words in the byte array, usingsep
as the delimiter. Ifmaxsplit
is given, at mostmaxsplit
splits are done (thus, the list will have at mostmaxsplit+1
elements). Ifmaxsplit
is not specified, then there is no limit on the number of splits (all possible splits are made).The semantics of
sep
and maxcount are identical to those ofsplit(sep, maxsplit)
, except that splits are generated from the right (and pushed onto the front of the result list). The result is only different from that ofsplit
ifmaxcount
limits the number of splits. For example,bytearray(b' 1 2 3 ').rsplit()
returns[bytearray(b'1'), bytearray(b'2'), bytearray(b'3')]
, andbytearray(b' 1 2 3 ').rsplit(None, 1)
returns[bytearray(b' 1 2'), bytearray(b'3')]
.
The elements of the
PyList
returned by this method are instances of the same actual type asthis
.- Parameters:
sep
-bytes
, or object viewable as bytes, defining the separatormaxsplit
- maximum number of splits- Returns:
- PyList of byte arrays that result from the split
-
split
Implementation of Pythonsplit()
, that returns a list of the words in the byte array, using whitespace as the delimiter. Seesplit(PyObject, int)
.The elements of the
PyList
returned by this method are instances of the same actual type asthis
.- Returns:
- PyList of byte arrays that result from the split
-
split
Implementation of Pythonsplit(sep)
, that returns a list of the words in the byte array, usingsep
as the delimiter. Seesplit(PyObject, int)
for the semantics of the separator.The elements of the
PyList
returned by this method are instances of the same actual type asthis
.- Parameters:
sep
-bytes
, or object viewable as bytes, defining the separator- Returns:
- PyList of byte arrays that result from the split
-
split
Implementation of Pythonsplit(sep, maxsplit)
, that returns a list of the words in the byte array, usingsep
as the delimiter. Ifmaxsplit
is given, at mostmaxsplit
splits are done. (Thus, the list will have at mostmaxsplit+1
elements). Ifmaxsplit
is not specified, then there is no limit on the number of splits (all possible splits are made).If
sep
is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example,'1,,2'.split(',')
returns['1', '', '2']
). Thesep
argument may consist of multiple characters (for example,'1<>2<>3'.split('<>')
returns['1', '2', '3']
). Splitting an empty string with a specified separator['']
.If
sep
is not specified or isNone
, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with aNone
separator returns[]
. For example,bytearray(b' 1 2 3 ').split()
returns[bytearray(b'1'), bytearray(b'2'), bytearray(b'3')]
, andbytearray(b' 1 2 3 ').split(None, 1)
returns[bytearray(b'1'), bytearray(b'2 3 ')]
.
The elements of the
PyList
returned by this method are instances of the same actual type asthis
.- Parameters:
sep
-bytes
, or object viewable as bytes, defining the separatormaxsplit
- maximum number of splits- Returns:
- PyList of byte arrays that result from the split
-
splitlines
Implementation of Pythonsplitlines()
, returning a list of the lines in the byte array, breaking at line boundaries. Line breaks are not included in the resulting segments.The elements of the
PyList
returned by this method are instances of the same actual type asthis
.- Returns:
- List of segments
-
splitlines
Implementation of Pythonsplitlines(keepends)
, returning a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unlesskeepends
is true.The elements of the
PyList
returned by this method are instances of the same actual type asthis
.- Parameters:
keepends
- if true, include the end of line bytes(s)- Returns:
- PyList of segments
-
isalnum
public boolean isalnum()Java API equivalent of Pythonisalnum()
. This method treats the bytes as US-ASCII code points.- Returns:
- true if all bytes in the array are code points for alphanumerics and there is at least one byte, false otherwise.
-
isalpha
public boolean isalpha()Java API equivalent of Pythonisalpha()
. This method treats the bytes as US-ASCII code points.- Returns:
- true if all bytes in the array are alphabetic and there is at least one byte, false otherwise
-
isdigit
public boolean isdigit()Java API equivalent of Pythonisdigit()
. This method treats the bytes as US-ASCII code points.- Returns:
- true if all bytes in the array are code points for digits and there is at least one byte, false otherwise.
-
islower
public boolean islower()Java API equivalent of Pythonislower()
. This method treats the bytes as US-ASCII code points.- Returns:
- true if all cased bytes in the array are code points for lowercase characters and there is at least one cased byte, false otherwise.
-
isspace
public boolean isspace()Java API equivalent of Pythonisspace()
. This method treats the bytes as US-ASCII code points.- Returns:
- true if all the bytes in the array are code points for whitespace characters and there is at least one byte, false otherwise.
-
istitle
public boolean istitle()Java API equivalent of Pythonistitle()
. This method treats the bytes as US-ASCII code points.- Returns:
- true if the string is a titlecased string and there is at least one cased byte, for example uppercase characters may only follow uncased bytes and lowercase characters only cased ones. Return false otherwise.
-
isupper
public boolean isupper()Java API equivalent of Pythonisupper()
. This method treats the bytes as US-ASCII code points.- Returns:
- true if all cased bytes in the array are code points for uppercase characters and there is at least one cased byte, false otherwise.
-
capitalize
Java API equivalent of Pythoncapitalize()
. This method treats the bytes as US-ASCII code points. TheBaseBytes
returned by this method has the same actual type asthis/self
.- Returns:
- a copy of the array with its first character capitalized and the rest lowercased.
-
lower
Java API equivalent of Pythonlower()
. This method treats the bytes as US-ASCII code points. TheBaseBytes
returned by this method has the same actual type asthis/self
.- Returns:
- a copy of the array with all the cased characters converted to lowercase.
-
swapcase
Java API equivalent of Pythonswapcase()
. This method treats the bytes as US-ASCII code points. TheBaseBytes
returned by this method has the same actual type asthis/self
.- Returns:
- a copy of the array with uppercase characters converted to lowercase and vice versa.
-
title
Java API equivalent of Pythontitle()
. The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result. TheBaseBytes
returned by this method has the same actual type asthis/self
.- Returns:
- a titlecased version of the array where words start with an uppercase character and the remaining characters are lowercase.
-
upper
Java API equivalent of Pythonupper()
. Note thatx.upper().isupper()
might befalse
if the array contains uncased characters. TheBaseBytes
returned by this method has the same actual type asthis/self
.- Returns:
- a copy of the array with all the cased characters converted to uppercase.
-
intAt
Return the Python byte (in range 0 to 255 inclusive) at the given index.- Parameters:
index
- of value in byte array- Returns:
- the integer value at the index
- Throws:
PyException
-IndexError
if the index is outside the array bounds
-
size
public int size()Number of bytes inbytearray
(orbytes
) object. -
isEmpty
public boolean isEmpty() -
contains
Returns true if this list contains the specified value. More formally, returns true if and only if this list contains at least one integer e such that o.equals(PyInteger(e)). -
iterator
-
toArray
-
toArray
public <T> T[] toArray(T[] a) -
add
-
remove
-
containsAll
- Specified by:
containsAll
in interfaceCollection<PyInteger>
- Specified by:
containsAll
in interfaceList<PyInteger>
-
addAll
-
addAll
-
removeAll
-
retainAll
-
clear
public void clear() -
equals
Test for the equality of (the value of) this byte array to the objectother
. In the case whereother
is aPyObject
, the comparison used is the standard Python==
operation throughPyObject
. Whenother
is not aPyObject
, this object acts as aList<PyInteger>
. -
hashCode
public int hashCode() -
get
-
set
-
add
-
remove
-
indexOf
-
lastIndexOf
- Specified by:
lastIndexOf
in interfaceList<PyInteger>
-
listIterator
- Specified by:
listIterator
in interfaceList<PyInteger>
-
listIterator
- Specified by:
listIterator
in interfaceList<PyInteger>
-
subList
-