use of java.nio.CharBuffer in project robovm by robovm.
the class AbstractCharsetTestCase method internalTestDecode.
/*
* Test decoding.
*/
protected void internalTestDecode(byte[] input, char[] output) {
CharBuffer chb = this.testingCharset.decode(ByteBuffer.wrap(input));
int i = 0;
chb.rewind();
while (chb.hasRemaining() && i < output.length) {
assertEquals(output[i], chb.get());
i++;
}
assertFalse(chb.hasRemaining());
assertEquals(output.length, i);
}
use of java.nio.CharBuffer in project robovm by robovm.
the class CharsetDecoderTest method implTestDecodeByteBuffer.
void implTestDecodeByteBuffer() throws CharacterCodingException {
// Null pointer
try {
decoder.decode(null);
fail("should throw null pointer exception");
} catch (NullPointerException e) {
}
// empty input buffer
CharBuffer out = decoder.decode(ByteBuffer.allocate(0));
assertCharBufferValue("", out);
// normal case
ByteBuffer in = getByteBuffer();
out = decoder.decode(in);
assertEquals(0, out.position());
assertEquals(getString().length(), out.limit());
assertEquals(getString().length(), out.remaining());
assertCharBufferValue(getString(), out);
// normal read only case
in = getByteBuffer().asReadOnlyBuffer();
out = decoder.decode(in);
assertEquals(out.position(), 0);
assertEquals(out.limit(), getString().length());
assertEquals(out.remaining(), getString().length());
assertCharBufferValue(getString(), out);
}
use of java.nio.CharBuffer in project robovm by robovm.
the class CharsetDecoderTest method testDecodeFalseIllegalState.
// test illegal states for two decode method with endOfInput is false
public void testDecodeFalseIllegalState() throws CharacterCodingException {
ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 });
CharBuffer out = CharBuffer.allocate(5);
// Normal case: just created
decoder.decode(in, out, false);
in.rewind();
out.rewind();
// Illegal state: just after decode facade
decoder.reset();
decoder.decode(in);
in.rewind();
try {
decoder.decode(in, out, false);
fail("should illegal state");
} catch (IllegalStateException e) {
}
in.rewind();
out.rewind();
// Illegal state: just after decode with that endOfInput is true
decoder.reset();
decoder.decode(in, CharBuffer.allocate(30), true);
in.rewind();
try {
decoder.decode(in, out, false);
fail("should illegal state");
} catch (IllegalStateException e) {
}
in.rewind();
out.rewind();
// Normal case:just after decode with that endOfInput is false
decoder.reset();
decoder.decode(in, CharBuffer.allocate(30), false);
in.rewind();
decoder.decode(in, out, false);
in.rewind();
out.rewind();
// Illegal state: just after flush
decoder.reset();
decoder.decode(in, CharBuffer.allocate(30), true);
in.rewind();
decoder.flush(CharBuffer.allocate(10));
try {
decoder.decode(in, out, false);
fail("should illegal state");
} catch (IllegalStateException e) {
}
}
use of java.nio.CharBuffer in project robovm by robovm.
the class CharsetDecoderTest method testDecodeByteBufferException.
public void testDecodeByteBufferException() throws CharacterCodingException, UnsupportedEncodingException {
CharBuffer out;
ByteBuffer in;
String replaceStr = decoder.replacement() + getString();
// MalformedException:
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
in = getMalformedByteBuffer();
if (in != null) {
try {
CharBuffer buffer = decoder.decode(in);
assertTrue(buffer.remaining() > 0);
fail("should throw MalformedInputException");
} catch (MalformedInputException e) {
}
decoder.reset();
in.rewind();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
out = decoder.decode(in);
assertCharBufferValue(getString(), out);
decoder.reset();
in.rewind();
decoder.onMalformedInput(CodingErrorAction.REPLACE);
out = decoder.decode(in);
assertCharBufferValue(replaceStr, out);
}
// Unmapped Exception:
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
in = getUnmappedByteBuffer();
if (in != null) {
try {
decoder.decode(in);
fail("should throw UnmappableCharacterException");
} catch (UnmappableCharacterException e) {
}
decoder.reset();
in.rewind();
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
out = decoder.decode(in);
assertCharBufferValue(getString(), out);
decoder.reset();
in.rewind();
decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
out = decoder.decode(in);
assertCharBufferValue(replaceStr, out);
}
// RuntimeException
try {
decoder.decode(getExceptionByteArray());
fail("should throw runtime exception");
} catch (RuntimeException e) {
}
}
use of java.nio.CharBuffer in project robovm by robovm.
the class CharsetDecoderTest method testFlush.
/*
* test flush
*/
public void testFlush() throws CharacterCodingException {
CharBuffer out = CharBuffer.allocate(10);
ByteBuffer in = ByteBuffer.wrap(new byte[] { 12, 12 });
decoder.decode(in, out, true);
assertSame(CoderResult.UNDERFLOW, decoder.flush(out));
decoder.reset();
decoder.decode((ByteBuffer) in.rewind(), (CharBuffer) out.rewind(), true);
assertSame(CoderResult.UNDERFLOW, decoder.flush(CharBuffer.allocate(10)));
}
Aggregations