Java IO Tutorial - Java CharsetDecoder .decode (ByteBuffer in, CharBuffer out, boolean endOfInput)








Syntax

CharsetDecoder.decode(ByteBuffer in, CharBuffer out, boolean endOfInput) has the following syntax.

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

Example

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

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
/*from   ww w .j a  va  2  s .c o  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 = CharBuffer.allocate(10);
    
    decoder.decode(bbuf,cbuf,true);
    String s = cbuf.toString();
  }
}