📄 charsetencoder.java
字号:
* dec.maxCharsPerByte()));
CoderResult cr = dec.decode(bb, cb, true);
return !cr.isError();
}
/**
* Returns this encoder'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 encoder'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 encoder
*
* @throws IllegalArgumentException
* If the precondition on the parameter does not hold
*/
public final CharsetEncoder onMalformedInput(CodingErrorAction newAction) {
if (newAction == null)
throw new IllegalArgumentException("Null action");
malformedInputAction = newAction;
implOnMalformedInput(newAction);
return this;
}
/**
* Reports a change to this encoder's malformed-input action.
*
* <p> The default implementation of this method does nothing. This method
* should be overridden by encoders that require notification of changes to
* the malformed-input action. </p>
*/
protected void implOnMalformedInput(CodingErrorAction newAction) { }
/**
* Returns this encoder'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 encoder'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 encoder
*
* @throws IllegalArgumentException
* If the precondition on the parameter does not hold
*/
public final CharsetEncoder onUnmappableCharacter(CodingErrorAction
newAction)
{
if (newAction == null)
throw new IllegalArgumentException("Null action");
unmappableCharacterAction = newAction;
implOnUnmappableCharacter(newAction);
return this;
}
/**
* Reports a change to this encoder's unmappable-character action.
*
* <p> The default implementation of this method does nothing. This method
* should be overridden by encoders that require notification of changes to
* the unmappable-character action. </p>
*/
protected void implOnUnmappableCharacter(CodingErrorAction newAction) { }
/**
* Returns the average number of bytes that will be produced for each
* character 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 bytes produced
* per character of input
*/
public final float averageBytesPerChar() {
return averageBytesPerChar;
}
/**
* Returns the maximum number of bytes that will be produced for each
* character 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 bytes that will be produced per
* character of input
*/
public final float maxBytesPerChar() {
return maxBytesPerChar;
}
/**
* Encodes as many characters 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()} characters
* will be read and at most {@link Buffer#remaining out.remaining()}
* bytes will be written. The buffers' positions will be advanced to
* reflect the characters read and the bytes written, but their marks and
* limits will not be modified.
*
* <p> In addition to reading characters from the input buffer and writing
* bytes 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 encoded. If there are no characters
* remaining and the invoker has no further input then the encoding
* 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 characters begin at the input
* buffer's (possibly incremented) position; the number of malformed
* characters 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 encoder
* 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 characters that
* encode the unmappable character begin at the input buffer's (possibly
* incremented) position; the number of such characters 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 encoder 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 encoding
* operation then care should be taken to preserve any characters 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 unencoded input will be treated
* as being malformed.
*
* <p> This method works by invoking the {@link #encodeLoop encodeLoop}
* method, interpreting its results, handling error conditions, and
* reinvoking it as necessary. </p>
*
*
* @param in
* The input character buffer
*
* @param out
* The output byte buffer
*
* @param endOfInput
* <tt>true</tt> if, and only if, the invoker can provide no
* additional input characters beyond those in the given buffer
*
* @return A coder-result object describing the reason for termination
*
* @throws IllegalStateException
* If a encoding 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 encoding operation
*
* @throws CoderMalfunctionError
* If an invocation of the encodeLoop method threw
* an unexpected exception
*/
public final CoderResult encode(CharBuffer in, ByteBuffer 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 = encodeLoop(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 encoder.
*
* <p> Some encoders maintain internal state and may need to write some
* final bytes 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()}
* bytes 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">encoding
* operation</a>.
*
* <p> This method invokes the {@link #implFlush implFlush} method to
* perform the actual flushing operation. </p>
*
* @param out
* The output byte buffer
*
* @return A coder-result object, either {@link CoderResult#UNDERFLOW} or
* {@link CoderResult#OVERFLOW}
*
* @throws IllegalStateException
* If the previous step of the current encoding operation was an
* invocation neither of the {@link #reset reset} method nor of
* the three-argument {@link
* #encode(CharBuffer,ByteBuffer,boolean) encode} method
* with a value of <tt>true</tt> for the <tt>endOfInput</tt>
* parameter
*/
public final CoderResult flush(ByteBuffer 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 + -