The input character sequence is provided in a character buffer or a series of such buffers. The output byte sequence is written to a byte buffer or a series of such buffers. An encoder should always be used by making the following sequence of method invocations, hereinafter referred to as an encoding operation:
Reset the encoder via the reset method, unless it
has not been used before;
Invoke the encode 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 encode method one final time, passing
true for the endOfInput argument; and then
Invoke the flush method so that the encoder can
flush any internal state to the output buffer.
encode method will encode as many
characters as possible from the input buffer, writing the resulting bytes
to the output buffer. The encode method returns when more
input is required, when there is not enough room in the output buffer, or
when an encoding 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 an encoding error, as appropriate, and try again.
There are two general types of encoding errors. If the input character sequence is not a legal sixteen-bit Unicode sequence then the input is considered malformed. If the input character sequence is legal but cannot be mapped to a valid byte sequence in the given charset then an unmappable character has been encountered.
How an encoding error is handled depends upon the action requested for
that type of error, which is described by an instance of the 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 byte array. The replacement
is initially set to the encoder's default replacement, which often
(but not always) has the initial value { (byte)'?' };
its value may be changed via the replaceWith method.
The default action for malformed-input and unmappable-character errors
is to 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.
This class is designed to handle many of the details of the encoding
process, including the implementation of error actions. An encoder for a
specific charset, which is a concrete subclass of this class, need only
implement the abstract encodeLoop method, which
encapsulates the basic encoding loop. A subclass that maintains internal
state should, additionally, override the implFlush and
implReset methods.
Instances of this class are not safe for use by multiple concurrent threads.
ByteBuffer, CharBuffer, Charset, CharsetDecoder
| Modifier and Type | Field and Description |
|---|---|
| private final float | |
| private WeakReference | |
| private final Charset | |
| private CodingErrorAction | |
| private final float | |
| private byte[] | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private int | |
| private static String[] | |
| private CodingErrorAction |
| Access | Constructor and Description |
|---|---|
| protected | CharsetEncoder(Charset
The charset that created this encoder cs, float A positive float value indicating the expected number of
bytes that will be produced for each input character averageBytesPerChar, float A positive float value indicating the maximum number of
bytes that will be produced for each input character maxBytesPerChar, byte[] The initial replacement; must not be replacement)null, must have
non-zero length, must not be longer than maxBytesPerChar,
and must be legalInitializes a new encoder. |
| protected | CharsetEncoder(Charset
The charset that created this encoder cs, float A positive float value indicating the expected number of
bytes that will be produced for each input character averageBytesPerChar, float A positive float value indicating the maximum number of
bytes that will be produced for each input character maxBytesPerChar)Initializes a new encoder. |
| Modifier and Type | Method and Description |
|---|---|
| public final float | Returns: The average number of bytes produced per character of inputReturns the average number of bytes that will be produced for each character of input. |
| private boolean | |
| public boolean | Returns: true if, and only if, this encoder can encode
the given characterThe given character c)Tells whether or not this encoder can encode the given character. |
| public boolean | Returns: true if, and only if, this encoder can encode
the given character without throwing any exceptions and without
performing any replacementsThe given character sequence cs)Tells whether or not this encoder can encode the given character sequence. |
| public final Charset | |
| public final CoderResult | Returns: A coder-result object describing the reason for terminationThe input character buffer in, ByteBuffer The output byte buffer out, boolean true if, and only if, the invoker can provide no
additional input characters beyond those in the given bufferEncodes as many characters as possible from the given input buffer, writing the results to the given output buffer. |
| public final ByteBuffer | Returns: A newly-allocated byte buffer containing the result of the encoding operation. The buffer's position will be zero and its limit will follow the last byte written.The input character buffer in)Convenience method that encodes the remaining content of a single input character buffer into a newly-allocated byte buffer. |
| private ByteBuffer | |
| protected abstract CoderResult | Returns: A coder-result object describing the reason for terminationThe input character buffer in, ByteBuffer The output byte buffer out)Encodes one or more characters into one or more bytes. |
| public final CoderResult | |
| protected CoderResult | |
| protected void | implOnMalformedInput(CodingErrorAction
The new action newAction)Reports a change to this encoder's malformed-input action. |
| protected void | implOnUnmappableCharacter(CodingErrorAction
The new action newAction)Reports a change to this encoder's unmappable-character action. |
| protected void | implReplaceWith(byte[]
The replacement value newReplacement)Reports a change to this encoder's replacement value. |
| protected void | |
| public boolean | Returns: true if, and only if, the given byte array
is a legal replacement value for this encoderThe byte array to be tested repl)Tells whether or not the given byte array is a legal replacement value for this encoder. |
| public CodingErrorAction | Returns: The current malformed-input action, which is nevernullReturns this encoder's current action for malformed-input errors. |
| public final float | Returns: The maximum number of bytes that will be produced per character of inputReturns the maximum number of bytes that will be produced for each character of input. |
| public final CharsetEncoder | Returns: This encoderThe new action; must not be newAction)nullChanges this encoder's action for malformed-input errors. |
| public final CharsetEncoder | Returns: This encoderThe new action; must not be newAction)nullChanges this encoder's action for unmappable-character errors. |
| public final byte[] | Returns: This encoder's current replacement, which is nevernull and is never emptyReturns this encoder's replacement value. |
| public final CharsetEncoder | Returns: This encoderThe new replacement; must not be
newReplacement)null, must have non-zero length,
must not be longer than the value returned by the
maxBytesPerChar method, and
must be legalChanges this encoder's replacement value. |
| public final CharsetEncoder | |
| private void | |
| public CodingErrorAction | Returns: The current unmappable-character action, which is nevernullReturns this encoder's current action for unmappable-character errors. |
| averageBytesPerChar | back to summary |
|---|---|
| private final float averageBytesPerChar | |
| cachedDecoder | back to summary |
|---|---|
| private WeakReference<CharsetDecoder> cachedDecoder | |
| charset | back to summary |
|---|---|
| private final Charset charset | |
| malformedInputAction | back to summary |
|---|---|
| private CodingErrorAction malformedInputAction | |
| maxBytesPerChar | back to summary |
|---|---|
| private final float maxBytesPerChar | |
| replacement | back to summary |
|---|---|
| private byte[] replacement | |
| ST_CODING | back to summary |
|---|---|
| private static final int ST_CODING | |
| ST_END | back to summary |
|---|---|
| private static final int ST_END | |
| ST_FLUSHED | back to summary |
|---|---|
| private static final int ST_FLUSHED | |
| ST_RESET | back to summary |
|---|---|
| private static final int ST_RESET | |
| state | back to summary |
|---|---|
| private int state | |
| stateNames | back to summary |
|---|---|
| private static String[] stateNames | |
| unmappableCharacterAction | back to summary |
|---|---|
| private CodingErrorAction unmappableCharacterAction | |
| CharsetEncoder | back to summary |
|---|---|
| protected CharsetEncoder(Charset cs, float averageBytesPerChar, float maxBytesPerChar, byte[] replacement) Initializes a new encoder. The new encoder will have the given bytes-per-char and replacement values.
| |
| CharsetEncoder | back to summary |
|---|---|
| protected CharsetEncoder(Charset cs, float averageBytesPerChar, float maxBytesPerChar) Initializes a new encoder. The new encoder will have the given
bytes-per-char values and its replacement will be the
byte array
| |
| averageBytesPerChar | back to summary |
|---|---|
| public final float averageBytesPerChar() 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.
| |
| canEncode | back to summary |
|---|---|
| private boolean canEncode(CharBuffer cb) | |
| canEncode | back to summary |
|---|---|
| public boolean canEncode(char c) Tells whether or not this encoder can encode the given character. This method returns This method may modify this encoder's state; it should therefore not be invoked if an encoding operation is already in progress. The default implementation of this method is not very efficient; it should generally be overridden to improve performance.
| |
| canEncode | back to summary |
|---|---|
| public boolean canEncode(CharSequence cs) Tells whether or not this encoder can encode the given character sequence. If this method returns This method may modify this encoder's state; it should therefore not be invoked if an encoding operation is already in progress. The default implementation of this method is not very efficient; it should generally be overridden to improve performance.
| |
| charset | back to summary |
|---|---|
| public final Charset charset() Returns the charset that created this encoder.
| |
| encode | back to summary |
|---|---|
| public final CoderResult encode(CharBuffer in, ByteBuffer out, boolean endOfInput) Encodes as many characters as possible from the given input buffer, writing the results to the given output buffer. The buffers are read from, and written to, starting at their current
positions. At most In addition to reading characters from the input buffer and writing
bytes to the output buffer, this method returns a
The This method works by invoking the
| |
| encode | back to summary |
|---|---|
| public final ByteBuffer encode(CharBuffer in) throws CharacterCodingException Convenience method that encodes the remaining content of a single input character buffer into a newly-allocated byte buffer. This method implements an entire encoding operation; that is, it resets this encoder, then it encodes the characters in the given character buffer, and finally it flushes this encoder. This method should therefore not be invoked if an encoding operation is already in progress.
| |
| encode | back to summary |
|---|---|
| private ByteBuffer encode(CharBuffer in, boolean throwOnError) throws CharacterCodingException | |
| encodeLoop | back to summary |
|---|---|
| protected abstract CoderResult encodeLoop(CharBuffer in, ByteBuffer out) Encodes one or more characters into one or more bytes. This method encapsulates the basic encoding loop, encoding as many
characters as possible until it either runs out of input, runs out of room
in the output buffer, or encounters an encoding error. This method is
invoked by the The buffers are read from, and written to, starting at their current
positions. At most This method returns a An implementation of this method may perform arbitrary lookahead by
returning
| |
| flush | back to summary |
|---|---|
| public final CoderResult flush(ByteBuffer out) Flushes this encoder. 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. Any additional output is written to the output buffer beginning at
its current position. At most If this method completes successfully then it returns If this encoder has already been flushed then invoking this method has no effect. This method invokes the
| |
| implFlush | back to summary |
|---|---|
| protected CoderResult implFlush(ByteBuffer out) Flushes this encoder. The default implementation of this method does nothing, and always
returns
| |
| implOnMalformedInput | back to summary |
|---|---|
| protected void implOnMalformedInput(CodingErrorAction newAction) Reports a change to this encoder's malformed-input action. 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.
| |
| implOnUnmappableCharacter | back to summary |
|---|---|
| protected void implOnUnmappableCharacter(CodingErrorAction newAction) Reports a change to this encoder's unmappable-character action. 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.
| |
| implReplaceWith | back to summary |
|---|---|
| protected void implReplaceWith(byte[] newReplacement) Reports a change to this encoder's replacement value. The default implementation of this method does nothing. This method should be overridden by encoders that require notification of changes to the replacement.
| |
| implReset | back to summary |
|---|---|
| protected void implReset() Resets this encoder, clearing any charset-specific internal state. The default implementation of this method does nothing. This method should be overridden by encoders that maintain internal state. | |
| isLegalReplacement | back to summary |
|---|---|
| public boolean isLegalReplacement(byte[] repl) Tells whether or not the given byte array is a legal replacement value for this encoder. A replacement is legal if, and only if, it is a legal sequence of bytes in this encoder's charset; that is, it must be possible to decode the replacement into one or more sixteen-bit Unicode characters. The default implementation of this method is not very efficient; it should generally be overridden to improve performance.
| |
| malformedInputAction | back to summary |
|---|---|
| public CodingErrorAction malformedInputAction() Returns this encoder's current action for malformed-input errors.
| |
| maxBytesPerChar | back to summary |
|---|---|
| public final float maxBytesPerChar() 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. This value accounts for any necessary content-independent prefix or suffix bytes, such as byte-order marks.
| |
| onMalformedInput | back to summary |
|---|---|
| public final CharsetEncoder onMalformedInput(CodingErrorAction newAction) Changes this encoder's action for malformed-input errors. This method invokes the
| |
| onUnmappableCharacter | back to summary |
|---|---|
| public final CharsetEncoder onUnmappableCharacter(CodingErrorAction newAction) Changes this encoder's action for unmappable-character errors. This method invokes the
| |
| replacement | back to summary |
|---|---|
| public final byte[] replacement() Returns this encoder's replacement value.
| |
| replaceWith | back to summary |
|---|---|
| public final CharsetEncoder replaceWith(byte[] newReplacement) Changes this encoder's replacement value. This method invokes the
| |
| reset | back to summary |
|---|---|
| public final CharsetEncoder reset() Resets this encoder, clearing any internal state. This method resets charset-independent state and also invokes the
| |
| throwIllegalStateException | back to summary |
|---|---|
| private void throwIllegalStateException(int from, int to) | |
| unmappableCharacterAction | back to summary |
|---|---|
| public CodingErrorAction unmappableCharacterAction() Returns this encoder's current action for unmappable-character errors.
| |