CharsetEncoder.encode(CharBuffer in) has the following syntax.
public final ByteBuffer encode(CharBuffer in) throws CharacterCodingException
In the following code shows how to use CharsetEncoder.encode(CharBuffer in) method.
//from www . j av a 2 s. c o m 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"; System.out.println(encoder.encode(CharBuffer.wrap(response))); } }
The code above generates the following result.