java.nio.charset
Class CharsetEncoder

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.nio.charset.CharsetEncoder

public abstract class CharsetEncoder
extends Object sample code for java.lang.Object definition code for java.lang.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 sample code for java.nio.charset.CharsetEncoder.reset() definition code for java.nio.charset.CharsetEncoder.reset() method, unless it has not been used before;

  2. Invoke the encode sample code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) definition code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) 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 sample code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) definition code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) method one final time, passing true for the endOfInput argument; and then

  4. Invoke the flush sample code for java.nio.charset.CharsetEncoder.flush(java.nio.ByteBuffer) definition code for java.nio.charset.CharsetEncoder.flush(java.nio.ByteBuffer) method so that the encoder can flush any internal state to the output buffer.

Each invocation of the encode sample code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) definition code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) method will encode as many characters as possible from the input buffer, writing the resulting bytes to the output buffer. The encode sample code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) definition code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) 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 sample code for java.nio.charset.CoderResult definition code for java.nio.charset.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 sample code for java.nio.charset.CodingErrorAction definition code for java.nio.charset.CodingErrorAction class. The possible error actions are to ignore sample code for java.nio.charset.CodingErrorAction.IGNORE definition code for java.nio.charset.CodingErrorAction.IGNORE the erroneous input, report sample code for java.nio.charset.CodingErrorAction.REPORT definition code for java.nio.charset.CodingErrorAction.REPORT the error to the invoker via the returned CoderResult sample code for java.nio.charset.CoderResult definition code for java.nio.charset.CoderResult object, or replace sample code for java.nio.charset.CodingErrorAction.REPLACE definition code for java.nio.charset.CodingErrorAction.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 sample code for java.nio.charset.CharsetEncoder.replaceWith(byte[]) definition code for java.nio.charset.CharsetEncoder.replaceWith(byte[]) method.

The default action for malformed-input and unmappable-character errors is to report sample code for java.nio.charset.CodingErrorAction.REPORT definition code for java.nio.charset.CodingErrorAction.REPORT them. The malformed-input error action may be changed via the onMalformedInput sample code for java.nio.charset.CharsetEncoder.onMalformedInput(java.nio.charset.CodingErrorAction) definition code for java.nio.charset.CharsetEncoder.onMalformedInput(java.nio.charset.CodingErrorAction) method; the unmappable-character action may be changed via the onUnmappableCharacter sample code for java.nio.charset.CharsetEncoder.onUnmappableCharacter(java.nio.charset.CodingErrorAction) definition code for java.nio.charset.CharsetEncoder.onUnmappableCharacter(java.nio.charset.CodingErrorAction) 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 sample code for java.nio.charset.CharsetEncoder.encodeLoop(java.nio.CharBuffer, java.nio.ByteBuffer) definition code for java.nio.charset.CharsetEncoder.encodeLoop(java.nio.CharBuffer, java.nio.ByteBuffer) method, which encapsulates the basic encoding loop. A subclass that maintains internal state should, additionally, override the flush sample code for java.nio.charset.CharsetEncoder.flush(java.nio.ByteBuffer) definition code for java.nio.charset.CharsetEncoder.flush(java.nio.ByteBuffer) and reset sample code for java.nio.charset.CharsetEncoder.reset() definition code for java.nio.charset.CharsetEncoder.reset() methods.

Instances of this class are not safe for use by multiple concurrent threads.

Since:
1.4
See Also:
ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer , CharBuffer sample code for java.nio.CharBuffer definition code for java.nio.CharBuffer , Charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset , CharsetDecoder sample code for java.nio.charset.CharsetDecoder definition code for java.nio.charset.CharsetDecoder

Constructor Summary
protected CharsetEncoder sample code for java.nio.charset.CharsetEncoder.CharsetEncoder(java.nio.charset.Charset, float, float) definition code for java.nio.charset.CharsetEncoder.CharsetEncoder(java.nio.charset.Charset, float, float) (Charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset  cs, float averageBytesPerChar, float maxBytesPerChar)
          Initializes a new encoder.
protected CharsetEncoder sample code for java.nio.charset.CharsetEncoder.CharsetEncoder(java.nio.charset.Charset, float, float, byte[]) definition code for java.nio.charset.CharsetEncoder.CharsetEncoder(java.nio.charset.Charset, float, float, byte[]) (Charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset  cs, float averageBytesPerChar, float maxBytesPerChar, byte[] replacement)
          Initializes a new encoder.
 
Method Summary
 float averageBytesPerChar sample code for java.nio.charset.CharsetEncoder.averageBytesPerChar() definition code for java.nio.charset.CharsetEncoder.averageBytesPerChar() ()
          Returns the average number of bytes that will be produced for each character of input.
 boolean canEncode sample code for java.nio.charset.CharsetEncoder.canEncode(char) definition code for java.nio.charset.CharsetEncoder.canEncode(char) (char c)
          Tells whether or not this encoder can encode the given character.
 boolean canEncode sample code for java.nio.charset.CharsetEncoder.canEncode(java.lang.CharSequence) definition code for java.nio.charset.CharsetEncoder.canEncode(java.lang.CharSequence) (CharSequence sample code for java.lang.CharSequence definition code for java.lang.CharSequence  cs)
          Tells whether or not this encoder can encode the given character sequence.
 Charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset charset sample code for java.nio.charset.CharsetEncoder.charset() definition code for java.nio.charset.CharsetEncoder.charset() ()
          Returns the charset that created this encoder.
 ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer encode sample code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer) definition code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer) (CharBuffer sample code for java.nio.CharBuffer definition code for java.nio.CharBuffer  in)
          Convenience method that encodes the remaining content of a single input character buffer into a newly-allocated byte buffer.
 CoderResult sample code for java.nio.charset.CoderResult definition code for java.nio.charset.CoderResult encode sample code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) definition code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) (CharBuffer sample code for java.nio.CharBuffer definition code for java.nio.CharBuffer  in, ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  out, boolean endOfInput)
          Encodes as many characters as possible from the given input buffer, writing the results to the given output buffer.
protected abstract  CoderResult sample code for java.nio.charset.CoderResult definition code for java.nio.charset.CoderResult encodeLoop sample code for java.nio.charset.CharsetEncoder.encodeLoop(java.nio.CharBuffer, java.nio.ByteBuffer) definition code for java.nio.charset.CharsetEncoder.encodeLoop(java.nio.CharBuffer, java.nio.ByteBuffer) (CharBuffer sample code for java.nio.CharBuffer definition code for java.nio.CharBuffer  in, ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  out)
          Encodes one or more characters into one or more bytes.
 CoderResult sample code for java.nio.charset.CoderResult definition code for java.nio.charset.CoderResult flush sample code for java.nio.charset.CharsetEncoder.flush(java.nio.ByteBuffer) definition code for java.nio.charset.CharsetEncoder.flush(java.nio.ByteBuffer) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  out)
          Flushes this encoder.
protected  CoderResult sample code for java.nio.charset.CoderResult definition code for java.nio.charset.CoderResult implFlush sample code for java.nio.charset.CharsetEncoder.implFlush(java.nio.ByteBuffer) definition code for java.nio.charset.CharsetEncoder.implFlush(java.nio.ByteBuffer) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  out)
          Flushes this encoder.
protected  void implOnMalformedInput sample code for java.nio.charset.CharsetEncoder.implOnMalformedInput(java.nio.charset.CodingErrorAction) definition code for java.nio.charset.CharsetEncoder.implOnMalformedInput(java.nio.charset.CodingErrorAction) (CodingErrorAction sample code for java.nio.charset.CodingErrorAction definition code for java.nio.charset.CodingErrorAction  newAction)
          Reports a change to this encoder's malformed-input action.
protected  void implOnUnmappableCharacter sample code for java.nio.charset.CharsetEncoder.implOnUnmappableCharacter(java.nio.charset.CodingErrorAction) definition code for java.nio.charset.CharsetEncoder.implOnUnmappableCharacter(java.nio.charset.CodingErrorAction) (CodingErrorAction sample code for java.nio.charset.CodingErrorAction definition code for java.nio.charset.CodingErrorAction  newAction)
          Reports a change to this encoder's unmappable-character action.
protected  void implReplaceWith sample code for java.nio.charset.CharsetEncoder.implReplaceWith(byte[]) definition code for java.nio.charset.CharsetEncoder.implReplaceWith(byte[]) (byte[] newReplacement)
          Reports a change to this encoder's replacement value.
protected  void implReset sample code for java.nio.charset.CharsetEncoder.implReset() definition code for java.nio.charset.CharsetEncoder.implReset() ()
          Resets this encoder, clearing any charset-specific internal state.
 boolean isLegalReplacement sample code for java.nio.charset.CharsetEncoder.isLegalReplacement(byte[]) definition code for java.nio.charset.CharsetEncoder.isLegalReplacement(byte[]) (byte[] repl)
          Tells whether or not the given byte array is a legal replacement value for this encoder.
 CodingErrorAction sample code for java.nio.charset.CodingErrorAction definition code for java.nio.charset.CodingErrorAction malformedInputAction sample code for java.nio.charset.CharsetEncoder.malformedInputAction() definition code for java.nio.charset.CharsetEncoder.malformedInputAction() ()
          Returns this encoder's current action for malformed-input errors.
 float maxBytesPerChar sample code for java.nio.charset.CharsetEncoder.maxBytesPerChar() definition code for java.nio.charset.CharsetEncoder.maxBytesPerChar() ()
          Returns the maximum number of bytes that will be produced for each character of input.
 CharsetEncoder sample code for java.nio.charset.CharsetEncoder definition code for java.nio.charset.CharsetEncoder onMalformedInput sample code for java.nio.charset.CharsetEncoder.onMalformedInput(java.nio.charset.CodingErrorAction) definition code for java.nio.charset.CharsetEncoder.onMalformedInput(java.nio.charset.CodingErrorAction) (CodingErrorAction sample code for java.nio.charset.CodingErrorAction definition code for java.nio.charset.CodingErrorAction  newAction)
          Changes this encoder's action for malformed-input errors.
 CharsetEncoder sample code for java.nio.charset.CharsetEncoder definition code for java.nio.charset.CharsetEncoder onUnmappableCharacter sample code for java.nio.charset.CharsetEncoder.onUnmappableCharacter(java.nio.charset.CodingErrorAction) definition code for java.nio.charset.CharsetEncoder.onUnmappableCharacter(java.nio.charset.CodingErrorAction) (CodingErrorAction sample code for java.nio.charset.CodingErrorAction definition code for java.nio.charset.CodingErrorAction  newAction)
          Changes this encoder's action for unmappable-character errors.
 byte[] replacement sample code for java.nio.charset.CharsetEncoder.replacement() definition code for java.nio.charset.CharsetEncoder.replacement() ()
          Returns this encoder's replacement value.
 CharsetEncoder sample code for java.nio.charset.CharsetEncoder definition code for java.nio.charset.CharsetEncoder replaceWith sample code for java.nio.charset.CharsetEncoder.replaceWith(byte[]) definition code for java.nio.charset.CharsetEncoder.replaceWith(byte[]) (byte[] newReplacement)
          Changes this encoder's replacement value.
 CharsetEncoder sample code for java.nio.charset.CharsetEncoder definition code for java.nio.charset.CharsetEncoder reset sample code for java.nio.charset.CharsetEncoder.reset() definition code for java.nio.charset.CharsetEncoder.reset() ()
          Resets this encoder, clearing any internal state.
 CodingErrorAction sample code for java.nio.charset.CodingErrorAction definition code for java.nio.charset.CodingErrorAction unmappableCharacterAction sample code for java.nio.charset.CharsetEncoder.unmappableCharacterAction() definition code for java.nio.charset.CharsetEncoder.unmappableCharacterAction() ()
          Returns this encoder's current action for unmappable-character errors.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

CharsetEncoder sample code for java.nio.charset.CharsetEncoder(java.nio.charset.Charset, float, float, byte[]) definition code for java.nio.charset.CharsetEncoder(java.nio.charset.Charset, float, float, byte[])

protected CharsetEncoder(Charset sample code for java.nio.charset.Charset definition code for java.nio.charset.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.

Parameters:
averageBytesPerChar - A positive float value indicating the expected number of bytes that will be produced for each input character
maxBytesPerChar - A positive float value indicating the maximum number of bytes that will be produced for each input character
replacement - The initial replacement; must not be null, must have non-zero length, must not be longer than maxBytesPerChar, and must be legal sample code for java.nio.charset.CharsetEncoder.isLegalReplacement(byte[]) definition code for java.nio.charset.CharsetEncoder.isLegalReplacement(byte[])
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the preconditions on the parameters do not hold

CharsetEncoder sample code for java.nio.charset.CharsetEncoder(java.nio.charset.Charset, float, float) definition code for java.nio.charset.CharsetEncoder(java.nio.charset.Charset, float, float)

protected CharsetEncoder(Charset sample code for java.nio.charset.Charset definition code for java.nio.charset.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 { (byte)'?' }.

Parameters:
averageBytesPerChar - A positive float value indicating the expected number of bytes that will be produced for each input character
maxBytesPerChar - A positive float value indicating the maximum number of bytes that will be produced for each input character
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the preconditions on the parameters do not hold
Method Detail

charset sample code for java.nio.charset.CharsetEncoder.charset() definition code for java.nio.charset.CharsetEncoder.charset()

public final Charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset  charset()
Returns the charset that created this encoder.

Returns:
This encoder's charset

replacement sample code for java.nio.charset.CharsetEncoder.replacement() definition code for java.nio.charset.CharsetEncoder.replacement()

public final byte[] replacement()
Returns this encoder's replacement value.

Returns:
This encoder's current replacement, which is never null and is never empty

replaceWith sample code for java.nio.charset.CharsetEncoder.replaceWith(byte[]) definition code for java.nio.charset.CharsetEncoder.replaceWith(byte[])

public final CharsetEncoder sample code for java.nio.charset.CharsetEncoder definition code for java.nio.charset.CharsetEncoder  replaceWith(byte[] newReplacement)
Changes this encoder's replacement value.

This method invokes the implReplaceWith sample code for java.nio.charset.CharsetEncoder.implReplaceWith(byte[]) definition code for java.nio.charset.CharsetEncoder.implReplaceWith(byte[]) method, passing the new replacement, after checking that the new replacement is acceptable.

Parameters:
newReplacement - The new replacement; must not be null, must have non-zero length, must not be longer than the value returned by the maxBytesPerChar sample code for java.nio.charset.CharsetEncoder.maxBytesPerChar definition code for java.nio.charset.CharsetEncoder.maxBytesPerChar method, and must be legal sample code for java.nio.charset.CharsetEncoder.isLegalReplacement(byte[]) definition code for java.nio.charset.CharsetEncoder.isLegalReplacement(byte[])
Returns:
This encoder
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the preconditions on the parameter do not hold

implReplaceWith sample code for java.nio.charset.CharsetEncoder.implReplaceWith(byte[]) definition code for java.nio.charset.CharsetEncoder.implReplaceWith(byte[])

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.

Parameters:
newReplacement -

isLegalReplacement sample code for java.nio.charset.CharsetEncoder.isLegalReplacement(byte[]) definition code for java.nio.charset.CharsetEncoder.isLegalReplacement(byte[])

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.

Parameters:
repl - The byte array to be tested
Returns:
true if, and only if, the given byte array is a legal replacement value for this encoder

malformedInputAction sample code for java.nio.charset.CharsetEncoder.malformedInputAction() definition code for java.nio.charset.CharsetEncoder.malformedInputAction()

public CodingErrorAction sample code for java.nio.charset.CodingErrorAction definition code for java.nio.charset.CodingErrorAction  malformedInputAction()
Returns this encoder's current action for malformed-input errors.

Returns:
The current malformed-input action, which is never null

onMalformedInput sample code for java.nio.charset.CharsetEncoder.onMalformedInput(java.nio.charset.CodingErrorAction) definition code for java.nio.charset.CharsetEncoder.onMalformedInput(java.nio.charset.CodingErrorAction)

public final CharsetEncoder sample code for java.nio.charset.CharsetEncoder definition code for java.nio.charset.CharsetEncoder  onMalformedInput(CodingErrorAction sample code for java.nio.charset.CodingErrorAction definition code for java.nio.charset.CodingErrorAction  newAction)
Changes this encoder's action for malformed-input errors.

This method invokes the implOnMalformedInput sample code for java.nio.charset.CharsetEncoder.implOnMalformedInput(java.nio.charset.CodingErrorAction) definition code for java.nio.charset.CharsetEncoder.implOnMalformedInput(java.nio.charset.CodingErrorAction) method, passing the new action.

Parameters:
newAction - The new action; must not be null
Returns:
This encoder
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the precondition on the parameter does not hold

implOnMalformedInput sample code for java.nio.charset.CharsetEncoder.implOnMalformedInput(java.nio.charset.CodingErrorAction) definition code for java.nio.charset.CharsetEncoder.implOnMalformedInput(java.nio.charset.CodingErrorAction)

protected void implOnMalformedInput(CodingErrorAction sample code for java.nio.charset.CodingErrorAction definition code for java.nio.charset.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.


unmappableCharacterAction sample code for java.nio.charset.CharsetEncoder.unmappableCharacterAction() definition code for java.nio.charset.CharsetEncoder.unmappableCharacterAction()

public CodingErrorAction sample code for java.nio.charset.CodingErrorAction definition code for java.nio.charset.CodingErrorAction  unmappableCharacterAction()
Returns this encoder's current action for unmappable-character errors.

Returns:
The current unmappable-character action, which is never null

onUnmappableCharacter sample code for java.nio.charset.CharsetEncoder.onUnmappableCharacter(java.nio.charset.CodingErrorAction) definition code for java.nio.charset.CharsetEncoder.onUnmappableCharacter(java.nio.charset.CodingErrorAction)

public final CharsetEncoder sample code for java.nio.charset.CharsetEncoder definition code for java.nio.charset.CharsetEncoder  onUnmappableCharacter(CodingErrorAction sample code for java.nio.charset.CodingErrorAction definition code for java.nio.charset.CodingErrorAction  newAction)
Changes this encoder's action for unmappable-character errors.

This method invokes the implOnUnmappableCharacter sample code for java.nio.charset.CharsetEncoder.implOnUnmappableCharacter(java.nio.charset.CodingErrorAction) definition code for java.nio.charset.CharsetEncoder.implOnUnmappableCharacter(java.nio.charset.CodingErrorAction) method, passing the new action.

Parameters:
newAction - The new action; must not be null
Returns:
This encoder
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If the precondition on the parameter does not hold

implOnUnmappableCharacter sample code for java.nio.charset.CharsetEncoder.implOnUnmappableCharacter(java.nio.charset.CodingErrorAction) definition code for java.nio.charset.CharsetEncoder.implOnUnmappableCharacter(java.nio.charset.CodingErrorAction)

protected void implOnUnmappableCharacter(CodingErrorAction sample code for java.nio.charset.CodingErrorAction definition code for java.nio.charset.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.


averageBytesPerChar sample code for java.nio.charset.CharsetEncoder.averageBytesPerChar() definition code for java.nio.charset.CharsetEncoder.averageBytesPerChar()

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.

Returns:
The average number of bytes produced per character of input

maxBytesPerChar sample code for java.nio.charset.CharsetEncoder.maxBytesPerChar() definition code for java.nio.charset.CharsetEncoder.maxBytesPerChar()

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.

Returns:
The maximum number of bytes that will be produced per character of input

encode sample code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) definition code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean)

public final CoderResult sample code for java.nio.charset.CoderResult definition code for java.nio.charset.CoderResult  encode(CharBuffer sample code for java.nio.CharBuffer definition code for java.nio.CharBuffer  in,
                                ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.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.remaining() sample code for java.nio.Buffer.remaining() definition code for java.nio.Buffer.remaining() characters will be read and at most out.remaining() sample code for java.nio.Buffer.remaining() definition code for java.nio.Buffer.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.

In addition to reading characters from the input buffer and writing bytes to the output buffer, this method returns a CoderResult sample code for java.nio.charset.CoderResult definition code for java.nio.charset.CoderResult object to describe its reason for termination:

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.

The endOfInput 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 false for this parameter; if there is no possibility of providing further input then the invoker should pass true. It is not erroneous, and in fact it is quite common, to pass false 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 true so that any remaining unencoded input will be treated as being malformed.

This method works by invoking the encodeLoop sample code for java.nio.charset.CharsetEncoder.encodeLoop(java.nio.CharBuffer, java.nio.ByteBuffer) definition code for java.nio.charset.CharsetEncoder.encodeLoop(java.nio.CharBuffer, java.nio.ByteBuffer) method, interpreting its results, handling error conditions, and reinvoking it as necessary.

Parameters:
in - The input character buffer
out - The output byte buffer
endOfInput - true if, and only if, the invoker can provide no additional input characters beyond those in the given buffer
Returns:
A coder-result object describing the reason for termination
Throws:
IllegalStateException sample code for java.lang.IllegalStateException definition code for java.lang.IllegalStateException - If an encoding operation is already in progress and the previous step was an invocation neither of the reset sample code for java.nio.charset.CharsetEncoder.reset() definition code for java.nio.charset.CharsetEncoder.reset() method, nor of this method with a value of false for the endOfInput parameter, nor of this method with a value of true for the endOfInput parameter but a return value indicating an incomplete encoding operation
CoderMalfunctionError sample code for java.nio.charset.CoderMalfunctionError definition code for java.nio.charset.CoderMalfunctionError - If an invocation of the encodeLoop method threw an unexpected exception

flush sample code for java.nio.charset.CharsetEncoder.flush(java.nio.ByteBuffer) definition code for java.nio.charset.CharsetEncoder.flush(java.nio.ByteBuffer)

public final CoderResult sample code for java.nio.charset.CoderResult definition code for java.nio.charset.CoderResult  flush(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.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 out.remaining() sample code for java.nio.Buffer.remaining() definition code for java.nio.Buffer.remaining() bytes will be written. The buffer's position will be advanced appropriately, but its mark and limit will not be modified.

If this method completes successfully then it returns CoderResult.UNDERFLOW sample code for java.nio.charset.CoderResult.UNDERFLOW definition code for java.nio.charset.CoderResult.UNDERFLOW . If there is insufficient room in the output buffer then it returns CoderResult.OVERFLOW sample code for java.nio.charset.CoderResult.OVERFLOW definition code for java.nio.charset.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 encoding operation.

This method invokes the implFlush sample code for java.nio.charset.CharsetEncoder.implFlush(java.nio.ByteBuffer) definition code for java.nio.charset.CharsetEncoder.implFlush(java.nio.ByteBuffer) method to perform the actual flushing operation.

Parameters:
out - The output byte buffer
Returns:
A coder-result object, either CoderResult.UNDERFLOW sample code for java.nio.charset.CoderResult.UNDERFLOW definition code for java.nio.charset.CoderResult.UNDERFLOW or CoderResult.OVERFLOW sample code for java.nio.charset.CoderResult.OVERFLOW definition code for java.nio.charset.CoderResult.OVERFLOW
Throws:
IllegalStateException sample code for java.lang.IllegalStateException definition code for java.lang.IllegalStateException - If the previous step of the current encoding operation was an invocation neither of the reset sample code for java.nio.charset.CharsetEncoder.reset() definition code for java.nio.charset.CharsetEncoder.reset() method nor of the three-argument encode sample code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) definition code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) method with a value of true for the endOfInput parameter

implFlush sample code for java.nio.charset.CharsetEncoder.implFlush(java.nio.ByteBuffer) definition code for java.nio.charset.CharsetEncoder.implFlush(java.nio.ByteBuffer)

protected CoderResult sample code for java.nio.charset.CoderResult definition code for java.nio.charset.CoderResult  implFlush(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  out)
Flushes this encoder.

The default implementation of this method does nothing, and always returns CoderResult.UNDERFLOW sample code for java.nio.charset.CoderResult.UNDERFLOW definition code for java.nio.charset.CoderResult.UNDERFLOW . This method should be overridden by encoders that may need to write final bytes to the output buffer once the entire input sequence has been read.

Parameters:
out - The output byte buffer
Returns:
A coder-result object, either CoderResult.UNDERFLOW sample code for java.nio.charset.CoderResult.UNDERFLOW definition code for java.nio.charset.CoderResult.UNDERFLOW or CoderResult.OVERFLOW sample code for java.nio.charset.CoderResult.OVERFLOW definition code for java.nio.charset.CoderResult.OVERFLOW

reset sample code for java.nio.charset.CharsetEncoder.reset() definition code for java.nio.charset.CharsetEncoder.reset()

public final CharsetEncoder sample code for java.nio.charset.CharsetEncoder definition code for java.nio.charset.CharsetEncoder  reset()
Resets this encoder, clearing any internal state.

This method resets charset-independent state and also invokes the implReset sample code for java.nio.charset.CharsetEncoder.implReset() definition code for java.nio.charset.CharsetEncoder.implReset() method in order to perform any charset-specific reset actions.

Returns:
This encoder

implReset sample code for java.nio.charset.CharsetEncoder.implReset() definition code for java.nio.charset.CharsetEncoder.implReset()

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.


encodeLoop sample code for java.nio.charset.CharsetEncoder.encodeLoop(java.nio.CharBuffer, java.nio.ByteBuffer) definition code for java.nio.charset.CharsetEncoder.encodeLoop(java.nio.CharBuffer, java.nio.ByteBuffer)

protected abstract CoderResult sample code for java.nio.charset.CoderResult definition code for java.nio.charset.CoderResult  encodeLoop(CharBuffer sample code for java.nio.CharBuffer definition code for java.nio.CharBuffer  in,
                                          ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.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 encode sample code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) definition code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) method, which handles result interpretation and error recovery.

The buffers are read from, and written to, starting at their current positions. At most in.remaining() sample code for java.nio.Buffer.remaining() definition code for java.nio.Buffer.remaining() characters will be read, and at most out.remaining() sample code for java.nio.Buffer.remaining() definition code for java.nio.Buffer.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.

This method returns a CoderResult sample code for java.nio.charset.CoderResult definition code for java.nio.charset.CoderResult object to describe its reason for termination, in the same manner as the encode sample code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) definition code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) method. Most implementations of this method will handle encoding errors by returning an appropriate result object for interpretation by the encode sample code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) definition code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean) method. An optimized implementation may instead examine the relevant error action and implement that action itself.

An implementation of this method may perform arbitrary lookahead by returning CoderResult.UNDERFLOW sample code for java.nio.charset.CoderResult.UNDERFLOW definition code for java.nio.charset.CoderResult.UNDERFLOW until it receives sufficient input.

Parameters:
in - The input character buffer
out - The output byte buffer
Returns:
A coder-result object describing the reason for termination

encode sample code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer) definition code for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer)

public final ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  encode(CharBuffer sample code for java.nio.CharBuffer definition code for java.nio.CharBuffer  in)
                        throws CharacterCodingException sample code for java.nio.charset.CharacterCodingException definition code for java.nio.charset.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.

Parameters:
in - The input character buffer
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.
Throws:
IllegalStateException sample code for java.lang.IllegalStateException definition code for java.lang.IllegalStateException - If an encoding operation is already in progress
MalformedInputException sample code for java.nio.charset.MalformedInputException definition code for java.nio.charset.MalformedInputException - If the character sequence starting at the input buffer's current position is not a legal sixteen-bit Unicode sequence and the current malformed-input action is CodingErrorAction.REPORT sample code for java.nio.charset.CodingErrorAction.REPORT definition code for java.nio.charset.CodingErrorAction.REPORT
UnmappableCharacterException sample code for java.nio.charset.UnmappableCharacterException definition code for java.nio.charset.UnmappableCharacterException - If the character sequence starting at the input buffer's current position cannot be mapped to an equivalent byte sequence and the current unmappable-character action is CodingErrorAction.REPORT sample code for java.nio.charset.CodingErrorAction.REPORT definition code for java.nio.charset.CodingErrorAction.REPORT
CharacterCodingException sample code for java.nio.charset.CharacterCodingException definition code for java.nio.charset.CharacterCodingException

canEncode sample code for java.nio.charset.CharsetEncoder.canEncode(char) definition code for java.nio.charset.CharsetEncoder.canEncode(char)

public boolean canEncode(char c)
Tells whether or not this encoder can encode the given character.

This method returns false if the given character is a surrogate character; such characters can be interpreted only when they are members of a pair consisting of a high surrogate followed by a low surrogate. The canEncode(CharSequence) sample code for java.nio.charset.CharsetEncoder.canEncode(java.lang.CharSequence) definition code for java.nio.charset.CharsetEncoder.canEncode(java.lang.CharSequence) method may be used to test whether or not a character sequence can be encoded.

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.

Returns:
true if, and only if, this encoder can encode the given character
Throws:
IllegalStateException sample code for java.lang.IllegalStateException definition code for java.lang.IllegalStateException - If an encoding operation is already in progress

canEncode sample code for java.nio.charset.CharsetEncoder.canEncode(java.lang.CharSequence) definition code for java.nio.charset.CharsetEncoder.canEncode(java.lang.CharSequence)

public boolean canEncode(CharSequence sample code for java.lang.CharSequence definition code for java.lang.CharSequence  cs)
Tells whether or not this encoder can encode the given character sequence.

If this method returns false for a particular character sequence then more information about why the sequence cannot be encoded may be obtained by performing a full encoding operation.

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.

Returns:
true if, and only if, this encoder can encode the given character without throwing any exceptions and without performing any replacements
Throws:
IllegalStateException sample code for java.lang.IllegalStateException definition code for java.lang.IllegalStateException - If an encoding operation is already in progress