use of java.nio.charset.CoderResult in project randomizedtesting by randomizedtesting.
the class WriterOutputStream method processInput.
/**
* Decode the contents of the input ByteBuffer into a CharBuffer.
*
* @param endOfInput indicates end of input
* @throws IOException if an I/O error occurs
*/
private void processInput(boolean endOfInput) throws IOException {
// Prepare decoderIn for reading
decoderIn.flip();
CoderResult coderResult;
while (true) {
coderResult = decoder.decode(decoderIn, decoderOut, endOfInput);
if (coderResult.isOverflow()) {
flushOutput();
} else if (coderResult.isUnderflow()) {
break;
} else {
// so we should not get here.
throw new IOException("Unexpected coder result");
}
}
// Discard the bytes that have been read
decoderIn.compact();
}
use of java.nio.charset.CoderResult in project robovm by robovm.
the class CharsetEncoderTest method implTestEncodeCharBufferByteBufferbooleanException.
protected void implTestEncodeCharBufferByteBufferbooleanException(boolean endOfInput) throws CharacterCodingException {
ByteBuffer out = ByteBuffer.allocate(100);
// MalformedException:
CharBuffer in = getMalformedCharBuffer();
encoder.onMalformedInput(CodingErrorAction.REPORT);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
if (in != null) {
encoder.reset();
CoderResult r = encoder.encode(in, out, endOfInput);
assertTrue(r.isMalformed());
encoder.reset();
out.clear();
in.rewind();
encoder.onMalformedInput(CodingErrorAction.IGNORE);
assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, endOfInput));
assertCodingErrorAction(endOfInput, out, in, unibytes);
encoder.reset();
out.clear();
in.rewind();
encoder.onMalformedInput(CodingErrorAction.REPLACE);
assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, endOfInput));
assertCodingErrorAction(endOfInput, out, in, unibytesWithRep);
} else {
System.out.println("Cannot find malformed char buffer for " + cs.name());
}
// Unmapped Exception:
in = getUnmapCharBuffer();
encoder.onMalformedInput(CodingErrorAction.REPORT);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
if (in != null) {
encoder.reset();
out.clear();
assertTrue(encoder.encode(in, out, endOfInput).isUnmappable());
encoder.reset();
out.clear();
in.rewind();
encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, endOfInput));
assertCodingErrorAction(endOfInput, out, in, unibytes);
encoder.reset();
out.clear();
in.rewind();
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, endOfInput));
assertCodingErrorAction(endOfInput, out, in, unibytesWithRep);
} else {
System.out.println("Cannot find unmapped char buffer for " + cs.name());
}
// RuntimeException
try {
encoder.encode(getExceptionCharBuffer());
fail("should throw runtime exception");
} catch (RuntimeException e) {
}
}
use of java.nio.charset.CoderResult 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.CoderResult in project robovm by robovm.
the class CharsetEncoderTest method testSurrogatePairAllAtOnce.
public void testSurrogatePairAllAtOnce() throws Exception {
// okay: surrogate pair seen all at once is decoded to U+20b9f.
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);
assertEquals(CoderResult.UNDERFLOW, cr);
assertEquals(4, 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));
}
use of java.nio.charset.CoderResult in project robovm by robovm.
the class OutputStreamWriter method drainEncoder.
private void drainEncoder() throws IOException {
// Strictly speaking, I think it's part of the CharsetEncoder contract that you call
// encode with endOfInput true before flushing. Our ICU-based implementations don't
// actually need this, and you'd hope that any reasonable implementation wouldn't either.
// CharsetEncoder.encode doesn't actually pass the boolean through to encodeLoop anyway!
CharBuffer chars = CharBuffer.allocate(0);
while (true) {
CoderResult result = encoder.encode(chars, bytes, true);
if (result.isError()) {
result.throwException();
} else if (result.isOverflow()) {
flushBytes(false);
continue;
}
break;
}
// Some encoders (such as ISO-2022-JP) have stuff to write out after all the
// characters (such as shifting back into a default state). In our implementation,
// this is actually the first time ICU is told that we've run out of input.
CoderResult result = encoder.flush(bytes);
while (!result.isUnderflow()) {
if (result.isOverflow()) {
flushBytes(false);
result = encoder.flush(bytes);
} else {
result.throwException();
}
}
}
Aggregations