public class CharsetTranslator extends Object
CharsetTranslator
translate byte streams from one
character encoding to another.
The CharsetDecoder
and CharsetEncoder
that are used to
perform the translation use CodingErrorAction.REPORT
for both
malformed-input and unmappable-character actions. Use
CharsetDecoder
and CharsetEncoder
directly if this
behavior is not desirable.
The useXMLCharRefReplacement(boolean)
feature can be used to enable
replacement of unmappable characters with their XML character reference
equivalents. The replacement occurs on encoding, as characters are written
to the target output stream. This feature is useful when preparing text for
display on the Web.
Unlike CharsetDecoder
and CharsetEncoder
, there is
no support for incremental translation using java.nio
buffers. All
translations are performed as single operations (though reads are buffered
internally, and the size of the internal character buffer can be
controlled).
CharsetTranslator
instances always reset the internal
decoder/encoder before translating. Therefore, it is safe to re-use the same
instance for multiple translation operations.
CharsetTranslator
implements equals(Object)
and
hashCode()
. This allows instances to be cached in a lookup table,
for example.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_BUFFER_SIZE
The default buffer size used when reading from the source input stream.
|
Constructor and Description |
---|
CharsetTranslator(Charset sourceCharset,
Charset targetCharset)
Constructs a new
CharsetTranslator that can translate using
a decoder and encoder from the given source and target Charset s,
respectively. |
CharsetTranslator(String sourceCharsetName,
String targetCharsetName)
Constructs a new
CharsetTranslator that can translate from
the named source encoding to the named target encoding. |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object obj) |
int |
getBufferSize()
Returns the size of the buffer used when reading from the source input
stream.
|
int |
hashCode() |
boolean |
isUsingXMLCharRefReplacement()
Tells whether or not this translator will replace unmappable characters
with their XML character reference equivalents.
|
void |
setBufferSize(int bufferSize)
Sets the size of the buffer used when reading from the source input
stream.
|
Charset |
sourceCharset()
Returns the source charset.
|
Charset |
targetCharset()
Returns the target charset.
|
String |
toString() |
void |
translate(InputStream sourceStream,
OutputStream targetStream)
Translates a stream of bytes from one character encoding to another.
|
CharsetTranslator |
useXMLCharRefReplacement(boolean useXMLCharRefReplacement)
Tells this translator whether or not to use XML character reference
replacements.
|
public static final int DEFAULT_BUFFER_SIZE
The buffer size is expressed as the maximum number of characters that will be read from the source input stream at once.
getBufferSize()
,
setBufferSize(int)
,
Constant Field Valuespublic CharsetTranslator(String sourceCharsetName, String targetCharsetName)
CharsetTranslator
that can translate from
the named source encoding to the named target encoding.sourceCharsetName
- the character encoding used to decode bytes
read from the source input streamtargetCharsetName
- the character encoding used to encode
characters written to the target output streamIllegalCharsetNameException
- if the source or target charset name
is illegalIllegalArgumentException
- if either
sourceCharsetName
or targetCharsetName
is null
UnsupportedCharsetException
- if the current JVM does not support
the named source or target charsetpublic CharsetTranslator(Charset sourceCharset, Charset targetCharset)
CharsetTranslator
that can translate using
a decoder and encoder from the given source and target Charset
s,
respectively.sourceCharset
- the character encoding used to decode bytes read
from the source input streamtargetCharset
- the character encoding used to encode characters
written to the target output streamIllegalArgumentException
- if either sourceCharset
or
targetCharset
is null
public final Charset sourceCharset()
public final Charset targetCharset()
public boolean isUsingXMLCharRefReplacement()
Replacement occurs as characters are written to the target output stream.
true
if this translator will use XML character
reference replacementspublic final CharsetTranslator useXMLCharRefReplacement(boolean useXMLCharRefReplacement)
Replacement occurs as characters are written to the target output stream.
useXMLCharRefReplacement
- true
if unmappable
characters should be replaced with their XML character reference
equivalentspublic int getBufferSize()
public void setBufferSize(int bufferSize)
bufferSize
- the maximum number of characters that will be
read from the source at onceIllegalArgumentException
- if the buffer size is less than 1 (one)public void translate(InputStream sourceStream, OutputStream targetStream) throws IOException
sourceStream
- the stream of bytes to be translatedtargetStream
- the stream to which translated bytes are writtenIOException
- if any reading/decoding/encoding/writing operation
failspublic int hashCode()
The hash code of a CharsetTranslator
is based on the source
charset, target charset, and whether or not XML character reference
replacement is enabled.
hashCode
in class Object
Object.hashCode()
public boolean equals(Object obj)
Two CharsetTranslator
instances are considered equal if,
and only if, each instance is using the same source and target charset
and XML character reference replacement is either enabled
or disabled for both instances at the time of comparison.
equals
in class Object
obj
- the reference object with which to comparetrue
if this translator is the same as
obj
; false
otherwiseObject.equals(java.lang.Object)
public String toString()
toString
in class Object
Object.toString()
Copyright © 2010–2015 Matthew Zipay. All rights reserved.