use of java.nio.charset.CharsetDecoder in project robovm by robovm.
the class CharsetDecoderTest method testCharsetDecoder.
/*
* test constructor
*/
public void testCharsetDecoder() {
// default value
decoder = new MockCharsetDecoder(cs, (float) AVER_BYTES, MAX_BYTES);
// normal case
CharsetDecoder ec = new MockCharsetDecoder(cs, 1, MAX_BYTES);
assertSame(ec.charset(), cs);
assertEquals(1.0, ec.averageCharsPerByte(), 0.0);
assertTrue(ec.maxCharsPerByte() == MAX_BYTES);
/*
* ------------------------ Exceptional cases -------------------------
*/
// Normal case: null charset
ec = new MockCharsetDecoder(null, 1, MAX_BYTES);
assertNull(ec.charset());
assertEquals(1.0, ec.averageCharsPerByte(), 0.0);
assertTrue(ec.maxCharsPerByte() == MAX_BYTES);
ec = new MockCharsetDecoder(new CharsetEncoderTest.MockCharset("mock", new String[0]), 1, MAX_BYTES);
// Illegal Argument: zero length
try {
ec = new MockCharsetDecoder(cs, 0, MAX_BYTES);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
try {
ec = new MockCharsetDecoder(cs, 1, 0);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
// Illegal Argument: negative length
try {
ec = new MockCharsetDecoder(cs, -1, MAX_BYTES);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
try {
ec = new MockCharsetDecoder(cs, 1, -1);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
}
use of java.nio.charset.CharsetDecoder in project robovm by robovm.
the class CharsetDecoderTest method test_ByteArray_decode_with_offset_using_facade_method.
// http://code.google.com/p/android/issues/detail?id=4237
public void test_ByteArray_decode_with_offset_using_facade_method() throws Exception {
CharsetDecoder decoder = Charset.forName("UTF-16").newDecoder();
byte[] arr = encode("UTF-16", "Android");
arr = prependByteToByteArray(arr, new Integer(1).byteValue());
int offset = 1;
CharBuffer outBuffer = decoder.decode(ByteBuffer.wrap(arr, offset, arr.length - offset));
assertEquals("Android", outBuffer.toString().trim());
}
use of java.nio.charset.CharsetDecoder in project robovm by robovm.
the class CharsetDecoderTest method test_ByteArray_decode_with_offset.
// http://code.google.com/p/android/issues/detail?id=4237
public void test_ByteArray_decode_with_offset() throws Exception {
CharsetDecoder decoder = Charset.forName("UTF-16").newDecoder();
byte[] arr = encode("UTF-16", "Android");
arr = prependByteToByteArray(arr, new Integer(1).byteValue());
int offset = 1;
ByteBuffer inBuffer = ByteBuffer.wrap(arr, offset, arr.length - offset).slice();
CharBuffer outBuffer = CharBuffer.allocate(arr.length - offset);
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 testInvalidDecoding.
public void testInvalidDecoding() throws IOException {
byte[][] invalidSequences = new byte[][] { // overlong NULL
{ (byte) 0xC0, (byte) 0x80 }, // overlong ascii 'A'
{ (byte) 0xC0, (byte) 0xC1 }, // overlong "/../"
{ (byte) 0x2F, (byte) 0xC0, (byte) 0xAE, (byte) 0x2E, (byte) 0x2F }, // Invalid encoding 2r11111000 (sequence too long)
{ (byte) 0xF8 }, // Invalid encoding 2r10000000 (sequence too short)
{ (byte) 0x80 } };
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
/*
* When bytebuffer has a backing array...
*/
for (byte[] bytes : invalidSequences) {
try {
CharBuffer cb = decoder.decode(ByteBuffer.wrap(bytes));
fail("No exception thrown on " + Arrays.toString(bytes) + " '" + cb + "'");
} catch (MalformedInputException expected) {
}
}
/*
* When bytebuffer has _not_ got a backing array...
*/
for (byte[] bytes : invalidSequences) {
try {
ByteBuffer bb = ByteBuffer.allocateDirect(8);
bb.put(bytes).flip();
CharBuffer cb = decoder.decode(bb);
fail("No exception thrown on " + Arrays.toString(bytes) + " '" + cb + "'");
} catch (MalformedInputException expected) {
}
}
}
use of java.nio.charset.CharsetDecoder in project lombok by rzwitserloot.
the class EmptyLombokFileObject method getDecoder.
@Override
public CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
CodingErrorAction action = ignoreEncodingErrors ? CodingErrorAction.REPLACE : CodingErrorAction.REPORT;
return decoder.onMalformedInput(action).onUnmappableCharacter(action);
}
Aggregations