Top Description Fields Constructors Methods
org.python.modules

public Class binascii

extends Object
Class Inheritance
Imports
org.python.core.ArgParser, .BufferProtocol, .Py, .PyBUF, .PyBuffer, .PyException, .PyObject, .PyString, .PyStringMap, .PyTuple, .PyUnicode, org.python.core.buffer.SimpleStringBuffer

The binascii.java module contains a number of methods to convert between binary and various ASCII-encoded binary representations. Normally, you will not use these modules directly but use wrapper modules like uu or hexbin instead, this module solely exists because bit-manipulation of large amounts of data is slow in Python.

The binascii.java module defines the following functions:

a2b_uu (string)
Convert a single line of uuencoded data back to binary and return the binary data. Lines normally contain 45 (binary) bytes, except for the last line. Line data may be followed by whitespace.
b2a_uu (data)
Convert binary data to a line of ASCII characters, the return value is the converted line, including a newline char. The length of data should be at most 45.
a2b_base64 (string)
Convert a block of base64 data back to binary and return the binary data. More than one line may be passed at a time.
b2a_base64 (data)
Convert binary data to a line of ASCII characters in base64 coding. The return value is the converted line, including a newline char. The length of data should be at most 57 to adhere to the base64 standard.
a2b_hqx (string)
Convert binhex4 formatted ASCII data to binary, without doing RLE-decompression. The string should contain a complete number of binary bytes, or (in case of the last portion of the binhex4 data) have the remaining bits zero.
rledecode_hqx (data)
Perform RLE-decompression on the data, as per the binhex4 standard. The algorithm uses 0x90 after a byte as a repeat indicator, followed by a count. A count of 0 specifies a byte value of 0x90. The routine returns the decompressed data, unless data input data ends in an orphaned repeat indicator, in which case the Incomplete exception is raised.
rlecode_hqx (data)
Perform binhex4 style RLE-compression on data and return the result.
b2a_hqx (data)
Perform hexbin4 binary-to-ASCII translation and return the resulting string. The argument should already be RLE-coded, and have a length divisible by 3 (except possibly the last fragment).
crc_hqx (data, crc)
Compute the binhex4 crc value of data, starting with an initial crc and returning the result.
Error
Exception raised on errors. These are usually programming errors.
Incomplete
Exception raised on incomplete data. These are usually not programming errors, but may be handled by reading a little more data and trying again.
The module is a line-by-line conversion of the original binasciimodule.c written by Jack Jansen, except that all mistakes and errors are my own.
Author
Finn Bock, bckfnn@pipmail.dknet.dk
Version
binascii.java,v 1.6 1999/02/20 11:37:07 fb Exp

Field Summary

Modifier and TypeField and Description
public static String
public static PyString
public static PyString
public static final PyString
public static PyString
public static PyString
public static PyString
public static PyString
public static final PyString
public static PyString
public static PyString
public static PyString
public static PyString
public static PyString
private static int
private static char
pack-priv static int[]
private static int[]
private static byte
public static final PyObject
private static byte
private static char[]
public static final PyObject
private static char
private static byte
private static byte[]
private static byte[]
private static char[]
private static char[]
private static final char[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static PyString
a2b_base64(PyObject text)

Convert a block of base64 data back to binary and return the binary data.

public static PyString
a2b_hex(PyObject hexstr)

public static PyTuple
a2b_hqx(PyObject text)

Convert binhex4 formatted ASCII data to binary, without doing RLE-decompression.

public static PyString
a2b_qp(PyObject[] arg, String[] kws)

public static PyString
a2b_uu(PyObject text)

Convert a single line of uuencoded data back to binary and return the binary data.

private static PyException

Returns:

TypeError to throw
argMustBeBytes
(String
name of function of error (or could be any text)
f
,
PyObject
argument provided from which actual type will be reported
arg
)

Convenience method providing the exception when an argument is not the expected type, in the format "f() argument 1 must bytes or unicode, not type(arg)."

public static PyString
b2a_base64(PyObject data)

Convert binary data to a line of ASCII characters in base64 coding.

public static PyString
public static PyString
b2a_hqx(PyObject data)

Perform hexbin4 binary-to-ASCII translation and return the resulting string.

public static PyString
b2a_qp(PyObject[] arg, String[] kws)

public static PyString
b2a_uu(PyObject data)

Convert binary data to a line of ASCII characters, the return value is the converted line, including a newline char.

private static int
binascii_find_valid(PyBuffer b, int offset, int num)

Finds & returns the (num+1)th valid character for base64, or -1 if none.

public static int
public static int
crc32(PyObject data, long long_crc)

public static int
crc_hqx(PyObject data, int crc)

Compute the binhex4 crc value of data, starting with an initial crc and returning the result.

public static PyObject
private static PyBuffer

Returns:

a byte-buffer view of argument (or default decoding if unicode)
getByteBuffer
(PyObject
an object with the buffer protocol (or unicode)
text
)

We use this when the argument given to a conversion method is to be interpreted as text.

private static boolean
getIntFlagAsBool(ArgParser ap, int index, int dflt, String errMsg)

public static PyString
hexlify(PyObject argbuf)

private static StringBuilder
qpEscape(StringBuilder sb, char c)

public static PyString
rlecode_hqx(PyObject data)

Perform binhex4 style RLE-compression on data and return the result.

public static PyString
rledecode_hqx(PyObject data)

Perform RLE-decompression on the data, as per the binhex4 standard.

public static PyString
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

__doc__back to summary
public static String __doc__
__doc__a2b_base64back to summary
public static PyString __doc__a2b_base64
__doc__a2b_hqxback to summary
public static PyString __doc__a2b_hqx
__doc__a2b_qpback to summary
public static final PyString __doc__a2b_qp
__doc__a2b_uuback to summary
public static PyString __doc__a2b_uu
__doc__b2a_base64back to summary
public static PyString __doc__b2a_base64
__doc__b2a_hexback to summary
public static PyString __doc__b2a_hex
__doc__b2a_hqxback to summary
public static PyString __doc__b2a_hqx
__doc__b2a_qpback to summary
public static final PyString __doc__b2a_qp
__doc__b2a_uuback to summary
public static PyString __doc__b2a_uu
__doc__crc_hqxback to summary
public static PyString __doc__crc_hqx
__doc__rlecode_hqxback to summary
public static PyString __doc__rlecode_hqx
__doc__rledecode_hqxback to summary
public static PyString __doc__rledecode_hqx
a2b_hex$docback to summary
public static PyString a2b_hex$doc
BASE64_MAXBINback to summary
private static int BASE64_MAXBIN
BASE64_PADback to summary
private static char BASE64_PAD
crc_32_tabback to summary
pack-priv static int[] crc_32_tab
crctab_hqxback to summary
private static int[] crctab_hqx
DONEback to summary
private static byte DONE
Errorback to summary
public static final PyObject Error
FAILback to summary
private static byte FAIL
hexdigitback to summary
private static char[] hexdigit
Incompleteback to summary
public static final PyObject Incomplete
RUNCHARback to summary
private static char RUNCHAR
SKIPback to summary
private static byte SKIP
table_a2b_base64back to summary
private static byte[] table_a2b_base64
table_a2b_hqxback to summary
private static byte[] table_a2b_hqx
table_b2a_base64back to summary
private static char[] table_b2a_base64
table_b2a_hqxback to summary
private static char[] table_b2a_hqx
upper_hexdigitback to summary
private static final char[] upper_hexdigit

Constructor Detail

binasciiback to summary
public binascii()

Method Detail

a2b_base64back to summary
public static PyString a2b_base64(PyObject text)

Convert a block of base64 data back to binary and return the binary data. More than one line may be passed at a time.

a2b_hexback to summary
public static PyString a2b_hex(PyObject hexstr)
a2b_hqxback to summary
public static PyTuple a2b_hqx(PyObject text)

Convert binhex4 formatted ASCII data to binary, without doing RLE-decompression. The string should contain a complete number of binary bytes, or (in case of the last portion of the binhex4 data) have the remaining bits zero.

a2b_qpback to summary
public static PyString a2b_qp(PyObject[] arg, String[] kws)
a2b_uuback to summary
public static PyString a2b_uu(PyObject text)

Convert a single line of uuencoded data back to binary and return the binary data. Lines normally contain 45 (binary) bytes, except for the last line. Line data may be followed by whitespace.

argMustBeBytesback to summary
private static PyException argMustBeBytes(String f, PyObject arg)

Convenience method providing the exception when an argument is not the expected type, in the format "f() argument 1 must bytes or unicode, not type(arg)."

Parameters
f:String

name of function of error (or could be any text)

arg:PyObject

argument provided from which actual type will be reported

Returns:PyException

TypeError to throw

b2a_base64back to summary
public static PyString b2a_base64(PyObject data)

Convert binary data to a line of ASCII characters in base64 coding. The return value is the converted line, including a newline char.

b2a_hexback to summary
public static PyString b2a_hex(PyObject data)
b2a_hqxback to summary
public static PyString b2a_hqx(PyObject data)

Perform hexbin4 binary-to-ASCII translation and return the resulting string. The argument should already be RLE-coded, and have a length divisible by 3 (except possibly the last fragment).

b2a_qpback to summary
public static PyString b2a_qp(PyObject[] arg, String[] kws)
b2a_uuback to summary
public static PyString b2a_uu(PyObject data)

Convert binary data to a line of ASCII characters, the return value is the converted line, including a newline char. The length of data should be at most 45.

binascii_find_validback to summary
private static int binascii_find_valid(PyBuffer b, int offset, int num)

Finds & returns the (num+1)th valid character for base64, or -1 if none.

crc32back to summary
public static int crc32(PyObject bp)
crc32back to summary
public static int crc32(PyObject data, long long_crc)
crc_hqxback to summary
public static int crc_hqx(PyObject data, int crc)

Compute the binhex4 crc value of data, starting with an initial crc and returning the result.

exceptionNamespaceback to summary
public static PyObject exceptionNamespace()
getByteBufferback to summary
private static PyBuffer getByteBuffer(PyObject text) throws ClassCastException

We use this when the argument given to a conversion method is to be interpreted as text. If it is byte-like, the bytes are used unchanged, assumed in the "intended" character set. It may be a PyUnicode, in which case the it will be decoded to bytes using the default encoding (sys.getdefaultencoding().

Parameters
text:PyObject

an object with the buffer protocol (or unicode)

Returns:PyBuffer

a byte-buffer view of argument (or default decoding if unicode)

Exceptions
ClassCastException:
where the text object does not implement the buffer protocol

getIntFlagAsBoolback to summary
private static boolean getIntFlagAsBool(ArgParser ap, int index, int dflt, String errMsg)
hexlifyback to summary
public static PyString hexlify(PyObject argbuf)
qpEscapeback to summary
private static StringBuilder qpEscape(StringBuilder sb, char c)
rlecode_hqxback to summary
public static PyString rlecode_hqx(PyObject data)

Perform binhex4 style RLE-compression on data and return the result.

rledecode_hqxback to summary
public static PyString rledecode_hqx(PyObject data)

Perform RLE-decompression on the data, as per the binhex4 standard. The algorithm uses 0x90 after a byte as a repeat indicator, followed by a count. A count of 0 specifies a byte value of 0x90. The routine returns the decompressed data, unless data input data ends in an orphaned repeat indicator, in which case the Incomplete exception is raised.

unhexlifyback to summary
public static PyString unhexlify(PyObject argbuf)