From CommonJS Spec Wiki
< Encodings‎ | A(Redirected from Encodings/OldClass)
Jump to: navigation, search

Class: Converter

There also should be a class enc.Converter for more advanced conversion.

Please note that the interface is one way. Despite the fact that it has two methods, it's one way. It only supports encoding, no decoding! Multiple people have got that wrong when glancing over it, so it's emphasised here.

[Constructor] Converter(from, to)
Where from and to are the encoding names.
[Method] write(byteStringOrArray)
Convert input from a ByteString or ByteArray. The results are stored in an internal buffer, and also those parts of byteStringOrArray that could not be converted (for multi-byte encodings, in a separate buffer).
Returns nothing.
[Method] read([byteArray,] [maximumSize])
Read maximumSize bytes or as many bytes as available out of the internal buffer. If byteArray is specified, the data is appended to that ByteArray.
Returns a ByteString if byteArray is not specified, or byteArray itself otherwise.
[Method] close()
Close the stream. Throws an exception if there was a conversion error (specifically, a partial multibyte character).
Returns nothing and takes no parameters.

Example usage:

 Converter = require('encodings').Converter
 converter = new Converter('iso-8859-1', 'utf-32')
 converter.write(input) // input is a ByteString
 output = converter.read() // output is a ByteString