📄 charsetdecoder.java
字号:
/**
* Returns this decoder's current action for malformed-input errors. </p>
*
* @return The current malformed-input action, which is never <tt>null</tt>
*/
public CodingErrorAction malformedInputAction() {
return malformedInputAction;
}
/**
* Changes this decoder's action for malformed-input errors. </p>
*
* <p> This method invokes the {@link #implOnMalformedInput
* implOnMalformedInput} method, passing the new action. </p>
*
* @param newAction The new action; must not be <tt>null</tt>
*
* @return This decoder
*
* @throws IllegalArgumentException
* If the precondition on the parameter does not hold
*/
public final CharsetDecoder onMalformedInput(CodingErrorAction newAction) {
if (newAction == null)
throw new IllegalArgumentException("Null action");
malformedInputAction = newAction;
implOnMalformedInput(newAction);
return this;
}
/**
* Reports a change to this decoder's malformed-input action.
*
* <p> 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. </p>
*/
protected void implOnMalformedInput(CodingErrorAction newAction) { }
/**
* Returns this decoder's current action for unmappable-character errors.
* </p>
*
* @return The current unmappable-character action, which is never
* <tt>null</tt>
*/
public CodingErrorAction unmappableCharacterAction() {
return unmappableCharacterAction;
}
/**
* Changes this decoder's action for unmappable-character errors.
*
* <p> This method invokes the {@link #implOnUnmappableCharacter
* implOnUnmappableCharacter} method, passing the new action. </p>
*
* @param newAction The new action; must not be <tt>null</tt>
*
* @return This decoder
*
* @throws IllegalArgumentException
* If the precondition on the parameter does not hold
*/
public final CharsetDecoder onUnmappableCharacter(CodingErrorAction
newAction)
{
if (newAction == null)
throw new IllegalArgumentException("Null action");
unmappableCharacterAction = newAction;
implOnUnmappableCharacter(newAction);
return this;
}
/**
* Reports a change to this decoder's unmappable-character action.
*
* <p> 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. </p>
*/
protected void implOnUnmappableCharacter(CodingErrorAction newAction) { }
/**
* 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. </p>
*
* @return The average number of characters produced
* per byte of input
*/
public final float averageCharsPerByte() {
return averageCharsPerByte;
}
/**
* 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. </p>
*
* @return The maximum number of characters that will be produced per
* byte of input
*/
public final float maxCharsPerByte() {
return maxCharsPerByte;
}
/**
* Decodes as many bytes as possible from the given input buffer,
* writing the results to the given output buffer.
*
* <p> The buffers are read from, and written to, starting at their current
* positions. At most {@link Buffer#remaining in.remaining()} bytes
* will be read and at most {@link Buffer#remaining out.remaining()}
* characters will be written. The buffers' positions will be advanced to
* reflect the bytes read and the characters written, but their marks and
* limits will not be modified.
*
* <p> In addition to reading bytes from the input buffer and writing
* characters to the output buffer, this method returns a {@link CoderResult}
* object to describe its reason for termination:
*
* <ul>
*
* <li><p> {@link CoderResult#UNDERFLOW} indicates that as much of the
* input buffer as possible has been decoded. If there are no bytes
* remaining and the invoker has no further input then the decoding
* operation is complete. Otherwise there is insufficient input for the
* operation to proceed, so this method should be invoked again with
* further input. </p></li>
*
* <li><p> {@link CoderResult#OVERFLOW} indicates that the output buffer
* is full. This method should be invoked again with a non-full output
* buffer. </p></li>
*
* <li><p> A {@link CoderResult#malformedForLength
* </code>malformed-input<code>} result indicates that a malformed-input
* error has been detected. The malformed bytes begin at the input
* buffer's (possibly incremented) position; the number of malformed
* bytes may be determined by invoking the result object's {@link
* CoderResult#length length} method. This case applies only if the
* {@link #onMalformedInput </code>malformed action<code>} of this decoder
* is {@link CodingErrorAction#REPORT}; otherwise the malformed input
* will be ignored or replaced, as requested. </p></li>
*
* <li><p> An {@link CoderResult#unmappableForLength
* </code>unmappable-character<code>} result indicates that an
* unmappable-character error has been detected. The bytes that
* decode the unmappable character begin at the input buffer's (possibly
* incremented) position; the number of such bytes may be determined
* by invoking the result object's {@link CoderResult#length length}
* method. This case applies only if the {@link #onUnmappableCharacter
* </code>unmappable action<code>} of this decoder is {@link
* CodingErrorAction#REPORT}; otherwise the unmappable character will be
* ignored or replaced, as requested. </p></li>
*
* </ul>
*
* In any case, if this method is to be reinvoked in the same decoding
* operation then care should be taken to preserve any bytes remaining
* in the input buffer so that they are available to the next invocation.
*
* <p> The <tt>endOfInput</tt> parameter advises this method as to whether
* the invoker can provide further input beyond that contained in the given
* input buffer. If there is a possibility of providing additional input
* then the invoker should pass <tt>false</tt> for this parameter; if there
* is no possibility of providing further input then the invoker should
* pass <tt>true</tt>. It is not erroneous, and in fact it is quite
* common, to pass <tt>false</tt> in one invocation and later discover that
* no further input was actually available. It is critical, however, that
* the final invocation of this method in a sequence of invocations always
* pass <tt>true</tt> so that any remaining undecoded input will be treated
* as being malformed.
*
* <p> This method works by invoking the {@link #decodeLoop decodeLoop}
* method, interpreting its results, handling error conditions, and
* reinvoking it as necessary. </p>
*
*
* @param in
* The input byte buffer
*
* @param out
* The output character buffer
*
* @param endOfInput
* <tt>true</tt> if, and only if, the invoker can provide no
* additional input bytes beyond those in the given buffer
*
* @return A coder-result object describing the reason for termination
*
* @throws IllegalStateException
* If a decoding operation is already in progress and the previous
* step was an invocation neither of the {@link #reset reset}
* method, nor of this method with a value of <tt>false</tt> for
* the <tt>endOfInput</tt> parameter, nor of this method with a
* value of <tt>true</tt> for the <tt>endOfInput</tt> parameter
* but a return value indicating an incomplete decoding operation
*
* @throws CoderMalfunctionError
* If an invocation of the decodeLoop method threw
* an unexpected exception
*/
public final CoderResult decode(ByteBuffer in, CharBuffer out,
boolean endOfInput)
{
int newState = endOfInput ? ST_END : ST_CODING;
if ((state != ST_RESET) && (state != ST_CODING)
&& !(endOfInput && (state == ST_END)))
throwIllegalStateException(state, newState);
state = newState;
for (;;) {
CoderResult cr;
try {
cr = decodeLoop(in, out);
} catch (BufferUnderflowException x) {
throw new CoderMalfunctionError(x);
} catch (BufferOverflowException x) {
throw new CoderMalfunctionError(x);
}
if (cr.isOverflow())
return cr;
if (cr.isUnderflow()) {
if (endOfInput && in.hasRemaining()) {
cr = CoderResult.malformedForLength(in.remaining());
// Fall through to malformed-input case
} else {
return cr;
}
}
CodingErrorAction action = null;
if (cr.isMalformed())
action = malformedInputAction;
else if (cr.isUnmappable())
action = unmappableCharacterAction;
else
assert false : cr.toString();
if (action == CodingErrorAction.REPORT)
return cr;
if (action == CodingErrorAction.REPLACE) {
if (out.remaining() < replacement.length())
return CoderResult.OVERFLOW;
out.put(replacement);
}
if ((action == CodingErrorAction.IGNORE)
|| (action == CodingErrorAction.REPLACE)) {
// Skip erroneous input either way
in.position(in.position() + cr.length());
continue;
}
assert false;
}
}
/**
* Flushes this decoder.
*
* <p> 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.
*
* <p> Any additional output is written to the output buffer beginning at
* its current position. At most {@link Buffer#remaining out.remaining()}
* characters will be written. The buffer's position will be advanced
* appropriately, but its mark and limit will not be modified.
*
* <p> If this method completes successfully then it returns {@link
* CoderResult#UNDERFLOW}. If there is insufficient room in the output
* buffer then it returns {@link CoderResult#OVERFLOW}. If this happens
* then this method must be invoked again, with an output buffer that has
* more room, in order to complete the current <a href="#steps">decoding
* operation</a>.
*
* <p> This method invokes the {@link #implFlush implFlush} method to
* perform the actual flushing operation. </p>
*
* @param out
* The output character buffer
*
* @return A coder-result object, either {@link CoderResult#UNDERFLOW} or
* {@link CoderResult#OVERFLOW}
*
* @throws IllegalStateException
* If the previous step of the current decoding operation was an
* invocation neither of the {@link #reset reset} method nor of
* the three-argument {@link
* #decode(ByteBuffer,CharBuffer,boolean) decode} method
* with a value of <tt>true</tt> for the <tt>endOfInput</tt>
* parameter
*/
public final CoderResult flush(CharBuffer out) {
if (state != ST_END)
throwIllegalStateException(state, ST_FLUSHED);
state = ST_FLUSHED;
return implFlush(out);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -