ServerJS/Encodings: Difference between revisions

Jump to navigation Jump to search
→‎API: fix example, add alternative API proposal
(→‎API: fix example, add alternative API proposal)
Line 65: Line 65:
   Converter = require('encodings').Converter
   Converter = require('encodings').Converter
   converter = new Converter('iso-8859-1', 'utf-32')
   converter = new Converter('iso-8859-1', 'utf-32')
   converter.push(input) // input is a ByteString
   converter.write(input) // input is a ByteString
   output = converter.get() // output is a ByteString
   output = converter.read() // output is a ByteString
  converter.close()
 
=== Alternative: Converter ===
 
There is another way the Converter interface could work.
 
; [Constructor] Converter(from, to)
: Where from and to are the encoding names.
; [Method] push(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 (as a ByteString) as much as could be converted.
; [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:
 
  Converter = require('encodings').Converter
  converter = new Converter('iso-8859-1', 'utf-32')
  output = converter.push(input) // input is a ByteString, and output too
  converter.close()
62

edits

Navigation menu