Search in sources :

Example 56 with CharBuffer

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]);
}
Also used : CharBuffer(java.nio.CharBuffer) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 57 with CharBuffer

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);
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 58 with CharBuffer

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;
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 59 with CharBuffer

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("💩")));
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 60 with CharBuffer

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))));
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

CharBuffer (java.nio.CharBuffer)401 ByteBuffer (java.nio.ByteBuffer)152 CoderResult (java.nio.charset.CoderResult)83 CharsetDecoder (java.nio.charset.CharsetDecoder)47 IOException (java.io.IOException)45 Charset (java.nio.charset.Charset)33 Test (org.junit.Test)23 CharacterCodingException (java.nio.charset.CharacterCodingException)15 CharsetEncoder (java.nio.charset.CharsetEncoder)15 FileInputStream (java.io.FileInputStream)11 IntBuffer (java.nio.IntBuffer)10 Reader (java.io.Reader)9 BufferOverflowException (java.nio.BufferOverflowException)9 DoubleBuffer (java.nio.DoubleBuffer)9 FloatBuffer (java.nio.FloatBuffer)9 LongBuffer (java.nio.LongBuffer)9 ShortBuffer (java.nio.ShortBuffer)9 BufferUnderflowException (java.nio.BufferUnderflowException)7 ValueWrapper (org.apache.geode.internal.memcached.ValueWrapper)7 InputStream (java.io.InputStream)6