Methods Summary |
---|
private java.nio.CharBuffer | allocateMore(java.nio.CharBuffer output)
if (output.capacity() == 0) {
return CharBuffer.allocate(1);
}
CharBuffer result = CharBuffer.allocate(output.capacity() * 2);
output.flip();
result.put(output);
return result;
|
public final float | averageCharsPerByte()Gets the average number of characters created by this decoder for a
single input byte.
return averChars;
|
public final java.nio.charset.Charset | charset()Gets the Charset which this decoder uses.
return cs;
|
private void | checkCoderResult(java.nio.charset.CoderResult result)
if (result.isMalformed() && malformAction == CodingErrorAction.REPORT) {
throw new MalformedInputException(result.length());
} else if (result.isUnmappable()
&& unmapAction == CodingErrorAction.REPORT) {
throw new UnmappableCharacterException(result.length());
}
|
public final java.nio.CharBuffer | decode(java.nio.ByteBuffer in)This is a facade method for the decoding operation.
This method decodes the remaining byte sequence of the given byte buffer
into a new character buffer. This method performs a complete decoding
operation, resets at first, then decodes, and flushes at last.
This method should not be invoked while another {@code decode} operation
is ongoing.
reset();
int length = (int) (in.remaining() * averChars);
CharBuffer output = CharBuffer.allocate(length);
CoderResult result = null;
while (true) {
result = decode(in, output, false);
checkCoderResult(result);
if (result.isUnderflow()) {
break;
} else if (result.isOverflow()) {
output = allocateMore(output);
}
}
result = decode(in, output, true);
checkCoderResult(result);
while (true) {
result = flush(output);
checkCoderResult(result);
if (result.isOverflow()) {
output = allocateMore(output);
} else {
break;
}
}
output.flip();
status = FLUSH;
return output;
|
public final java.nio.charset.CoderResult | decode(java.nio.ByteBuffer in, java.nio.CharBuffer out, boolean endOfInput)Decodes bytes starting at the current position of the given input buffer,
and writes the equivalent character sequence into the given output buffer
from its current position.
The buffers' position will be changed with the reading and writing
operation, but their limits and marks will be kept intact.
A CoderResult instance will be returned according to
following rules:
- {@link CoderResult#OVERFLOW CoderResult.OVERFLOW} indicates that
even though not all of the input has been processed, the buffer the
output is being written to has reached its capacity. In the event of this
code being returned this method should be called once more with an
out argument that has not already been filled.
- {@link CoderResult#UNDERFLOW CoderResult.UNDERFLOW} indicates that
as many bytes as possible in the input buffer have been decoded. If there
is no further input and no remaining bytes in the input buffer then this
operation may be regarded as complete. Otherwise, this method should be
called once more with additional input.
- A {@link CoderResult#malformedForLength(int) malformed input} result
indicates that some malformed input error has been encountered, and the
erroneous bytes start at the input buffer's position and their number can
be got by result's {@link CoderResult#length() length}. This kind of
result can be returned only if the malformed action is
{@link CodingErrorAction#REPORT CodingErrorAction.REPORT}.
- A {@link CoderResult#unmappableForLength(int) unmappable character}
result indicates that some unmappable character error has been
encountered, and the erroneous bytes start at the input buffer's position
and their number can be got by result's
{@link CoderResult#length() length}. This kind of result can be returned
only if the unmappable character action is
{@link CodingErrorAction#REPORT CodingErrorAction.REPORT}.
The endOfInput parameter indicates that the invoker cannot
provide further input. This parameter is true if and only if the bytes in
current input buffer are all inputs for this decoding operation. Note
that it is common and won't cause an error if the invoker sets false and
then can't provide more input, while it may cause an error if the invoker
always sets true in several consecutive invocations. This would make the
remaining input to be treated as malformed input.
This method invokes the
{@link #decodeLoop(ByteBuffer, CharBuffer) decodeLoop} method to
implement the basic decode logic for a specific charset.
/*
* status check
*/
if ((status == FLUSH) || (!endOfInput && status == END)) {
throw new IllegalStateException();
}
CoderResult result = null;
// begin to decode
while (true) {
CodingErrorAction action = null;
try {
result = decodeLoop(in, out);
} catch (BufferOverflowException ex) {
// unexpected exception
throw new CoderMalfunctionError(ex);
} catch (BufferUnderflowException ex) {
// unexpected exception
throw new CoderMalfunctionError(ex);
}
/*
* result handling
*/
if (result.isUnderflow()) {
int remaining = in.remaining();
status = endOfInput ? END : ONGOING;
if (endOfInput && remaining > 0) {
result = CoderResult.malformedForLength(remaining);
in.position(in.position() + result.length());
} else {
return result;
}
}
if (result.isOverflow()) {
return result;
}
// set coding error handle action
action = malformAction;
if (result.isUnmappable()) {
action = unmapAction;
}
// If the action is IGNORE or REPLACE, we should continue decoding.
if (action == CodingErrorAction.REPLACE) {
if (out.remaining() < replace.length()) {
return CoderResult.OVERFLOW;
}
out.put(replace);
} else {
if (action != CodingErrorAction.IGNORE)
return result;
}
if (!result.isMalformed()) {
// Note: the following condition is removed in Harmony revision 518047
// However, making the conditional statement unconditional
// leads to misbehavior when using REPLACE on malformedInput.
in.position(in.position() + result.length());
}
}
|
protected abstract java.nio.charset.CoderResult | decodeLoop(java.nio.ByteBuffer in, java.nio.CharBuffer out)Decodes bytes into characters. This method is called by the
{@link #decode(ByteBuffer, CharBuffer, boolean) decode} method.
This method will implement the essential decoding operation, and it won't
stop decoding until either all the input bytes are read, the output
buffer is filled, or some exception is encountered. Then it will return a
CoderResult object indicating the result of current
decoding operation. The rules to construct the CoderResult
are the same as for
{@link #decode(ByteBuffer, CharBuffer, boolean) decode}. When an
exception is encountered in the decoding operation, most implementations
of this method will return a relevant result object to the
{@link #decode(ByteBuffer, CharBuffer, boolean) decode} method, and some
performance optimized implementation may handle the exception and
implement the error action itself.
The buffers are scanned from their current positions, and their positions
will be modified accordingly, while their marks and limits will be
intact. At most {@link ByteBuffer#remaining() in.remaining()} characters
will be read, and {@link CharBuffer#remaining() out.remaining()} bytes
will be written.
Note that some implementations may pre-scan the input buffer and return a
CoderResult.UNDERFLOW until it receives sufficient input.
|
public java.nio.charset.Charset | detectedCharset()Gets the charset detected by this decoder; this method is optional.
If implementing an auto-detecting charset, then this decoder returns the
detected charset from this method when it is available. The returned
charset will be the same for the rest of the decode operation.
If insufficient bytes have been read to determine the charset, an
IllegalStateException will be thrown.
The default implementation always throws
UnsupportedOperationException , so it should be overridden
by a subclass if needed.
throw new UnsupportedOperationException();
|
public final java.nio.charset.CoderResult | flush(java.nio.CharBuffer out)Flushes this decoder.
This method will call {@link #implFlush(CharBuffer) implFlush}. Some
decoders may need to write some characters to the output buffer when they
have read all input bytes; subclasses can override
{@link #implFlush(CharBuffer) implFlush} to perform the writing operation.
The maximum number of written bytes won't be larger than
{@link CharBuffer#remaining() out.remaining()}. If some decoder wants to
write more bytes than an output buffer's remaining space allows, then a
CoderResult.OVERFLOW will be returned, and this method
must be called again with a character buffer that has more remaining
space. Otherwise this method will return
CoderResult.UNDERFLOW , which means one decoding process
has been completed successfully.
During the flush, the output buffer's position will be changed
accordingly, while its mark and limit will be intact.
if (status != END && status != INIT) {
throw new IllegalStateException();
}
CoderResult result = implFlush(out);
if (result == CoderResult.UNDERFLOW) {
status = FLUSH;
}
return result;
|
protected java.nio.charset.CoderResult | implFlush(java.nio.CharBuffer out)Flushes this decoder. The default implementation does nothing and always
returns CoderResult.UNDERFLOW ; this method can be
overridden if needed.
return CoderResult.UNDERFLOW;
|
protected void | implOnMalformedInput(java.nio.charset.CodingErrorAction newAction)Notifies that this decoder's CodingErrorAction specified
for malformed input error has been changed. The default implementation
does nothing; this method can be overridden if needed.
// default implementation is empty
|
protected void | implOnUnmappableCharacter(java.nio.charset.CodingErrorAction newAction)Notifies that this decoder's CodingErrorAction specified
for unmappable character error has been changed. The default
implementation does nothing; this method can be overridden if needed.
// default implementation is empty
|
protected void | implReplaceWith(java.lang.String newReplacement)Notifies that this decoder's replacement has been changed. The default
implementation does nothing; this method can be overridden if needed.
// default implementation is empty
|
protected void | implReset()Reset this decoder's charset related state. The default implementation
does nothing; this method can be overridden if needed.
// default implementation is empty
|
public boolean | isAutoDetecting()Indicates whether this decoder implements an auto-detecting charset.
return false;
|
public boolean | isCharsetDetected()Indicates whether this decoder has detected a charset; this method is
optional.
If this decoder implements an auto-detecting charset, then this method
may start to return true during decoding operation to indicate that a
charset has been detected in the input bytes and that the charset can be
retrieved by invoking the {@link #detectedCharset() detectedCharset}
method.
Note that a decoder that implements an auto-detecting charset may still
succeed in decoding a portion of the given input even when it is unable
to detect the charset. For this reason users should be aware that a
false return value does not indicate that no decoding took
place.
The default implementation always throws an
UnsupportedOperationException ; it should be overridden by
a subclass if needed.
throw new UnsupportedOperationException();
|
public java.nio.charset.CodingErrorAction | malformedInputAction()Gets this decoder's CodingErrorAction when malformed input
occurred during the decoding process.
return malformAction;
|
public final float | maxCharsPerByte()Gets the maximum number of characters which can be created by this
decoder for one input byte, must be positive.
return maxChars;
|
public final java.nio.charset.CharsetDecoder | onMalformedInput(java.nio.charset.CodingErrorAction newAction)Sets this decoder's action on malformed input errors.
This method will call the
{@link #implOnMalformedInput(CodingErrorAction) implOnMalformedInput}
method with the given new action as argument.
if (null == newAction) {
throw new IllegalArgumentException();
}
malformAction = newAction;
implOnMalformedInput(newAction);
return this;
|
public final java.nio.charset.CharsetDecoder | onUnmappableCharacter(java.nio.charset.CodingErrorAction newAction)Sets this decoder's action on unmappable character errors.
This method will call the
{@link #implOnUnmappableCharacter(CodingErrorAction) implOnUnmappableCharacter}
method with the given new action as argument.
if (null == newAction) {
throw new IllegalArgumentException();
}
unmapAction = newAction;
implOnUnmappableCharacter(newAction);
return this;
|
public final java.nio.charset.CharsetDecoder | replaceWith(java.lang.String newReplacement)Sets the new replacement string.
This method first checks the given replacement's validity, then changes
the replacement value, and at last calls the
{@link #implReplaceWith(String) implReplaceWith} method with the given
new replacement as argument.
if (null == newReplacement || newReplacement.length() == 0) {
// niochar.06=Replacement string cannot be null or empty.
throw new IllegalArgumentException(Messages.getString("niochar.06")); //$NON-NLS-1$
}
if (newReplacement.length() > maxChars) {
// niochar.07=Replacement string's length cannot be larger than max
// characters per byte.
throw new IllegalArgumentException(Messages.getString("niochar.07")); //$NON-NLS-1$
}
replace = newReplacement;
implReplaceWith(newReplacement);
return this;
|
public final java.lang.String | replacement()Gets the replacement string, which is never null or empty.
return replace;
|
public final java.nio.charset.CharsetDecoder | reset()Resets this decoder. This method will reset the internal status, and then
calls implReset() to reset any status related to the
specific charset.
status = INIT;
implReset();
return this;
|
public java.nio.charset.CodingErrorAction | unmappableCharacterAction()Gets this decoder's CodingErrorAction when an unmappable
character error occurred during the decoding process.
return unmapAction;
|