Module ibm.jzos
Package com.ibm.jzos

Class Transcoder

java.lang.Object
com.ibm.jzos.Transcoder

public class Transcoder extends Object
This class provides conversions from a source Charset (Codepage) to a target Charset. A byte array encoded in the source Charset is accepted as input. These bytes are decoded to unicode with the source Charset decoder. The resulting CharBuffer is written to the supplied outputStream through an OutputStreamWriter encoded with the target Charset.

This class is not threadsafe; the decoder and ByteBuffer used for processing the input are stateful. This is due to the fact that the source byte array may be encoded with a multi-byte Charset (e.g. UTF-8), and a call to translate() may deliver a byte array that has an incomplete byte sequence. Subsequent call(s) to translate() are expected to deliver the remaining bytes in the sequence. Once these bytes arrive, the incomplete sequence can be decoded. A direct ByteBuffer is used (rather than wrapping the input byte array) so that the incomplete byte sequence can be preserved between calls.

The errorActions taken for both the decoder and encoder are CodingErrorAction.REPLACE. The default replacement will be used, but can be overridden.

  • Constructor Details

    • Transcoder

      public Transcoder(String sourceEncoding, String targetEncoding, OutputStream outputStream) throws UnsupportedCharsetException
      Construct an instance
      Parameters:
      sourceEncoding - the source codepage name
      targetEncoding - the target codepage name
      outputStream - the target OutputStream
      Throws:
      UnsupportedCharsetException
    • Transcoder

      public Transcoder(String sourceEncoding, String targetEncoding, OutputStream outputStream, int byteBufferSize, int charBufferSize, boolean autoFlush) throws UnsupportedCharsetException
      Construct an instance
      Parameters:
      sourceEncoding - the source codepage name
      targetEncoding - the target codepage name
      outputStream - the target OutputStream
      byteBufferSize - the byte/input buffer size
      charBufferSize - the character/output buffersize
      autoFlush - if true, autoflush the output stream
      Throws:
      UnsupportedCharsetException
  • Method Details

    • translate

      public void translate(byte[] bytes) throws IOException
      Transcode a byte array
      Throws:
      IOException
    • translate

      public void translate(byte[] bytes, int offset, int length) throws IOException
      Transcode a byte array at a given offset and length
      Throws:
      IOException
    • getDecoder

      public CharsetDecoder getDecoder()
      Return the CharsetDecoder that is in use by this Transcoder. Access is provided so that the CodingErrorActions can be changed or a new unicode replacement string supplied. By default, the decoder REPLACEs malformed input and unmappable bytes with the its default replacement string.
    • getEncoder

      public CharsetEncoder getEncoder()
      Return the CharsetEncoder that is in use by this Transcoder. Access is provided so that the CodingErrorActions can be changed or a new replacement byte sequence supplied. By default, the encoder REPLACEs malformed input and unmappable characters with the its default replacement byte sequence.
    • flush

      public void flush() throws IOException
      Flush the output encoding writer
      Throws:
      IOException
    • reset

      public void reset() throws IOException
      Rest the encoding state
      Throws:
      IOException
    • isAutoFlush

      public boolean isAutoFlush()
      Answer true if autoFlush is enabled
    • setAutoFlush

      public void setAutoFlush(boolean autoFlush)
      Enable or disable autoflush
    • getSourceCharset

      public Charset getSourceCharset()
      Answer the source CharSet
    • getTargetCharset

      public Charset getTargetCharset()
      Answer the target Charset