Java - Use character set encoder/decoder

Description

Use character set encoder/decoder

Demo

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;

public class Main {
  public static void main(String[] args) throws Exception {
    MyData dss = new MyData();
    System.out.println("Original Text:from book2s.com");
    encode(dss, "UTF-8");
    System.out.println("Decoded Text:");
    decode(dss, "UTF-8");
  }/*  ww  w.  ja  va 2  s .  c  o  m*/

  public static void encode(MyData ds, String charset) {
    CharsetEncoder encoder = Charset.forName(charset).newEncoder();
    CharBuffer input = CharBuffer.allocate(8);
    ByteBuffer output = ByteBuffer.allocate(8);

    boolean endOfInput = false;
    CoderResult result = CoderResult.UNDERFLOW;

    while (!endOfInput) {
      if (result == CoderResult.UNDERFLOW) {
        input.clear();
        endOfInput = (ds.getCharData(input) == -1);
        input.flip();
      }
      result = encoder.encode(input, output, endOfInput);
      if (result == CoderResult.OVERFLOW
          || (endOfInput && result == CoderResult.UNDERFLOW)) {
        output.flip();
        ds.storeByteData(output);
        output.clear();
      }
    }
    while (true) {
      output.clear();
      result = encoder.flush(output);
      output.flip();
      if (output.hasRemaining()) {
        ds.storeByteData(output);
        output.clear();
      }
      if (result == CoderResult.UNDERFLOW) {
        break;
      }
    }
  }

  public static void decode(MyData dss, String charset) {
    CharsetDecoder decoder = Charset.forName(charset).newDecoder();
    ByteBuffer input = ByteBuffer.allocate(8);
    CharBuffer output = CharBuffer.allocate(8);

    boolean endOfInput = false;
    CoderResult result = CoderResult.UNDERFLOW;

    while (!endOfInput) {
      if (result == CoderResult.UNDERFLOW) {
        input.clear();
        endOfInput = (dss.getByteData(input) == -1);
        input.flip();
      }
      result = decoder.decode(input, output, endOfInput);
      if (result == CoderResult.OVERFLOW
          || (endOfInput && result == CoderResult.UNDERFLOW)) {
        output.flip();
        while (output.hasRemaining()) {
          System.out.print(output.get());
        }
        output.clear();
      }
    }
    while (true) {
      output.clear();
      result = decoder.flush(output);
      output.flip();
      while (output.hasRemaining()) {
        System.out.print(output.get());
      }
      if (result == CoderResult.UNDERFLOW) {
        break;
      }
    }
  }
}

class MyData {
  private CharBuffer cBuffer = CharBuffer.wrap("from book2s.com");
  private ByteBuffer bBuffer = null;

  public int getByteData(ByteBuffer buffer) {
    if (!bBuffer.hasRemaining()) {
      return -1;
    }
    int count = 0;
    while (bBuffer.hasRemaining() && buffer.hasRemaining()) {
      buffer.put(bBuffer.get());
      count++;
    }
    return count;
  }

  public int getCharData(CharBuffer buffer) {
    if (!cBuffer.hasRemaining()) {
      return -1;
    }
    int count = 0;
    while (cBuffer.hasRemaining() && buffer.hasRemaining()) {
      buffer.put(cBuffer.get());
      count++;
    }
    return count;
  }

  public void storeByteData(ByteBuffer byteData) {
    if (this.bBuffer == null) {
      int total = byteData.remaining();
      this.bBuffer = ByteBuffer.allocate(total);
      while (byteData.hasRemaining()) {
        this.bBuffer.put(byteData.get());
      }
      this.bBuffer.flip();
    } else {
      this.bBuffer = this.appendContent(byteData);
    }
  }
  private ByteBuffer appendContent(ByteBuffer content) {
    int count = bBuffer.limit() + content.remaining();
    ByteBuffer newBuffer = ByteBuffer.allocate(count);
    bBuffer.clear();
    newBuffer.put(bBuffer);
    newBuffer.put(content);
    bBuffer.clear();
    newBuffer.clear();
    return newBuffer;
  }
}

Result

Related Topic