CharsetEncoder

public abstract class CharsetEncoder extends Object

An engine that can transform a sequence of sixteen-bit Unicode characters into a sequence of bytes in a specific charset.

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:

  1. Reset the encoder via the reset method, unless it has not been used before;

  2. 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;

  3. Invoke the encode method one final time, passing true for the endOfInput argument; and then

  4. Invoke the flush method so that the encoder can flush any internal state to the output buffer.

Each invocation of the 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.

Protected Constructor Summary

CharsetEncoder(Charset cs, float averageBytesPerChar, float maxBytesPerChar, byte[] replacement)
Initializes a new encoder.
CharsetEncoder(Charset cs, float averageBytesPerChar, float maxBytesPerChar, byte[] replacement, boolean trusted)
CharsetEncoder(Charset cs, float averageBytesPerChar, float maxBytesPerChar)
Initializes a new encoder.

Public Method Summary

final float
averageBytesPerChar()
Returns the average number of bytes that will be produced for each character of input.
boolean
canEncode(char c)
Tells whether or not this encoder can encode the given character.
boolean
canEncode(CharSequence cs)
Tells whether or not this encoder can encode the given character sequence.
final Charset
charset()
Returns the charset that created this encoder.
final ByteBuffer
encode(CharBuffer in)
Convenience method that encodes the remaining content of a single input character buffer into a newly-allocated byte buffer.
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.
final CoderResult
flush(ByteBuffer out)
Flushes this encoder.
boolean
isLegalReplacement(byte[] repl)
Tells whether or not the given byte array is a legal replacement value for this encoder.
CodingErrorAction
malformedInputAction()
Returns this encoder's current action for malformed-input errors.
final float
maxBytesPerChar()
Returns the maximum number of bytes that will be produced for each character of input.
final CharsetEncoder
onMalformedInput(CodingErrorAction newAction)
Changes this encoder's action for malformed-input errors.
final CharsetEncoder
onUnmappableCharacter(CodingErrorAction newAction)
Changes this encoder's action for unmappable-character errors.
final CharsetEncoder
replaceWith(byte[] newReplacement)
Changes this encoder's replacement value.
final byte[]
replacement()
Returns this encoder's replacement value.
final CharsetEncoder
reset()
Resets this encoder, clearing any internal state.
CodingErrorAction
unmappableCharacterAction()
Returns this encoder's current action for unmappable-character errors.

Protected Method Summary

abstract CoderResult
encodeLoop(CharBuffer in, ByteBuffer out)
Encodes one or more characters into one or more bytes.
CoderResult
implFlush(ByteBuffer out)
Flushes this encoder.
void
implOnMalformedInput(CodingErrorAction newAction)
Reports a change to this encoder's malformed-input action.
void
implOnUnmappableCharacter(CodingErrorAction newAction)
Reports a change to this encoder's unmappable-character action.
void
implReplaceWith(byte[] newReplacement)
Reports a change to this encoder's replacement value.
void
implReset()
Resets this encoder, clearing any charset-specific internal state.

Inherited Method Summary