Java Reference
In-Depth Information
public static void decode(DataSourceSink 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();
}
// Decode the input bytes
result = decoder.decode(input, output, endOfInput);
// Drain output when
// 1. It is an overflow. Or,
// 2. It is an underflow and it is the end of the input
if (result == CoderResult.OVERFLOW ||
(endOfInput && result == CoderResult.UNDERFLOW)) {
output.flip();
while (output.hasRemaining()) {
System.out.print(output.get());
}
output.clear();
}
}
// Flush the internal state of the decoder
while (true) {
output.clear();
result = decoder.flush(output);
output.flip();
while (output.hasRemaining()) {
System.out.print(output.get());
}
if (result == CoderResult.UNDERFLOW) {
break;
}
}
}
}
Search WWH ::




Custom Search