Java IO Tutorial - Java Charset.decode(ByteBuffer bb)








Syntax

Charset.decode(ByteBuffer bb) has the following syntax.

public final CharBuffer decode(ByteBuffer bb)

Example

In the following code shows how to use Charset.decode(ByteBuffer bb) method.

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
/*from   w ww.  j  a v  a 2  s  .  c  o  m*/
public class Main {

  public static void print(ByteBuffer bb) {
    while (bb.hasRemaining())
      System.out.print(bb.get() + " ");
    System.out.println();
    bb.rewind();
  }

  public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] {(byte) 'j',(byte) 'a',(byte) 'v',(byte) 'a'  ,(byte) '2' });
    System.out.println("Initial Byte Buffer");
    print(bb);
    Charset csets = Charset.forName("UTF-8");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

  }
}

The code above generates the following result.