Java IO Tutorial - Java CharsetDecoder .decode (ByteBuffer in)


CharsetDecoder.decode(ByteBuffer in) has the following syntax.

public final CharBuffer decode(ByteBuffer in)   throws CharacterCodingException


In the following code shows how to use CharsetDecoder.decode(ByteBuffer in) method.

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
//  ww w . j a v a 2  m
public class Main {
  public static void main(String[] argv) throws Exception {
    Charset charset = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = charset.newDecoder();
    CharsetEncoder encoder = charset.newEncoder();

    ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("a string"));

    CharBuffer cbuf = decoder.decode(bbuf);
    String s = cbuf.toString();