use of java.nio.charset.MalformedInputException in project robovm by robovm.
the class ASCIICharsetEncoderTest method testEncodeMapping.
public void testEncodeMapping() throws CharacterCodingException {
encoder.reset();
for (int i = 0; i <= MAXCODEPOINT; i++) {
char[] chars = Character.toChars(i);
CharBuffer cb = CharBuffer.wrap(chars);
ByteBuffer bb = encoder.encode(cb);
assertEquals(i, bb.get(0));
}
CharBuffer cb = CharBuffer.wrap("");
try {
encoder.encode(cb);
} catch (UnmappableCharacterException e) {
//expected
}
cb = CharBuffer.wrap("�");
try {
encoder.encode(cb);
} catch (MalformedInputException e) {
//expected
}
ByteBuffer bb = ByteBuffer.allocate(0x10);
cb = CharBuffer.wrap("A");
encoder.reset();
encoder.encode(cb, bb, false);
try {
encoder.encode(cb);
} catch (IllegalStateException e) {
//expected
}
}
use of java.nio.charset.MalformedInputException in project robovm by robovm.
the class CharsetDecoderTest method test_decode.
/**
* @tests java.nio.charset.CharsetDecoder#decode(java.nio.ByteBuffer)
*/
public void test_decode() throws CharacterCodingException {
// Regression for HARMONY-33
// ByteBuffer bb = ByteBuffer.allocate(1);
// bb.put(0, (byte) 77);
// CharsetDecoder decoder = Charset.forName("UTF-16").newDecoder();
// decoder.onMalformedInput(CodingErrorAction.REPLACE);
// decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
// decoder.decode(bb);
// Regression for HARMONY-67
// byte[] b = new byte[] { (byte) 1 };
// ByteBuffer buf = ByteBuffer.wrap(b);
// CharBuffer charbuf = Charset.forName("UTF-16").decode(buf);
// assertEquals("Assert 0: charset UTF-16", 1, charbuf.length());
//
// charbuf = Charset.forName("UTF-16BE").decode(buf);
// assertEquals("Assert 1: charset UTF-16BE", 0, charbuf.length());
//
// charbuf = Charset.forName("UTF-16LE").decode(buf);
// assertEquals("Assert 2: charset UTF16LE", 0, charbuf.length());
// Regression for HARMONY-99
CharsetDecoder decoder2 = Charset.forName("UTF-16").newDecoder();
decoder2.onMalformedInput(CodingErrorAction.REPORT);
decoder2.onUnmappableCharacter(CodingErrorAction.REPORT);
ByteBuffer in = ByteBuffer.wrap(new byte[] { 109, 97, 109 });
try {
decoder2.decode(in);
fail("Assert 3: MalformedInputException should have thrown");
} catch (MalformedInputException e) {
//expected
}
}
use of java.nio.charset.MalformedInputException in project robovm by robovm.
the class MalformedInputExceptionTest method testConstructor.
public void testConstructor() {
MalformedInputException ex = new MalformedInputException(3);
assertTrue(ex instanceof CharacterCodingException);
assertNull(ex.getCause());
assertEquals(ex.getInputLength(), 3);
assertTrue(ex.getMessage().indexOf("3") != -1);
ex = new MalformedInputException(-3);
assertNull(ex.getCause());
assertEquals(ex.getInputLength(), -3);
assertTrue(ex.getMessage().indexOf("-3") != -1);
ex = new MalformedInputException(0);
assertNull(ex.getCause());
assertEquals(ex.getInputLength(), 0);
assertTrue(ex.getMessage().indexOf("0") != -1);
}
use of java.nio.charset.MalformedInputException in project robovm by robovm.
the class CharsetEncoderTest method testEncodeCharBufferException.
public void testEncodeCharBufferException() throws CharacterCodingException {
ByteBuffer out;
CharBuffer in;
// MalformedException:
in = getMalformedCharBuffer();
encoder.onMalformedInput(CodingErrorAction.REPORT);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
if (in != null) {
try {
// regression test for Harmony-1379
encoder.encode(in);
fail("should throw MalformedInputException");
} catch (MalformedInputException e) {
}
encoder.reset();
in.rewind();
encoder.onMalformedInput(CodingErrorAction.IGNORE);
out = encoder.encode(in);
assertByteArray(out, addSurrogate(unibytes));
encoder.reset();
in.rewind();
encoder.onMalformedInput(CodingErrorAction.REPLACE);
out = encoder.encode(in);
assertByteArray(out, addSurrogate(unibytesWithRep));
}
// Unmapped Exception:
in = getUnmapCharBuffer();
encoder.onMalformedInput(CodingErrorAction.REPORT);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
if (in != null) {
encoder.reset();
try {
encoder.encode(in);
fail("should throw UnmappableCharacterException");
} catch (UnmappableCharacterException e) {
}
encoder.reset();
in.rewind();
encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
out = encoder.encode(in);
assertByteArray(out, unibytes);
encoder.reset();
in.rewind();
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
out = encoder.encode(in);
assertByteArray(out, unibytesWithRep);
}
// RuntimeException
try {
encoder.encode(getExceptionCharBuffer());
fail("should throw runtime exception");
} catch (RuntimeException e) {
}
}
Aggregations