【脚本语言JavaScript】Ringo JS-模块 ringo/encoding

RingoJS是一个强大的JavaScript平台,它基于Mozilla Rhino引擎,允许开发者在服务器端使用JavaScript进行复杂应用开发。RingoJS提供了丰富的模块系统,简化了I/O操作,支持创建命令行工具、Web应用及GUI应用。本文详细介绍RingoJS的模块系统、编码与解码模块,以及如何使用JavaScript进行高效编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ringo 是一个 JavaScript 平台

ECMA JavaScript 规范将该语言描述为面向对象的编程语言,用于在主机环境中执行计算和处理计算对象。每个用 JavaScript 编写的应用程序都需要一个主机环境,它提供特定于环境的对象和 API 来执行 I / O。 Ringo 为 JavaScript 提供了这样一个环境,并附带一组模块以使应用程序开发更容易。由于其作为通用编程语言的特性,JavaScript 可以用来解决各种各样的问题,而 Ringo 可以帮助您这么做。利用 Ringo,编写命令行工具,复杂的 Web 应用程序甚至基于 Java UI 技术的 GUI 应用程序都很容易。

脚本语言如 JavaScript 需要一个引擎来解释和执行程序。 Ringo 没有自己的引擎。相反,它使用 Mozilla Rhino,一种 Java 中的 JavaScript 实现。犀牛的最初发展始于 Netscape 时代,并一直持续到现在。基本思想是将 JavaScript 程序编译为 Java 字节码,Java 字节码可以由 Java 虚拟机(JVM)执行。犀牛还提供了对 Java 标准类库和其他每个 Java 类的轻松访问。这使得将现有的 Java 库集成到新的 JavaScript 应用程序变得很容易。例如:Ringo 不是编写自己的 I / O 系统,而是使用现有的 Java I / O 类,并将它们封装起来以提供从 JavaScript 更容易的访问。

Ringo 在服务器或专用机器上执行 JavaScript,而不是在 Web 浏览器上下文中执行。如果您已经从基于 HTML 的应用程序中了解 JavaScript,则这是主要区别。没有什么像一个窗口对象,你没有一个 DOM 来操纵 HTML 对象。尽管如此,很多事情会像你从浏览器中知道的那样。您可以使用 console.log() 调试到控制台,但也有专用的日志记录模块可用于更复杂的日志记录。

Ringo 最大的优势之一就是模块系统。 Ringo 并没有自己构建代码,而是拥有一个易于使用的模块系统。它基于 CommonJS 模块,这是用于保持代码可互换的服务器端 JavaScript 环境的规范。如果您了解 Node.js 的模块,您还知道如何在 Ringo 中编写模块。一个模块封装了 JavaScript 方法和变量,并将它们与其他模块隔离。

模块 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

Stringcharset

the charset name

Booleanstrict

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

Numbercapacity

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.Binarybytes

a ByteString or ByteArray

Numberstart

The start index, or 0 if undefined

Numberend

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");

See

java.nio.CharBuffer


Decoder.prototype. read ()

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

Returns

String

the decoded string

See

readFrom


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.Streamsource

the source stream

See

io streams


Decoder.prototype. readLine (includeNewline)

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

Parameters

BooleanincludeNewline

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

Returns

String

the decoded string or null if stream is empty

See

readFrom


Decoder.prototype. toString ()

返回解码的字符串。

Returns

String

the decoded string


Encoder (charset, strict, capacity)

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

Parameters

Stringcharset

the charset name

Booleanstrict

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

Numbercapacity

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

Stringstring

the string to encode

Numberstart

optional index of the first character to encode

Numberend

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

Streamsink

the destination stream

See

io streams

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值