Sdílet prostřednictvím


CharsetEncoder.Encode Method

Definition

Overloads

Encode(CharBuffer)

Convenience method that encodes the remaining content of a single input character buffer into a newly-allocated byte buffer.

Encode(CharBuffer, ByteBuffer, Boolean)

Encodes as many characters as possible from the given input buffer, writing the results to the given output buffer.

Encode(CharBuffer)

Convenience method that encodes the remaining content of a single input character buffer into a newly-allocated byte buffer.

[Android.Runtime.Register("encode", "(Ljava/nio/CharBuffer;)Ljava/nio/ByteBuffer;", "")]
public Java.Nio.ByteBuffer? Encode (Java.Nio.CharBuffer? in);
[<Android.Runtime.Register("encode", "(Ljava/nio/CharBuffer;)Ljava/nio/ByteBuffer;", "")>]
member this.Encode : Java.Nio.CharBuffer -> Java.Nio.ByteBuffer

Parameters

in
CharBuffer

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.

Attributes

Exceptions

if another encoding operation is ongoing.

if an illegal input character sequence for this charset is encountered, and the action for malformed error is Report

if a legal but unmappable input character sequence for this charset is encountered, and the action for unmappable character error is Report. Unmappable means the Unicode character sequence at the input buffer's current position cannot be mapped to a equivalent byte sequence.

if other exception happened during the encode operation.

Remarks

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.

Java documentation for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

Encode(CharBuffer, ByteBuffer, Boolean)

Encodes as many characters as possible from the given input buffer, writing the results to the given output buffer.

[Android.Runtime.Register("encode", "(Ljava/nio/CharBuffer;Ljava/nio/ByteBuffer;Z)Ljava/nio/charset/CoderResult;", "")]
public Java.Nio.Charset.CoderResult? Encode (Java.Nio.CharBuffer? in, Java.Nio.ByteBuffer? out, bool endOfInput);
[<Android.Runtime.Register("encode", "(Ljava/nio/CharBuffer;Ljava/nio/ByteBuffer;Z)Ljava/nio/charset/CoderResult;", "")>]
member this.Encode : Java.Nio.CharBuffer * Java.Nio.ByteBuffer * bool -> Java.Nio.Charset.CoderResult

Parameters

in
CharBuffer

The input character buffer

out
ByteBuffer

The output byte buffer

endOfInput
Boolean

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

Attributes

Exceptions

if the encoding operation has already started or no more input is needed in this encoding process.

If the EncodeLoop(CharBuffer, ByteBuffer) method threw an BufferUnderflowException or BufferUnderflowException.

Remarks

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 Buffer#remaining in.remaining() characters will be read and at most 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.

In addition to reading characters from the input buffer and writing bytes to the output buffer, this method returns a CoderResult object to describe its reason for termination:

<ul>

<li>

CoderResult#UNDERFLOW indicates that as much of the input buffer as possible has been encoded. If there is no further input then the invoker can proceed to the next step of the encoding operation. Otherwise this method should be invoked again with further input.

</li>

<li>

CoderResult#OVERFLOW indicates that there is insufficient space in the output buffer to encode any more characters. This method should be invoked again with an output buffer that has more Buffer#remaining remaining bytes. This is typically done by draining any encoded bytes from the output buffer.

</li>

<li>

A CoderResult#malformedForLength malformed-input 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 CoderResult#length() length method. This case applies only if the #onMalformedInput malformed action of this encoder is CodingErrorAction#REPORT; otherwise the malformed input will be ignored or replaced, as requested.

</li>

<li>

An CoderResult#unmappableForLength unmappable-character 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 CoderResult#length() length method. This case applies only if the #onUnmappableCharacter unmappable action of this encoder is CodingErrorAction#REPORT; otherwise the unmappable character will be ignored or replaced, as requested.

</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.

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 encodeLoop method, interpreting its results, handling error conditions, and reinvoking it as necessary.

Java documentation for java.nio.charset.CharsetEncoder.encode(java.nio.CharBuffer, java.nio.ByteBuffer, boolean).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to