How to decode a stream of bytes representing a character set encoding. - Java Internationalization

Java examples for Internationalization:Charset

Introduction

Charset decoding

Demo Code

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.UnsupportedCharsetException;

public class Main {
  public static void main(String[] argv) throws IOException {
    String charsetName = "ISO-8859-1";
    decodeChannel(Channels.newChannel(System.in), new OutputStreamWriter(
        System.out), Charset.forName(charsetName));
  }/* w  w w  . jav a  2s. co m*/
  public static void decodeChannel(ReadableByteChannel source, Writer writer,
      Charset charset) throws UnsupportedCharsetException, IOException {
    CharsetDecoder decoder = charset.newDecoder();
    decoder.onMalformedInput(CodingErrorAction.REPLACE);
    decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    ByteBuffer bb = ByteBuffer.allocateDirect(16 * 1024);
    CharBuffer cb = CharBuffer.allocate(57);
    CoderResult result = CoderResult.UNDERFLOW;
    boolean eof = false;
    while (!eof) {
      if (result == CoderResult.UNDERFLOW) {
        bb.clear();

        eof = (source.read(bb) == -1);

        bb.flip();
      }
      result = decoder.decode(bb, cb, eof);
      if (result == CoderResult.OVERFLOW) {
        output(cb, writer);
      }
    }
    while (decoder.flush(cb) == CoderResult.OVERFLOW) {
      output(cb, writer);
    }
    output(cb, writer);
    source.close();
    writer.flush();
  }
  static void output(CharBuffer cb, Writer writer) throws IOException {
    cb.flip();
    if (cb.hasRemaining()) {
      writer.write(cb.toString());
    }
    cb.clear();
  }
}

Related Tutorials