Java examples for Internationalization:Charset
Decode text data with a specific charset
//package com.java2s; import java.io.ByteArrayInputStream; import java.io.IOException; 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; public class Main { /**//from ww w . j a va 2s.com * Decode text data with a specific charset * @param textData:Input data | decodedStr:Decoded data,if only used for charset-checking,set <code>null</code> | charset:Specific charset * @return true:Decode successfully | false:Otherwise * @throws java.io.IOException */ public static boolean decode(byte[] textData, StringBuilder decodedStr, Charset charset) throws IOException { ReadableByteChannel channel = Channels .newChannel(new ByteArrayInputStream(textData)); CharsetDecoder decoder = charset.newDecoder(); ByteBuffer byteBuffer = ByteBuffer.allocate(2048); CharBuffer charBuffer = CharBuffer.allocate(1024); boolean inputEnds = false; while (!inputEnds) { int size = channel.read(byteBuffer); byteBuffer.flip(); if (size == -1) { inputEnds = true; } CoderResult result = null; do { result = decoder.decode(byteBuffer, charBuffer, inputEnds); charBuffer.flip(); if (result.isError()) { return false; } if (decodedStr != null && charBuffer.hasRemaining()) { decodedStr.append(charBuffer.toString()); } charBuffer.clear(); } while (result != CoderResult.UNDERFLOW); byteBuffer.compact(); } CoderResult result = null; do { result = decoder.flush(charBuffer); charBuffer.flip(); if (result.isError()) { return false; } if (decodedStr != null && charBuffer.hasRemaining()) { decodedStr.append(charBuffer); } charBuffer.clear(); } while (result != CoderResult.UNDERFLOW); return true; } }