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 Summary
ConstructorsConstructorDescriptionTranscoder(String sourceEncoding, String targetEncoding, OutputStream outputStream) Construct an instanceTranscoder(String sourceEncoding, String targetEncoding, OutputStream outputStream, int byteBufferSize, int charBufferSize, boolean autoFlush) Construct an instance -
Method Summary
Modifier and TypeMethodDescriptionvoidflush()Flush the output encoding writerReturn the CharsetDecoder that is in use by this Transcoder.Return the CharsetEncoder that is in use by this Transcoder.Answer the source CharSetAnswer the target CharsetbooleanAnswer true if autoFlush is enabledvoidreset()Rest the encoding statevoidsetAutoFlush(boolean autoFlush) Enable or disable autoflushvoidtranslate(byte[] bytes) Transcode a byte arrayvoidtranslate(byte[] bytes, int offset, int length) Transcode a byte array at a given offset and length
-
Constructor Details
-
Transcoder
public Transcoder(String sourceEncoding, String targetEncoding, OutputStream outputStream) throws UnsupportedCharsetException Construct an instance- Parameters:
sourceEncoding- the source codepage nametargetEncoding- the target codepage nameoutputStream- 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 nametargetEncoding- the target codepage nameoutputStream- the target OutputStreambyteBufferSize- the byte/input buffer sizecharBufferSize- the character/output buffersizeautoFlush- if true, autoflush the output stream- Throws:
UnsupportedCharsetException
-
-
Method Details
-
translate
Transcode a byte array- Throws:
IOException
-
translate
Transcode a byte array at a given offset and length- Throws:
IOException
-
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
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
Flush the output encoding writer- Throws:
IOException
-
reset
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
Answer the source CharSet -
getTargetCharset
Answer the target Charset
-