📄 charsetdecoder.java
字号:
/**
* Flushes this decoder.
*
* <p> The default implementation of this method does nothing, and always
* returns {@link CoderResult#UNDERFLOW}. This method should be overridden
* by decoders that may need to write final characters to the output buffer
* once the entire input sequence has been read. </p>
*
* @param out
* The output character buffer
*
* @return A coder-result object, either {@link CoderResult#UNDERFLOW} or
* {@link CoderResult#OVERFLOW}
*/
protected CoderResult implFlush(CharBuffer out) {
return CoderResult.UNDERFLOW;
}
/**
* Resets this decoder, clearing any internal state.
*
* <p> This method resets charset-independent state and also invokes the
* {@link #implReset() implReset} method in order to perform any
* charset-specific reset actions. </p>
*
* @return This decoder
*
*/
public final CharsetDecoder reset() {
implReset();
state = ST_RESET;
return this;
}
/**
* Resets this decoder, clearing any charset-specific internal state.
*
* <p> The default implementation of this method does nothing. This method
* should be overridden by decoders that maintain internal state. </p>
*/
protected void implReset() { }
/**
* Decodes one or more bytes into one or more characters.
*
* <p> 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 {@link #decode decode} method, which handles result
* interpretation and error recovery.
*
* <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> This method returns a {@link CoderResult} object to describe its
* reason for termination, in the same manner as the {@link #decode decode}
* method. Most implementations of this method will handle decoding errors
* by returning an appropriate result object for interpretation by the
* {@link #decode decode} method. An optimized implementation may instead
* examine the relevant error action and implement that action itself.
*
* <p> An implementation of this method may perform arbitrary lookahead by
* returning {@link CoderResult#UNDERFLOW} until it receives sufficient
* input. </p>
*
* @param in
* The input byte buffer
*
* @param out
* The output character buffer
*
* @return A coder-result object describing the reason for termination
*/
protected abstract CoderResult decodeLoop(ByteBuffer in,
CharBuffer out);
/**
* Convenience method that decodes the remaining content of a single input
* byte buffer into a newly-allocated character buffer.
*
* <p> This method implements an entire <a href="#steps">decoding
* operation</a>; 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. </p>
*
* @param in
* The input byte buffer
*
* @return 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.
*
* @throws IllegalStateException
* If a decoding operation is already in progress
*
* @throws MalformedInputException
* If the byte sequence starting at the input buffer's current
* position is not legal for this charset and the current malformed-input action
* is {@link CodingErrorAction#REPORT}
*
* @throws UnmappableCharacterException
* If the byte sequence starting at the input buffer's current
* position cannot be mapped to an equivalent character sequence and
* the current unmappable-character action is {@link
* CodingErrorAction#REPORT}
*/
public final CharBuffer decode(ByteBuffer in)
throws CharacterCodingException
{
int n = (int)(in.remaining() * averageCharsPerByte());
CharBuffer out = CharBuffer.allocate(n);
if (n == 0)
return out;
reset();
for (;;) {
CoderResult cr;
if (in.hasRemaining())
cr = decode(in, out, true);
else
cr = flush(out);
if (cr.isUnderflow())
break;
if (cr.isOverflow()) {
n *= 2;
CharBuffer o = CharBuffer.allocate(n);
out.flip();
o.put(out);
out = o;
continue;
}
cr.throwException();
}
out.flip();
return out;
}
/**
* Tells whether or not this decoder implements an auto-detecting charset.
*
* <p> The default implementation of this method always returns
* <tt>false</tt>; it should be overridden by auto-detecting decoders to
* return <tt>true</tt>. </p>
*
* @return <tt>true</tt> if, and only if, this decoder implements an
* auto-detecting charset
*/
public boolean isAutoDetecting() {
return false;
}
/**
* Tells whether or not this decoder has yet detected a
* charset <i>(optional operation)</i>.
*
* <p> If this decoder implements an auto-detecting charset then at a
* single point during a decoding operation this method may start returning
* <tt>true</tt> to indicate that a specific charset has been detected in
* the input byte sequence. Once this occurs, the {@link #detectedCharset
* detectedCharset} method may be invoked to retrieve the detected charset.
*
* <p> That this method returns <tt>false</tt> 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.
*
* <p> The default implementation of this method always throws an {@link
* UnsupportedOperationException}; it should be overridden by
* auto-detecting decoders to return <tt>true</tt> once the input charset
* has been determined. </p>
*
* @return <tt>true</tt> if, and only if, this decoder has detected a
* specific charset
*
* @throws UnsupportedOperationException
* If this decoder does not implement an auto-detecting charset
*/
public boolean isCharsetDetected() {
throw new UnsupportedOperationException();
}
/**
* Retrieves the charset that was detected by this
* decoder <i>(optional operation)</i>.
*
* <p> 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 {@link
* IllegalStateException}.
*
* <p> The default implementation of this method always throws an {@link
* UnsupportedOperationException}; it should be overridden by
* auto-detecting decoders to return the appropriate value. </p>
*
* @return The charset detected by this auto-detecting decoder,
* or <tt>null</tt> if the charset has not yet been determined
*
* @throws IllegalStateException
* If insufficient bytes have been read to determine a charset
*
* @throws UnsupportedOperationException
* If this decoder does not implement an auto-detecting charset
*/
public Charset detectedCharset() {
throw new UnsupportedOperationException();
}
private void throwIllegalStateException(int from, int to) {
throw new IllegalStateException("Current state = " + stateNames[from]
+ ", new state = " + stateNames[to]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -