use of java.nio.charset.CharsetDecoder in project robovm by robovm.
the class CharsetDecoderTest method testUtf8BytesSplitAcrossMultipleWrites.
public void testUtf8BytesSplitAcrossMultipleWrites() throws Exception {
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
CharBuffer cb = CharBuffer.allocate(128);
CoderResult cr;
cr = decoder.decode(ByteBuffer.wrap(new byte[] { (byte) 0xe2 }), cb, false);
assertEquals(CoderResult.UNDERFLOW, cr);
cr = decoder.decode(ByteBuffer.wrap(new byte[] { (byte) 0x98 }), cb, false);
assertEquals(CoderResult.UNDERFLOW, cr);
cr = decoder.decode(ByteBuffer.wrap(new byte[] { (byte) 0x83 }), cb, false);
assertEquals(CoderResult.UNDERFLOW, cr);
cr = decoder.decode(ByteBuffer.wrap(new byte[0]), cb, true);
assertEquals(CoderResult.UNDERFLOW, cr);
cr = decoder.flush(cb);
assertEquals(CoderResult.UNDERFLOW, cr);
assertEquals(1, cb.position());
assertEquals('☃', cb.get(0));
}
use of java.nio.charset.CharsetDecoder in project robovm by robovm.
the class OldCharsetEncoderDecoderBufferTest method testDecoderInputBuffer.
/* Checks for a buffer corruption that happens in ICU
* (CharsetDecoderICU) when a decode operation
* is done first with an in-buffer with hasArray()==true, and next with an in-buffer with
* hasArray()==false. In that situation ICU may overwrite the array of the first in-buffer.
*/
public void testDecoderInputBuffer() {
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
CharBuffer out = CharBuffer.wrap(new char[10]);
byte[] inArray = { (byte) 'a', (byte) 'b' };
ByteBuffer inWithArray = ByteBuffer.wrap(inArray);
assertTrue(inWithArray.hasArray());
decoder.decode(inWithArray, out, false);
assertEquals('a', inArray[0]);
assertEquals('b', inArray[1]);
// A read-only ByteBuffer must not expose its array.
ByteBuffer inWithoutArray = ByteBuffer.wrap(new byte[] { (byte) 'x' }).asReadOnlyBuffer();
assertFalse(inWithoutArray.hasArray());
decoder.decode(inWithoutArray, out, true);
// check whether the first buffer was corrupted by the second decode
assertEquals('a', inArray[0]);
assertEquals('b', inArray[1]);
}
use of java.nio.charset.CharsetDecoder in project robovm by robovm.
the class OldCharsetEncoderDecoderBufferTest method testDecoderOutputBuffer.
/* Checks for a buffer corruption that happens in ICU
* (CharsetDecoderICU) when a decode operation
* is done first with an out-buffer with hasArray()==true, and next with an out-buffer with
* hasArray()==false. In that situation ICU may overwrite the first out-buffer.
*/
public void testDecoderOutputBuffer() {
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
char[] cBuf = new char[10];
CharBuffer out = CharBuffer.wrap(cBuf);
assertTrue(out.hasArray());
decoder.decode(ByteBuffer.wrap(new byte[] { (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd' }), out, false);
assertEquals("abcd", new String(cBuf, 0, 4));
assertEquals(0, cBuf[4]);
assertEquals(0, cBuf[5]);
byte[] bBuf = new byte[10];
out = ByteBuffer.wrap(bBuf).asCharBuffer();
assertFalse(out.hasArray());
decoder.decode(ByteBuffer.wrap(new byte[] { (byte) 'x' }), out, true);
assertEquals('x', bBuf[1]);
assertEquals(0, bBuf[3]);
// check if the first buffer was corrupted by the second decode
assertEquals("abcd", new String(cBuf, 0, 4));
assertEquals(0, cBuf[4]);
assertEquals(0, cBuf[5]);
}
use of java.nio.charset.CharsetDecoder in project robovm by robovm.
the class CharsetDecoderTest method test_ByteArray_decode_no_offset.
// http://code.google.com/p/android/issues/detail?id=4237
public void test_ByteArray_decode_no_offset() throws Exception {
CharsetDecoder decoder = Charset.forName("UTF-16").newDecoder();
byte[] arr = encode("UTF-16", "Android");
ByteBuffer inBuffer = ByteBuffer.wrap(arr, 0, arr.length).slice();
CharBuffer outBuffer = CharBuffer.allocate(arr.length);
decoder.reset();
CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true);
assertFalse(coderResult.toString(), coderResult.isError());
decoder.flush(outBuffer);
outBuffer.flip();
assertEquals("Android", outBuffer.toString().trim());
}
use of java.nio.charset.CharsetDecoder in project robovm by robovm.
the class CharsetDecoderTest method test_replaceWith.
// None of the harmony or jtreg tests actually check that replaceWith does the right thing!
public void test_replaceWith() throws Exception {
CharsetDecoder d = Charset.forName("UTF-16").newDecoder();
d.replaceWith("x");
d.onMalformedInput(CodingErrorAction.REPLACE);
d.onUnmappableCharacter(CodingErrorAction.REPLACE);
ByteBuffer in = ByteBuffer.wrap(new byte[] { 109, 97, 109 });
assertEquals("浡x", d.decode(in).toString());
}
Aggregations