脚本开发

模块 ringo/encoding

对字符编码和解码的低级支持。 它使用包 java.nio 和 java.nio.charset 作为基础操作。

Example

var enc = new Encoder('utf-8');
enc.encode('I \u2665 JS').encode('I \u2665 JS');
var bs = enc.toByteString();

// prints 'I ♥ JSI ♥ JS'
console.log(bs.decodeToString('utf-8'));

var dec = new Decoder('ISO-8859-1');
var ba = new ByteArray([246, 228, 252]);

// prints öäü
console.log(dec.decode(ba));

Class Decoder

Instance Methods

Instance Properties

Class Encoder

Instance Methods

Instance Properties


Decoder (charset, strict, capacity)

创建一个新的解码器,将 ByteString 或 ByteArray 转换为字符串。

Example

// throws an Error: MALFORMED[1]
var dec = new Decoder('ASCII', true);
dec.decode(new ByteArray([246, 228, 252, 999999]));

// replaces 999999 with a substitutions character ���
var dec = new Decoder('ASCII');
dec.decode(new ByteArray([246, 228, 252, 999999]));

Parameters

String charset

the charset name

Boolean strict

if true, unmappable characters stop the decoder and throw an exception, otherwise malformed input is replaced with a substitution character

Number capacity

initial capacity for the input byte buffer and output character buffer. The output buffer's size depends on the average bytes used per character by the charset.


Decoder.prototype. clear ()

清除字符缓冲区。

Example

dec.decode(someByteArray);
dec.toString(); // returns the decoded string
dec.clear();
dec.toString(); // returns ''

Decoder.prototype. close ()

关闭解码器以进一步输入。如果再次调用 decode() ,则封闭解码器会抛出 java.nio.BufferOverflowException。

Returns

Decoder

the decoder


Decoder.prototype. decode (bytes, start, end)

解码给定缓冲区的字节。

Parameters

binary.Binary bytes

a ByteString or ByteArray

Number start

The start index, or 0 if undefined

Number end

the end index, or bytes.length if undefined


Decoder.prototype. hasPendingInput ()

检查所有字节是否已经解码或者是否有未决输入。

Returns

Boolean

true if there not all bytes are decoded, false otherwise


Decoder.prototype. length

字符缓冲区的长度,它在内部使用 Java 原始字符。缓冲区中的每个字符都是一个 16 位的 Unicode 字符。

Example

// an emoji in 4 raw bytes
var ba = new ByteArray([0xF0,0x9F,0x98,0x98]);

// a UTF-8 based decoder
var dec = new Decoder("UTF-8");

// prints 😘
console.log(dec.decode(ba));

// prints "2 chars vs. 4 bytes"
console.log(dec.length + " chars vs. " + ba.length + " bytes");

Decoder.prototype. read ()

读取整个流并将其作为字符串返回。此方法仅在解码器具有连接的流时才有用。

Returns

String

the decoded string

See


Decoder.prototype. readFrom (source)

设置要从中读取的源流。使用 io 流是从普通二进制 ByteArray 或 ByteString 对象读取的替代方法。

Example

var stream = new MemoryStream();
stream.write(...); // write some bytes into the stream
stream.position = 0; // reset the pointer

var dec = new Decoder('ASCII');
dec.readFrom(stream); // connect the stream with the decoder
dec.read(); // returns the stream's content as string

Parameters

io.Stream source

the source stream


Decoder.prototype. readLine (includeNewline)

逐行读取流并将其作为字符串返回。此方法仅在解码器具有连接的流时才有用。

Parameters

Boolean includeNewline

if true, the newline character is included in the result, otherwise not

Returns

String

the decoded string or null if stream is empty

See


Decoder.prototype. toString ()

返回解码的字符串。

Returns

String

the decoded string


Encoder (charset, strict, capacity)

创建一个新的Encoder 将字符串转换为二进制 ByteString 或 ByteArray。

Parameters

String charset

the charset name

Boolean strict

if true, unmappable characters stop the decoder and throw an exception, otherwise malformed input is replaced with a substitution character

Number capacity

initial capacity for the input character buffer and output byte buffer. The binary buffer's size depends on the average bytes used per character by the charset.


Encoder.prototype. clear ()

清除字节缓冲区。

Returns

Encoder

the cleared encoder


Encoder.prototype. close ()

关闭编码器。

Returns

Encoder

the now closed encoder


Encoder.prototype. encode (string, start, end)

将给定的字符串编码到编码器的二进制缓冲区中。

Example

// this will only encode 'e' and 'f'
enc.encode("abcdef", 4, 6);

Parameters

String string

the string to encode

Number start

optional index of the first character to encode

Number end

optional index of the character after the last character to encode


Encoder.prototype. length

基础字节缓冲区的长度。


Encoder.prototype. toByteArray ()

将编码的字节转换为 ByteArray。

Returns

ByteArray

the resulting ByteArray


Encoder.prototype. toByteString ()

将编码的字节转换为 ByteString。

Returns

ByteString

the resulting ByteString


Encoder.prototype. toString ()


Encoder.prototype. writeTo (sink)

设置要写入的输出流。使用 io 流作为目标是替代写入普通二进制 ByteArray 或 ByteString 对象。

Parameters

Stream sink

the destination stream