use of java.nio.CharBuffer in project robovm by robovm.
the class OldCharsetEncoderDecoderBufferTest method testEncoderInputBuffer.
/* Checks for a buffer corruption that happens in ICU
* (CharsetEncoderICU) when an encode operation
* is done first with an in-buffer with hasArray()==true, and next with an in-buffer with
* hasArray()==false. In that situation ICU may overwrite the array of the first in-buffer.
*/
public void testEncoderInputBuffer() {
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
ByteBuffer out = ByteBuffer.wrap(new byte[10]);
char[] inArray = { 'a', 'b' };
CharBuffer inWithArray = CharBuffer.wrap(inArray);
assertTrue(inWithArray.hasArray());
encoder.encode(inWithArray, out, false);
assertEquals('a', inArray[0]);
assertEquals('b', inArray[1]);
CharBuffer inWithoutArray = CharBuffer.wrap("x");
assertFalse(inWithoutArray.hasArray());
encoder.encode(inWithoutArray, out, true);
// check whether the second decode corrupted the first buffer
assertEquals('a', inArray[0]);
assertEquals('b', inArray[1]);
}
use of java.nio.CharBuffer in project robovm by robovm.
the class OldCharset_AbstractTest method test_CodecDynamic.
public void test_CodecDynamic() throws CharacterCodingException {
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
decoder.onMalformedInput(CodingErrorAction.REPORT);
CharBuffer inputCB = CharBuffer.allocate(65536);
for (int code = 32; code <= 65533; ++code) {
// even for charsets like US-ASCII. http://b/10310751
if (code >= 0xd800 && code <= 0xdfff) {
continue;
}
if (encoder.canEncode((char) code)) {
inputCB.put((char) code);
}
}
inputCB.rewind();
ByteBuffer intermediateBB = encoder.encode(inputCB);
inputCB.rewind();
intermediateBB.rewind();
CharBuffer outputCB = decoder.decode(intermediateBB);
outputCB.rewind();
assertEqualCBs("decode(encode(A)) must be identical with A!", inputCB, outputCB);
}
use of java.nio.CharBuffer in project jvm-serializers by eishay.
the class JsonFormat method toStringBuilder.
// TODO(chrisn): See if working around java.io.Reader#read(CharBuffer)
// overhead is worthwhile
private static StringBuilder toStringBuilder(Readable input) throws IOException {
StringBuilder text = new StringBuilder();
CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE);
while (true) {
int n = input.read(buffer);
if (n == -1) {
break;
}
buffer.flip();
text.append(buffer, 0, n);
}
return text;
}
use of java.nio.CharBuffer in project buck by facebook.
the class NulTerminatedCharsetDecoderTest method nulTerminatedBufferDecodesContentsToBuffer.
@Test
public void nulTerminatedBufferDecodesContentsToBuffer() {
NulTerminatedCharsetDecoder decoder = new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder());
// U+1F4A9 in UTF-8
ByteBuffer in = decodeHex("F09F92A900");
CharBuffer out = CharBuffer.allocate(2);
assertThat(in.position(), is(equalTo(0)));
assertThat(in.limit(), is(equalTo(5)));
assertThat(out.position(), is(equalTo(0)));
assertThat(out.limit(), is(equalTo(2)));
NulTerminatedCharsetDecoder.Result result = decoder.decode(in, out, true);
assertThat(result, is(equalTo(new NulTerminatedCharsetDecoder.Result(true, CoderResult.UNDERFLOW))));
assertThat(in.position(), is(equalTo(5)));
assertThat(in.limit(), is(equalTo(5)));
assertThat(out.position(), is(equalTo(2)));
assertThat(out.limit(), is(equalTo(2)));
out.flip();
// U+1F4A9 in Java
assertThat(out.toString(), is(equalTo("💩")));
}
use of java.nio.CharBuffer in project buck by facebook.
the class NulTerminatedCharsetDecoderTest method nulTerminatedEmptyBufferDecodesToEmptyBuffer.
@Test
public void nulTerminatedEmptyBufferDecodesToEmptyBuffer() {
NulTerminatedCharsetDecoder decoder = new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder());
ByteBuffer in = decodeHex("00");
CharBuffer out = CharBuffer.allocate(0);
assertThat(in.position(), is(equalTo(0)));
assertThat(in.limit(), is(equalTo(1)));
assertThat(out.position(), is(equalTo(0)));
assertThat(out.limit(), is(equalTo(0)));
NulTerminatedCharsetDecoder.Result result = decoder.decode(in, out, true);
assertThat(in.position(), is(equalTo(1)));
assertThat(in.limit(), is(equalTo(1)));
assertThat(out.position(), is(equalTo(0)));
assertThat(out.limit(), is(equalTo(0)));
assertThat(result, is(equalTo(new NulTerminatedCharsetDecoder.Result(true, CoderResult.UNDERFLOW))));
}
Aggregations