Java IO Tutorial - Java CharsetEncoder .encode (CharBuffer in, ByteBuffer out, boolean endOfInput)








Syntax

CharsetEncoder.encode(CharBuffer in, ByteBuffer out, boolean endOfInput) has the following syntax.

public final CoderResult encode(CharBuffer in,   ByteBuffer out,   boolean endOfInput)

Example

In the following code shows how to use CharsetEncoder.encode(CharBuffer in, ByteBuffer out, boolean endOfInput) method.

//from  ww w  . j  a va 2s.  c om
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

public class Main {
  public static void main(String[] args) throws Exception{
    CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder();

    String response = "java2s.com";
    
    ByteBuffer bb = ByteBuffer.allocate(10);
    
    System.out.println(encoder.encode(CharBuffer.wrap(response),bb,true));
  }
}

The code above generates the following result.