ServerJS/Encodings: Difference between revisions

→‎Class: Converter: => Transcoder
(→‎API: move write/read API into separate page)
(→‎Class: Converter: => Transcoder)
Line 41: Line 41:
: encodingCheckerFunction takes the encoding name as a parameter and returns true-ish if the encoding should be listed. Regexes should also be supported. If the parameter is missing, returns all supported encodings.
: encodingCheckerFunction takes the encoding name as a parameter and returns true-ish if the encoding should be listed. Regexes should also be supported. If the parameter is missing, returns all supported encodings.


=== Class: Converter ===
=== Class: Transcoder ===


There also should be a class enc.Converter for more advanced conversion (transcoding).
There also should be a class enc.Transcoder for general transcoding conversion (between ByteStrings or ByteArrays).


; [Constructor] Converter(from, to)
; [Constructor] Transcoder(from, to)
: Where from and to are the encoding names.
: Where from and to are the encoding names.
; [Method] push(byteStringOrArray[, outputByteArray])
; [Method] push(byteStringOrArray[, outputByteArray])
Line 58: Line 58:
Example:
Example:


   Converter = require('encodings').Converter
   Transcoder = require('encodings').Transcoder
   converter = new Converter('iso-8859-1', 'utf-32')
   transcoder = new Transcoder('iso-8859-1', 'utf-32')
   output = converter.push(input) // input is a ByteString, and output too
   output = transcoder.push(input) // input is a ByteString, and output too
   converter.close()
   transcoder.close()


Another example:
Another example:


   converter = new Converter('utf-32', 'utf-8')
   transcoder = new Transcoder('utf-32', 'utf-8')
   output = new ByteArray()
   output = new ByteArray()
   while (input = readSomeByteFromSomewhere()) {
   while (input = readSomeByteFromSomewhere()) {
           converter.push(input, output)
           transcoder.push(input, output)
   }
   }
   converter.close()
   transcoder.close()
   // output is the complete conversion of all the input chunks concatenated now
   // output is the complete conversion of all the input chunks concatenated now


(See [[ServerJS/Encodings/OldClass]] for another API.)
(See [[ServerJS/Encodings/OldClass]] for another API.)
62

edits