public class Transcoder
extends java.lang.Object
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 and Description |
---|
Transcoder(java.lang.String sourceEncoding,
java.lang.String targetEncoding,
java.io.OutputStream outputStream)
Construct an instnace
|
Transcoder(java.lang.String sourceEncoding,
java.lang.String targetEncoding,
java.io.OutputStream outputStream,
int byteBufferSize,
int charBufferSize,
boolean autoFlush)
Construct an instance
|
Modifier and Type | Method and Description |
---|---|
void |
flush()
Flush the output encoding writer
|
java.nio.charset.CharsetDecoder |
getDecoder()
Return the CharsetDecoder that is in use by this Transcoder.
|
java.nio.charset.CharsetEncoder |
getEncoder()
Return the CharsetEncoder that is in use by this Transcoder.
|
java.nio.charset.Charset |
getSourceCharset()
Answer the source CharSet
|
java.nio.charset.Charset |
getTargetCharset()
Answer the target Charset
|
boolean |
isAutoFlush()
Answer true if autoFlush is enabled
|
void |
reset()
Rest the encoding state
|
void |
setAutoFlush(boolean autoFlush)
Enable or disable autoflush
|
void |
translate(byte[] bytes)
Transcode a byte array
|
void |
translate(byte[] bytes,
int offset,
int length)
Transcode a byte array at a given offset and length
|
public Transcoder(java.lang.String sourceEncoding, java.lang.String targetEncoding, java.io.OutputStream outputStream) throws java.nio.charset.UnsupportedCharsetException
sourceEncoding
- the source codepage nametargetEncoding
- the target codepage nameoutputStream
- the target OutputStreamjava.nio.charset.UnsupportedCharsetException
public Transcoder(java.lang.String sourceEncoding, java.lang.String targetEncoding, java.io.OutputStream outputStream, int byteBufferSize, int charBufferSize, boolean autoFlush) throws java.nio.charset.UnsupportedCharsetException
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 streamjava.nio.charset.UnsupportedCharsetException
public void translate(byte[] bytes) throws java.io.IOException
java.io.IOException
public void translate(byte[] bytes, int offset, int length) throws java.io.IOException
java.io.IOException
public java.nio.charset.CharsetDecoder getDecoder()
public java.nio.charset.CharsetEncoder getEncoder()
public void flush() throws java.io.IOException
java.io.IOException
public void reset() throws java.io.IOException
java.io.IOException
public boolean isAutoFlush()
public void setAutoFlush(boolean autoFlush)
public java.nio.charset.Charset getSourceCharset()
public java.nio.charset.Charset getTargetCharset()