Top Description Inners Fields Constructors Methods
org.python.core

public Class codecs

Additional top-level class in compilation unit: StringSubsequenceIterator.

extends Object
Class Inheritance
Imports
java.nio.charset.Charset, java.util.ArrayList, .Iterator, org.python.core.util.StringUtil, org.python.modules._codecs

This class implements the codec registry and utility methods supporting codecs, such as those providing the standard replacement strategies ("ignore", "backslashreplace", etc.). The _codecs module relies heavily on apparatus implemented here, and therefore so does the Python codecs module (in Lib/codecs.py). It corresponds approximately to CPython's Python/codecs.c.

The class also contains the inner methods of the standard Unicode codecs, available for transcoding of text at the Java level. These also are exposed through the _codecs module. In CPython, the implementations are found in Objects/unicodeobject.c.

Since
Jython 2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
private static class
pack-priv static enum
codecs.UTF7Error

Enumeration of the error causes during decoding of the Base64 segment of UTF-7

Field Summary

Modifier and TypeField and Description
private static final String
B64_CHARS

Look-up for the Base64 encoded byte [0..0x3f]

public static final String
private static final byte[]
BASE64_VALUE

Look-up table to convert ASCII byte to 6-bit Base64 value, -1 if not Base64, and -2 if special terminator '-'.

private static char[]
pack-priv static char[]
public static final String
private static char
public static final String
private static final byte[]
private static byte[]
public static final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static StringBuilder
backslashreplace(int start, int end, String toReplace)

public static PyObject
private static void
backslashreplace_internal(int start, int end, String object, StringBuilder replacement)

public static int

Returns:

absolute resume position.
calcNewPosition
(int
of byte buffer being decoded
size
,
PyObject
returned from error handler
errorTuple
)

Given the return from some codec error handler (invoked while encoding or decoding), which specifies a resume position, and the length of the input being encoded or decoded, check and interpret the resume position.

private static void
checkErrorHandlerReturn(String
name of the error policy
errors
,
PyObject
from error handler
replacementSpec
)

Check thet the error handler returned a tuple (replacement_unicode, resume_index).

public static PyObject

Returns:

Unicode string decoded from bytes
decode
(PyString
bytes to be decoded
v
,
String
name of encoding (to look up in codec registry)
encoding
,
String
error policy name (e.g. "ignore", "replace")
errors
)

Decode the bytes v using the codec registered for the encoding.

public static PyObject

Returns:

must be a tuple (replacement_unicode, resume_index)
decoding_error
(String
name of the error policy (or null meaning "strict")
errors
,
String
name of encoding that encountered the error
encoding
,
String
bytes being decoded
toDecode
,
int
index of first byte it couldn't decode
start
,
int
index+1 of last byte it couldn't decode (usually becomes the resume point)
end
,
String
contribution to error message if any
reason
)

Invoke a user-defined error-handling mechanism, for errors encountered during decoding, as registered through register_error(String, PyObject).

private static int

Returns:

the number of bits (<6) not emitted
emitBase64
(StringBuilder
output byte array
v
,
long
holding the bits
buffer
,
int
the number of bits held (<=64)
n
)

Helper for PyUnicode_EncodeUTF7(String, boolean, boolean, String) to emit 6-bit Base64 code units as bytes to the output.

private static void
emitBase64Padded(StringBuilder
output byte array
v
,
long
holding the bits
buffer
,
int
the number of bits held (<=60)
n
)

Helper for PyUnicode_EncodeUTF7(String, boolean, boolean, String) to emit 6-bit Base64 code units as bytes to the output.

private static int

Returns:

the number of bits not emitted (<32 unless error)
emitCodePoints
(StringBuilder
output UTF-16 sequence
v
,
long
holding the bits
buffer
,
int
the number of bits held (<=64)
n
)

Helper for PyUnicode_DecodeUTF7Stateful(String, String, int[]) to emit characters that accumulated as UTF-16 code units in the bits of a long integer (from Base64 decoding, say).

private static codecs.UTF7Error

Returns:

the diagnosis
emitCodePointsDiagnosis
(long
holding the bits
buffer
,
int
the number of bits held (<=64)
n
)

Helper for PyUnicode_DecodeUTF7Stateful(String, String, int[]) to diagnose what went wrong in emitCodePoints(StringBuilder, long, int).

public static String

Returns:

bytes object encoding v
encode
(PyString
unicode string to be encoded
v
,
String
name of encoding (to look up in codec registry)
encoding
,
String
error policy name (e.g. "ignore")
errors
)

Encode v using the codec registered for the encoding.

private static boolean

Returns:

true if c should be encoded as itself
ENCODE_DIRECT
(int
code point of the character
c
,
boolean
true if characters in "set O" may be encoded as themselves
directO
,
boolean
true if whitespace characters may be encoded as themselves
directWS
)

Determine whether, in the UTF-7 encoder, this character should be encoded as itself.

public static PyObject

Returns:

must be a tuple (replacement_unicode, resume_index)
encoding_error
(String
name of the error policy (or null meaning "strict")
errors
,
String
name of encoding that encountered the error
encoding
,
String
unicode string being encoded
toEncode
,
int
index of first char it couldn't encode
start
,
int
index+1 of last char it couldn't encode (usually becomes the resume point)
end
,
String
contribution to error message if any
reason
)

Invoke a user-defined error-handling mechanism, for errors encountered during encoding, as registered through register_error(String, PyObject).

private static int
FROM_BASE64(int c)

Is c the code point of a Base64 character?

public static String
public static PyObject
ignore_errors(PyObject[] args, String[] kws)

public static int

Returns:

the resume position: index of next byte to decode
insertReplacementAndGetResume
(StringBuilder
output buffer of unicode (as UTF-16) that the codec is building
partialDecode
,
String
name of the error policy (or null meaning "strict")
errors
,
String
name of encoding that encountered the error
encoding
,
String
bytes being decoded
toDecode
,
int
index of first byte it couldn't decode
start
,
int
index+1 of last byte it couldn't decode (usually becomes the resume point)
end
,
String
contribution to error message if any
reason
)

Handler for errors encountered during decoding, adjusting the output buffer contents and returning the correct position to resume decoding (if the handler does not simply raise an exception).

private static boolean
public static PyTuple
lookup(String encoding)

public static PyObject
lookup_error(String handlerName)

private static String
public static String
PyUnicode_DecodeASCII(String str, int size, String errors)

public static PyUnicode
private static String
PyUnicode_DecodeIntLimited(String str, int size, String errors, String encoding, int limit)

public static String
PyUnicode_DecodeLatin1(String str, int size, String errors)

public static PyUnicode
public static String
public static String

Returns:

unicode result (as UTF-16 Java String)
PyUnicode_DecodeUTF7
(String
input represented as String (Jython PyString convention)
bytes
,
String
error policy name (e.g. "ignore", "replace")
errors
)

Decode completely a sequence of bytes representing the UTF-7 encoded form of a Unicode string and return the (Jython internal representation of) the unicode object.

public static String

Returns:

unicode result (as UTF-16 Java String)
PyUnicode_DecodeUTF7Stateful
(String
input represented as String (Jython PyString convention)
bytes
,
String
error policy name (e.g. "ignore", "replace")
errors
,
int[]
returns number of bytes consumed in element 0, or is null if a "final" call
consumed
)

Decode (perhaps partially) a sequence of bytes representing the UTF-7 encoded form of a Unicode string and return the (Jython internal representation of) the unicode object, and amount of input consumed.

public static String
public static String
PyUnicode_DecodeUTF8Stateful(String str, String errors, int[] consumed)

public static String
PyUnicode_EncodeASCII(String str, int size, String errors)

public static String
private static String
PyUnicode_EncodeIntLimited(String str, int size, String errors, String encoding, int limit)

public static String
PyUnicode_EncodeLatin1(String str, int size, String errors)

public static String
public static String
PyUnicode_EncodeRawUnicodeEscape(String str, String errors, boolean modifed)

public static String

Returns:

bytes representing the encoded unicode string
PyUnicode_EncodeUTF7
(String
to be encoded
unicode
,
boolean
true if characters in "set O" should be translated to base64
base64SetO
,
boolean
true if white-space characters should be translated to base64
base64WhiteSpace
,
String
error policy name (e.g. "ignore", "replace")
errors
)

Encode a UTF-16 Java String as UTF-7 bytes represented by the low bytes of the characters in a String.

public static String
public static void
register(PyObject search_function)

public static void
public static PyObject
public static void
public static PyObject
strict_errors(PyObject[] args, String[] kws)

private static char
TO_BASE64(int n)

What is the Base64 encoded byte for (the bottom 6 bits of) n?

private static PyUnicode
private static PyException
public static StringBuilder
xmlcharrefreplace(int start, int end, String toReplace)

public static PyObject
private static void
xmlcharrefreplace_internal(int start, int end, String object, StringBuilder replacement)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait