public abstract class CharsetDecoder extends Object
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 The default action for malformed-input and unmappable-character errors
is to 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 Instances of this class are not safe for use by multiple concurrent
threads. 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.
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.
decodeLoop method, which
encapsulates the basic decoding loop. A subclass that maintains internal
state should, additionally, override the implFlush and
implReset methods.
ByteBuffer,
CharBuffer,
Charset,
CharsetEncoder| Modifier | Constructor and Description |
|---|---|
protected |
CharsetDecoder(Charset cs,
float averageCharsPerByte,
float maxCharsPerByte)
Initializes a new decoder.
|
| Modifier and Type | Method and Description |
|---|---|
float |
averageCharsPerByte()
Returns the average number of characters that will be produced for each
byte of input.
|
Charset |
charset()
Returns the charset that created this decoder.
|
Charset |
detectedCharset()
Retrieves the charset that was detected by this
decoder (optional operation).
|
protected void |
implReplaceWith(String newReplacement)
Reports a change to this decoder's replacement value.
|
protected void |
implReset()
Resets this decoder, clearing any charset-specific internal state.
|
boolean |
isAutoDetecting()
Tells whether or not this decoder implements an auto-detecting charset.
|
boolean |
isCharsetDetected()
Tells whether or not this decoder has yet detected a
charset (optional operation).
|
float |
maxCharsPerByte()
Returns the maximum number of characters that will be produced for each
byte of input.
|
String |
replacement()
Returns this decoder's replacement value.
|
CharsetDecoder |
replaceWith(String newReplacement)
Changes this decoder's replacement value.
|
CharsetDecoder |
reset()
Resets this decoder, clearing any internal state.
|
protected CharsetDecoder(Charset cs, float averageCharsPerByte, float maxCharsPerByte)
averageCharsPerByte - A positive float value indicating the expected number of
characters that will be produced for each input bytemaxCharsPerByte - A positive float value indicating the maximum number of
characters that will be produced for each input byteIllegalArgumentException - If the preconditions on the parameters do not holdpublic final Charset charset()
public final String replacement()
public final CharsetDecoder replaceWith(String newReplacement)
This method invokes the implReplaceWith
method, passing the new replacement, after checking that the new
replacement is acceptable.
newReplacement - The new replacement; must not be null
and must have non-zero lengthIllegalArgumentException - If the preconditions on the parameter do not holdprotected void implReplaceWith(String newReplacement)
The default implementation of this method does nothing. This method should be overridden by decoders that require notification of changes to the replacement.
newReplacement - public final float averageCharsPerByte()
public final float maxCharsPerByte()
public final CharsetDecoder reset()
This method resets charset-independent state and also invokes the
implReset method in order to perform any
charset-specific reset actions.
protected void implReset()
The default implementation of this method does nothing. This method should be overridden by decoders that maintain internal state.
public boolean isAutoDetecting()
The default implementation of this method always returns false; it should be overridden by auto-detecting decoders to return true.
public boolean isCharsetDetected()
If this decoder implements an auto-detecting charset then at a
single point during a decoding operation this method may start returning
true to indicate that a specific charset has been detected in
the input byte sequence. Once this occurs, the detectedCharset method may be invoked to retrieve the detected charset.
That this method returns false does not imply that no bytes have yet been decoded. Some auto-detecting decoders are capable of decoding some, or even all, of an input byte sequence without fixing on a particular charset.
The default implementation of this method always throws an UnsupportedOperationException; it should be overridden by
auto-detecting decoders to return true once the input charset
has been determined.
UnsupportedOperationException - If this decoder does not implement an auto-detecting charsetpublic Charset detectedCharset()
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 IllegalStateException.
The default implementation of this method always throws an UnsupportedOperationException; it should be overridden by
auto-detecting decoders to return the appropriate value.
IllegalStateException - If insufficient bytes have been read to determine a charsetUnsupportedOperationException - If this decoder does not implement an auto-detecting charsetCopyright © 2016 API Design. All Rights Reserved.