The input byte sequence is provided in a byte buffer or a series of such buffers. The output character sequence is written to a character buffer or a series of such buffers. A decoder should always be used by making the following sequence of method invocations, hereinafter referred to as a decoding operation:
Reset the decoder via the reset
method, unless it
has not been used before;
Invoke the decode
method zero or more times, as
long as additional input may be available, passing false
for the
endOfInput
argument and filling the input buffer and flushing the
output buffer between invocations;
Invoke the decode
method one final time, passing
true
for the endOfInput
argument; and then
Invoke the flush
method so that the decoder can
flush any internal state to the output buffer.
decode
method will decode as many
bytes as possible from the input buffer, writing the resulting characters
to the output buffer. The decode
method returns when more
input is required, when there is not enough room in the output buffer, or
when a decoding error has occurred. In each case a CoderResult
object is returned to describe the reason for termination. An invoker can
examine this object and fill the input buffer, flush the output buffer, or
attempt to recover from a decoding error, as appropriate, and try again.
There are two general types of decoding errors. If the input byte sequence is not legal for this charset then the input is considered malformed. If the input byte sequence is legal but cannot be mapped to a valid Unicode character then an unmappable character has been encountered.
How a decoding error is handled depends upon the action requested for
that type of error, which is described by an instance of the CodingErrorAction
class. The possible error actions are to ignore the erroneous input, report the error to the invoker via
the returned CoderResult
object, or replace the erroneous input with the current value of the
replacement string. The replacement
has the initial value "\uFFFD"
;
its value may be changed via the replaceWith
method.
The default action for malformed-input and unmappable-character errors
is to report them. The
malformed-input error action may be changed via the onMalformedInput
method; the
unmappable-character action may be changed via the onUnmappableCharacter
method.
This class is designed to handle many of the details of the decoding
process, including the implementation of error actions. A decoder for a
specific charset, which is a concrete subclass of this class, need only
implement the abstract decodeLoop
method, which
encapsulates the basic decoding loop. A subclass that maintains internal
state should, additionally, override the implFlush
and
implReset
methods.
Instances of this class are not safe for use by multiple concurrent threads.
ByteBuffer
, CharBuffer
, Charset
, CharsetEncoder
Modifier and Type | Field and Description |
---|---|
private final float | |
private final Charset | |
private CodingErrorAction | |
private final float | |
private String | |
private static final int | |
private static final int | |
private static final int | |
private static final int | |
private int | |
private static String[] | |
private CodingErrorAction |
Access | Constructor and Description |
---|---|
private | CharsetDecoder(Charset
The charset that created this decoder cs, float A positive float value indicating the expected number of
characters that will be produced for each input byte averageCharsPerByte, float A positive float value indicating the maximum number of
characters that will be produced for each input byte maxCharsPerByte, String The initial replacement; must not be replacement)null , must have
non-zero length, must not be longer than maxCharsPerByte,
and must be legalInitializes a new decoder. |
protected | CharsetDecoder(Charset
The charset that created this decoder cs, float A positive float value indicating the expected number of
characters that will be produced for each input byte averageCharsPerByte, float A positive float value indicating the maximum number of
characters that will be produced for each input byte maxCharsPerByte)Initializes a new decoder. |
Modifier and Type | Method and Description |
---|---|
public final float | Returns: The average number of characters produced per byte of inputReturns the average number of characters that will be produced for each byte of input. |
public final Charset | |
public final CoderResult | Returns: A coder-result object describing the reason for terminationThe input byte buffer in, CharBuffer The output character buffer out, boolean true if, and only if, the invoker can provide no
additional input bytes beyond those in the given bufferDecodes as many bytes as possible from the given input buffer, writing the results to the given output buffer. |
public final CharBuffer | Returns: A newly-allocated character buffer containing the result of the decoding operation. The buffer's position will be zero and its limit will follow the last character written.The input byte buffer in)Convenience method that decodes the remaining content of a single input byte buffer into a newly-allocated character buffer. |
protected abstract CoderResult | Returns: A coder-result object describing the reason for terminationThe input byte buffer in, CharBuffer The output character buffer out)Decodes one or more bytes into one or more characters. |
public Charset | Returns: The charset detected by this auto-detecting decoder, ornull if the charset has not yet been determinedRetrieves the charset that was detected by this decoder (optional operation). |
public final CoderResult | |
protected CoderResult | |
protected void | implOnMalformedInput(CodingErrorAction
The new action newAction)Reports a change to this decoder's malformed-input action. |
protected void | implOnUnmappableCharacter(CodingErrorAction
The new action newAction)Reports a change to this decoder's unmappable-character action. |
protected void | implReplaceWith(String
The replacement value newReplacement)Reports a change to this decoder's replacement value. |
protected void | |
public boolean | Returns: true if, and only if, this decoder implements an
auto-detecting charsetTells whether or not this decoder implements an auto-detecting charset. |
public boolean | Returns: true if, and only if, this decoder has detected a
specific charsetTells whether or not this decoder has yet detected a charset (optional operation). |
public CodingErrorAction | Returns: The current malformed-input action, which is nevernull Returns this decoder's current action for malformed-input errors. |
public final float | Returns: The maximum number of characters that will be produced per byte of inputReturns the maximum number of characters that will be produced for each byte of input. |
public final CharsetDecoder | Returns: This decoderThe new action; must not be newAction)null Changes this decoder's action for malformed-input errors. |
public final CharsetDecoder | Returns: This decoderThe new action; must not be newAction)null Changes this decoder's action for unmappable-character errors. |
public final String | Returns: This decoder's current replacement, which is nevernull and is never emptyReturns this decoder's replacement value. |
public final CharsetDecoder | Returns: This decoderThe new replacement; must not be
newReplacement)null , must have non-zero length,
and must not be longer than the value returned by the
maxCharsPerByte methodChanges this decoder's replacement value. |
public final CharsetDecoder | |
private void | |
public CodingErrorAction | Returns: The current unmappable-character action, which is nevernull Returns this decoder's current action for unmappable-character errors. |
averageCharsPerByte | back to summary |
---|---|
private final float averageCharsPerByte |
charset | back to summary |
---|---|
private final Charset charset |
malformedInputAction | back to summary |
---|---|
private CodingErrorAction malformedInputAction |
maxCharsPerByte | back to summary |
---|---|
private final float maxCharsPerByte |
replacement | back to summary |
---|---|
private String replacement |
ST_CODING | back to summary |
---|---|
private static final int ST_CODING |
ST_END | back to summary |
---|---|
private static final int ST_END |
ST_FLUSHED | back to summary |
---|---|
private static final int ST_FLUSHED |
ST_RESET | back to summary |
---|---|
private static final int ST_RESET |
state | back to summary |
---|---|
private int state |
stateNames | back to summary |
---|---|
private static String[] stateNames |
unmappableCharacterAction | back to summary |
---|---|
private CodingErrorAction unmappableCharacterAction |
CharsetDecoder | back to summary |
---|---|
private CharsetDecoder(Charset cs, float averageCharsPerByte, float maxCharsPerByte, String replacement) Initializes a new decoder. The new decoder will have the given chars-per-byte and replacement values.
|
CharsetDecoder | back to summary |
---|---|
protected CharsetDecoder(Charset cs, float averageCharsPerByte, float maxCharsPerByte) Initializes a new decoder. The new decoder will have the given
chars-per-byte values and its replacement will be the
string
|
averageCharsPerByte | back to summary |
---|---|
public final float averageCharsPerByte() Returns the average number of characters that will be produced for each byte of input. This heuristic value may be used to estimate the size of the output buffer required for a given input sequence.
|
charset | back to summary |
---|---|
public final Charset charset() Returns the charset that created this decoder.
|
decode | back to summary |
---|---|
public final CoderResult decode(ByteBuffer in, CharBuffer out, boolean endOfInput) Decodes as many bytes as possible from the given input buffer, writing the results to the given output buffer. The buffers are read from, and written to, starting at their current
positions. At most In addition to reading bytes from the input buffer and writing
characters to the output buffer, this method returns a
The This method works by invoking the
|
decode | back to summary |
---|---|
public final CharBuffer decode(ByteBuffer in) throws CharacterCodingException Convenience method that decodes the remaining content of a single input byte buffer into a newly-allocated character buffer. This method implements an entire decoding operation; that is, it resets this decoder, then it decodes the bytes in the given byte buffer, and finally it flushes this decoder. This method should therefore not be invoked if a decoding operation is already in progress.
|
decodeLoop | back to summary |
---|---|
protected abstract CoderResult decodeLoop(ByteBuffer in, CharBuffer out) Decodes one or more bytes into one or more characters. This method encapsulates the basic decoding loop, decoding as many
bytes as possible until it either runs out of input, runs out of room
in the output buffer, or encounters a decoding error. This method is
invoked by the The buffers are read from, and written to, starting at their current
positions. At most This method returns a An implementation of this method may perform arbitrary lookahead by
returning
|
detectedCharset | back to summary |
---|---|
public Charset detectedCharset() Retrieves the charset that was detected by this decoder (optional operation). If this decoder implements an auto-detecting charset then this
method returns the actual charset once it has been detected. After that
point, this method returns the same value for the duration of the
current decoding operation. If not enough input bytes have yet been
read to determine the actual charset then this method throws an The default implementation of this method always throws an
|
flush | back to summary |
---|---|
public final CoderResult flush(CharBuffer out) Flushes this decoder. Some decoders maintain internal state and may need to write some final characters to the output buffer once the overall input sequence has been read. Any additional output is written to the output buffer beginning at
its current position. At most If this method completes successfully then it returns If this decoder has already been flushed then invoking this method has no effect. This method invokes the
|
implFlush | back to summary |
---|---|
protected CoderResult implFlush(CharBuffer out) Flushes this decoder. The default implementation of this method does nothing, and always
returns
|
implOnMalformedInput | back to summary |
---|---|
protected void implOnMalformedInput(CodingErrorAction newAction) Reports a change to this decoder's malformed-input action. The default implementation of this method does nothing. This method should be overridden by decoders that require notification of changes to the malformed-input action.
|
implOnUnmappableCharacter | back to summary |
---|---|
protected void implOnUnmappableCharacter(CodingErrorAction newAction) Reports a change to this decoder's unmappable-character action. The default implementation of this method does nothing. This method should be overridden by decoders that require notification of changes to the unmappable-character action.
|
implReplaceWith | back to summary |
---|---|
protected void implReplaceWith(String newReplacement) Reports a change to this decoder's replacement value. The default implementation of this method does nothing. This method should be overridden by decoders that require notification of changes to the replacement.
|
implReset | back to summary |
---|---|
protected void implReset() Resets this decoder, clearing any charset-specific internal state. The default implementation of this method does nothing. This method should be overridden by decoders that maintain internal state. |
isAutoDetecting | back to summary |
---|---|
public boolean isAutoDetecting() Tells whether or not this decoder implements an auto-detecting charset. The default implementation of this method always returns
|
isCharsetDetected | back to summary |
---|---|
public boolean isCharsetDetected() Tells whether or not this decoder has yet detected a charset (optional operation). If this decoder implements an auto-detecting charset then at a
single point during a decoding operation this method may start returning
That this method returns The default implementation of this method always throws an
|
malformedInputAction | back to summary |
---|---|
public CodingErrorAction malformedInputAction() Returns this decoder's current action for malformed-input errors.
|
maxCharsPerByte | back to summary |
---|---|
public final float maxCharsPerByte() Returns the maximum number of characters that will be produced for each byte of input. This value may be used to compute the worst-case size of the output buffer required for a given input sequence. This value accounts for any necessary content-independent prefix or suffix characters.
|
onMalformedInput | back to summary |
---|---|
public final CharsetDecoder onMalformedInput(CodingErrorAction newAction) Changes this decoder's action for malformed-input errors. This method invokes the
|
onUnmappableCharacter | back to summary |
---|---|
public final CharsetDecoder onUnmappableCharacter(CodingErrorAction newAction) Changes this decoder's action for unmappable-character errors. This method invokes the
|
replacement | back to summary |
---|---|
public final String replacement() Returns this decoder's replacement value.
|
replaceWith | back to summary |
---|---|
public final CharsetDecoder replaceWith(String newReplacement) Changes this decoder's replacement value. This method invokes the
|
reset | back to summary |
---|---|
public final CharsetDecoder reset() Resets this decoder, clearing any internal state. This method resets charset-independent state and also invokes the
|
throwIllegalStateException | back to summary |
---|---|
private void throwIllegalStateException(int from, int to) |
unmappableCharacterAction | back to summary |
---|---|
public CodingErrorAction unmappableCharacterAction() Returns this decoder's current action for unmappable-character errors.
|