use of java.nio.charset.CharsetEncoder in project robovm by robovm.
the class CharsetEncoderTest method testFlushWithoutEndOfInput.
public void testFlushWithoutEndOfInput() throws Exception {
Charset cs = Charset.forName("UTF-32BE");
CharsetEncoder e = cs.newEncoder();
ByteBuffer bb = ByteBuffer.allocate(128);
CoderResult cr = e.encode(CharBuffer.wrap(new char[] { 'x' }), bb, false);
assertEquals(CoderResult.UNDERFLOW, cr);
assertEquals(4, bb.position());
try {
cr = e.flush(bb);
fail();
} catch (IllegalStateException expected) {
// You must call encode with endOfInput true before you can flush.
}
// We had a bug where we wouldn't reset inEnd before calling encode in implFlush.
// That would result in flush outputting garbage.
cr = e.encode(CharBuffer.wrap(new char[] { 'x' }), bb, true);
assertEquals(CoderResult.UNDERFLOW, cr);
assertEquals(8, bb.position());
cr = e.flush(bb);
assertEquals(CoderResult.UNDERFLOW, cr);
assertEquals(8, bb.position());
}
use of java.nio.charset.CharsetEncoder in project robovm by robovm.
the class CharsetEncoderTest method testMalformedSurrogatePair.
public void testMalformedSurrogatePair() throws Exception {
// malformed: low surrogate first is detected as an error.
Charset cs = Charset.forName("UTF-32BE");
CharsetEncoder e = cs.newEncoder();
ByteBuffer bb = ByteBuffer.allocate(128);
CoderResult cr = e.encode(CharBuffer.wrap(new char[] { '�' }), bb, false);
assertTrue(cr.toString(), cr.isMalformed());
assertEquals(1, cr.length());
}
use of java.nio.charset.CharsetEncoder in project robovm by robovm.
the class CharsetEncoderTest method testCharsetEncoderSplitSurrogates.
private void testCharsetEncoderSplitSurrogates(CodingErrorAction cea) throws Exception {
// Writing the two halves of the surrogate pair in separate writes should work just fine.
// This is true of Android and ICU, but not of the RI.
// On the RI, writing the two halves of the surrogate pair in separate writes
// is an error because the CharsetEncoder doesn't remember it's half-way through a
// surrogate pair across the two calls!
// IGNORE just ignores both characters, REPORT complains that the second is
// invalid (because it doesn't remember seeing the first), and REPLACE inserts a
// replacement character U+fffd when it sees the second character (because it too
// doesn't remember seeing the first).
// Android just does the right thing.
Charset cs = Charset.forName("UTF-32BE");
CharsetEncoder e = cs.newEncoder();
e.onMalformedInput(cea);
e.onUnmappableCharacter(cea);
ByteBuffer bb = ByteBuffer.allocate(128);
CoderResult cr = e.encode(CharBuffer.wrap(new char[] { '�' }), bb, false);
assertEquals(CoderResult.UNDERFLOW, cr);
assertEquals(0, bb.position());
cr = e.encode(CharBuffer.wrap(new char[] { '�' }), bb, false);
assertEquals(CoderResult.UNDERFLOW, cr);
int expectedPosition = 4;
assertEquals(expectedPosition, bb.position());
System.err.println(Arrays.toString(Arrays.copyOfRange(bb.array(), 0, bb.position())));
assertEquals((byte) 0x00, bb.get(0));
assertEquals((byte) 0x02, bb.get(1));
assertEquals((byte) 0x0b, bb.get(2));
assertEquals((byte) 0x9f, bb.get(3));
cr = e.encode(CharBuffer.wrap(new char[] {}), bb, true);
assertEquals(CoderResult.UNDERFLOW, cr);
assertEquals(expectedPosition, bb.position());
cr = e.flush(bb);
assertEquals(CoderResult.UNDERFLOW, cr);
assertEquals(expectedPosition, bb.position());
}
use of java.nio.charset.CharsetEncoder in project robovm by robovm.
the class Charset_TestGenerator method genEncoded.
static void genEncoded(Charset charset, CharBuffer cb) {
System.out.println(charset.name());
Dumper out = new Dumper1();
CharsetEncoder encoder = charset.newEncoder();
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
try {
ByteBuffer bb = encoder.encode(cb);
// bb.rewind();
while (bb.hasRemaining()) {
out.consume(bb.get());
}
} catch (CharacterCodingException e) {
System.out.println(e);
// e.printStackTrace();
}
}
use of java.nio.charset.CharsetEncoder in project robovm by robovm.
the class OldCharsetEncoderDecoderBufferTest method testEncoderOutputBuffer.
/* Checks for a buffer corruption that happens in ICU
* (CharsetEncoderICU) when an encode 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 testEncoderOutputBuffer() {
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
byte[] buffer = new byte[10];
ByteBuffer out = ByteBuffer.wrap(buffer);
assertTrue(out.hasArray());
encoder.encode(CharBuffer.wrap("ab"), out, false);
assertEquals('a', buffer[0]);
assertEquals('b', buffer[1]);
assertEquals(0, buffer[2]);
out = ByteBuffer.allocateDirect(10);
// It's no longer possible to get a byte buffer without a backing byte[] on Android.
// This test is useless on Android, unless that changes again. (You can't even
// subclass ByteBuffer because -- although it's non-final -- both the RI and Android
// have [different] package-private abstract methods you'd need to implement but can't.)
//assertFalse(out.hasArray());
encoder.encode(CharBuffer.wrap("x"), out, true);
// check whether the second decode corrupted the first buffer
assertEquals('a', buffer[0]);
assertEquals('b', buffer[1]);
assertEquals(0, buffer[2]);
}
Aggregations