Class BaseBytes
- java.lang.Object
-
- org.python.core.PyObject
-
- org.python.core.PySequence
-
- org.python.core.BaseBytes
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable<PyInteger>
,java.util.Collection<PyInteger>
,java.util.List<PyInteger>
- Direct Known Subclasses:
PyByteArray
public abstract class BaseBytes extends PySequence implements java.util.List<PyInteger>
Base class for Jythonbytearray
(andbytes
in due course) that provides most of the Java API, including JavaList
behaviour. Attempts to modify the contents through this API will throw aTypeError
if the actual type of the object is not mutable. It is possible for a Java client to treat this class as aList<PyInteger>
, obtaining equivalent functionality to the Python interface in a Java paradigm.Subclasses must define (from
each returning an appropriate concrete type. Mutable subclasses should override:PySequence
):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 aPyObject
as argument, where you might have expected abyte[]
orBaseBytes
, in order to accommodate the full range of types accepted by the Python equivalent: usually, anyPyObject
that implementsBufferProtocol
, 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 ofBaseBytes
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 examplecapitalize()
, defined inBaseBytes
to return a BaseBytes, actually returns aPyByteArray
when applied to abytearray
. Or it may be that the method returns aPyList
of instances of the target type, for examplerpartition(PyObject)
. This is achieved by the sub-class defininggetslice(int, int, int)
andgetBuilder(int)
to return instances of its own type. See the documentation of particular methods for more information.- See Also:
- Serialized Form
-
-
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
Constructors Constructor Description BaseBytes(PyType type)
Constructs a zero-lengthBaseBytes
of explicitly-specified sub-type.BaseBytes(PyType type, int size)
Constructs a zero-filled array of defined size and type.BaseBytes(PyType type, int[] value)
Constructs a byte array of defined type by copying values from int[].
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
__len__()
Equivalent to the standard Python __len__ method.PyObject
__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.void
add(int index, PyInteger element)
boolean
add(PyInteger o)
boolean
addAll(int index, java.util.Collection<? extends PyInteger> c)
boolean
addAll(java.util.Collection<? extends PyInteger> c)
java.lang.String
asString()
Present the bytes of a byte array, with no decoding, as a Java String.BaseBytes
capitalize()
Java API equivalent of Pythoncapitalize()
.void
clear()
boolean
contains(java.lang.Object o)
Returns true if this list contains the specified value.boolean
containsAll(java.util.Collection<?> c)
PyObject
decode()
Decode the byte array to a Unicode string according to the default encoding.PyObject
decode(java.lang.String encoding)
Decode the byte array to a Unicode string according to the specified encoding and default error policy.PyObject
decode(java.lang.String encoding, java.lang.String errors)
Decode the byte array to a Unicode string according to the specified encoding and error policy.boolean
equals(java.lang.Object other)
Test for the equality of (the value of) this byte array to the objectother
.PyInteger
get(int index)
int
hashCode()
int
indexOf(java.lang.Object o)
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()
.java.util.Iterator<PyInteger>
iterator()
int
lastIndexOf(java.lang.Object o)
java.util.ListIterator<PyInteger>
listIterator()
java.util.ListIterator<PyInteger>
listIterator(int index)
BaseBytes
lower()
Java API equivalent of Pythonlower()
.PyTuple
partition(PyObject sep)
Implementation of Pythonpartition(sep)
, returning a 3-tuple of byte arrays (of the same type asthis
).void
pyinsert(int index, PyObject element)
Insert the element (interpreted as a Python byte value) at the given index.PyInteger
remove(int index)
boolean
remove(java.lang.Object o)
boolean
removeAll(java.util.Collection<?> c)
boolean
retainAll(java.util.Collection<?> c)
PyTuple
rpartition(PyObject sep)
Implementation of Pythonrpartition(sep)
, returning a 3-tuple of byte arrays (of the same type asthis
).PyList
rsplit()
Implementation of Pythonrsplit()
, that returns a list of the words in the byte array, using whitespace as the delimiter.PyList
rsplit(PyObject sep)
Implementation of Pythonrsplit(sep)
, that returns a list of the words in the byte array, usingsep
as the delimiter.PyList
rsplit(PyObject sep, int maxsplit)
Implementation of Pythonrsplit(sep, maxsplit)
, that returns a list of the words in the byte array, usingsep
as the delimiter.PyInteger
set(int index, PyInteger element)
int
size()
Number of bytes inbytearray
(orbytes
) object.PyList
split()
Implementation of Pythonsplit()
, that returns a list of the words in the byte array, using whitespace as the delimiter.PyList
split(PyObject sep)
Implementation of Pythonsplit(sep)
, that returns a list of the words in the byte array, usingsep
as the delimiter.PyList
split(PyObject sep, int maxsplit)
Implementation of Pythonsplit(sep, maxsplit)
, that returns a list of the words in the byte array, usingsep
as the delimiter.PyList
splitlines()
Implementation of Pythonsplitlines()
, returning a list of the lines in the byte array, breaking at line boundaries.PyList
splitlines(boolean keepends)
Implementation of Pythonsplitlines(keepends)
, returning a list of the lines in the string, breaking at line boundaries.java.util.List<PyInteger>
subList(int fromIndex, int toIndex)
BaseBytes
swapcase()
Java API equivalent of Pythonswapcase()
.BaseBytes
title()
Java API equivalent of Pythontitle()
.java.lang.Object[]
toArray()
<T> T[]
toArray(T[] a)
BaseBytes
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
-
-
-
-
Constructor Detail
-
BaseBytes
public BaseBytes(PyType type)
Constructs a zero-lengthBaseBytes
of explicitly-specified sub-type.- Parameters:
type
- explicit Jython type
-
BaseBytes
public BaseBytes(PyType type, int size)
Constructs a zero-filled array of defined size and type.- Parameters:
size
- requiredtype
- explicit Jython type
-
BaseBytes
public BaseBytes(PyType type, int[] value)
Constructs a byte array of defined type by copying values from int[].- Parameters:
type
- explicit Jython typevalue
- source of values (and size)
-
-
Method Detail
-
pyinsert
public void pyinsert(int index, PyObject element)
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
public java.lang.String 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
public PyObject 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
public PyObject decode(java.lang.String encoding)
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
public PyObject decode(java.lang.String encoding, java.lang.String errors)
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__
public PyObject __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
public PyTuple partition(PyObject sep)
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
public PyTuple rpartition(PyObject sep)
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
public PyList 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
public PyList rsplit(PyObject sep)
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
public PyList rsplit(PyObject sep, int maxsplit)
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
public PyList 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
public PyList split(PyObject sep)
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
public PyList split(PyObject sep, int maxsplit)
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
public PyList 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
public PyList splitlines(boolean keepends)
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
public BaseBytes 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
public BaseBytes 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
public BaseBytes 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
public BaseBytes 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
public BaseBytes 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
public int intAt(int index) throws PyException
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
public boolean contains(java.lang.Object o)
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
public java.util.Iterator<PyInteger> iterator()
-
toArray
public java.lang.Object[] toArray()
-
toArray
public <T> T[] toArray(T[] a)
-
add
public boolean add(PyInteger o)
-
remove
public boolean remove(java.lang.Object o)
-
containsAll
public boolean containsAll(java.util.Collection<?> c)
-
addAll
public boolean addAll(java.util.Collection<? extends PyInteger> c)
-
addAll
public boolean addAll(int index, java.util.Collection<? extends PyInteger> c)
- Specified by:
addAll
in interfacejava.util.List<PyInteger>
-
removeAll
public boolean removeAll(java.util.Collection<?> c)
-
retainAll
public boolean retainAll(java.util.Collection<?> c)
-
clear
public void clear()
-
equals
public boolean equals(java.lang.Object other)
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>
.- Specified by:
equals
in interfacejava.util.Collection<PyInteger>
- Specified by:
equals
in interfacejava.util.List<PyInteger>
- Overrides:
equals
in classPyObject
- Parameters:
other
- object to compare this byte array to- Returns:
true
if and only if this byte array is equal (in value) toother
- See Also:
List.equals(java.lang.Object)
-
hashCode
public int hashCode()
-
set
public PyInteger set(int index, PyInteger element)
- Specified by:
set
in interfacejava.util.List<PyInteger>
-
add
public void add(int index, PyInteger element)
- Specified by:
add
in interfacejava.util.List<PyInteger>
-
remove
public PyInteger remove(int index)
- Specified by:
remove
in interfacejava.util.List<PyInteger>
-
indexOf
public int indexOf(java.lang.Object o)
- Specified by:
indexOf
in interfacejava.util.List<PyInteger>
-
lastIndexOf
public int lastIndexOf(java.lang.Object o)
- Specified by:
lastIndexOf
in interfacejava.util.List<PyInteger>
-
listIterator
public java.util.ListIterator<PyInteger> listIterator()
- Specified by:
listIterator
in interfacejava.util.List<PyInteger>
-
listIterator
public java.util.ListIterator<PyInteger> listIterator(int index)
- Specified by:
listIterator
in interfacejava.util.List<PyInteger>
-
-