This code throws an exception: import java.nio.*; import java.nio.charset.*; class bug { public static void main(String[] args) throws Exception { byte[] data = new byte[] { (byte)0, (byte)129 }; int offset = 0; int count = 2; CharsetDecoder csd = Charset.forName("Windows-1252").newDecoder(); csd.onUnmappableCharacter(CodingErrorAction.REPLACE); CharBuffer out = CharBuffer.allocate(count * (int)csd.maxCharsPerByte()); csd.decode(ByteBuffer.wrap(data, offset, count), out, true); csd.flush(out); System.out.println(out.flip().toString()); } }
Fixed.