Search in sources :

Example 61 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project robovm by robovm.

the class CharsetDecoderTest method testUtf8BytesSplitAcrossMultipleWrites.

public void testUtf8BytesSplitAcrossMultipleWrites() throws Exception {
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
    CharBuffer cb = CharBuffer.allocate(128);
    CoderResult cr;
    cr = decoder.decode(ByteBuffer.wrap(new byte[] { (byte) 0xe2 }), cb, false);
    assertEquals(CoderResult.UNDERFLOW, cr);
    cr = decoder.decode(ByteBuffer.wrap(new byte[] { (byte) 0x98 }), cb, false);
    assertEquals(CoderResult.UNDERFLOW, cr);
    cr = decoder.decode(ByteBuffer.wrap(new byte[] { (byte) 0x83 }), cb, false);
    assertEquals(CoderResult.UNDERFLOW, cr);
    cr = decoder.decode(ByteBuffer.wrap(new byte[0]), cb, true);
    assertEquals(CoderResult.UNDERFLOW, cr);
    cr = decoder.flush(cb);
    assertEquals(CoderResult.UNDERFLOW, cr);
    assertEquals(1, cb.position());
    assertEquals('☃', cb.get(0));
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) CoderResult(java.nio.charset.CoderResult)

Example 62 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project robovm by robovm.

the class OldCharsetEncoderDecoderBufferTest method testDecoderInputBuffer.

/* Checks for a buffer corruption that happens in ICU
     * (CharsetDecoderICU) when a decode 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 testDecoderInputBuffer() {
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
    CharBuffer out = CharBuffer.wrap(new char[10]);
    byte[] inArray = { (byte) 'a', (byte) 'b' };
    ByteBuffer inWithArray = ByteBuffer.wrap(inArray);
    assertTrue(inWithArray.hasArray());
    decoder.decode(inWithArray, out, false);
    assertEquals('a', inArray[0]);
    assertEquals('b', inArray[1]);
    // A read-only ByteBuffer must not expose its array.
    ByteBuffer inWithoutArray = ByteBuffer.wrap(new byte[] { (byte) 'x' }).asReadOnlyBuffer();
    assertFalse(inWithoutArray.hasArray());
    decoder.decode(inWithoutArray, out, true);
    // check whether the first buffer was corrupted by the second decode
    assertEquals('a', inArray[0]);
    assertEquals('b', inArray[1]);
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 63 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project robovm by robovm.

the class OldCharsetEncoderDecoderBufferTest method testDecoderOutputBuffer.

/* Checks for a buffer corruption that happens in ICU
     * (CharsetDecoderICU) when a decode operation
     * is done first with an out-buffer with hasArray()==true, and next with an out-buffer with
     * hasArray()==false. In that situation ICU may overwrite the first out-buffer.
     */
public void testDecoderOutputBuffer() {
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
    char[] cBuf = new char[10];
    CharBuffer out = CharBuffer.wrap(cBuf);
    assertTrue(out.hasArray());
    decoder.decode(ByteBuffer.wrap(new byte[] { (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd' }), out, false);
    assertEquals("abcd", new String(cBuf, 0, 4));
    assertEquals(0, cBuf[4]);
    assertEquals(0, cBuf[5]);
    byte[] bBuf = new byte[10];
    out = ByteBuffer.wrap(bBuf).asCharBuffer();
    assertFalse(out.hasArray());
    decoder.decode(ByteBuffer.wrap(new byte[] { (byte) 'x' }), out, true);
    assertEquals('x', bBuf[1]);
    assertEquals(0, bBuf[3]);
    // check if the first buffer was corrupted by the second decode
    assertEquals("abcd", new String(cBuf, 0, 4));
    assertEquals(0, cBuf[4]);
    assertEquals(0, cBuf[5]);
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer)

Example 64 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project robovm by robovm.

the class CharsetDecoderTest method test_ByteArray_decode_no_offset.

// http://code.google.com/p/android/issues/detail?id=4237
public void test_ByteArray_decode_no_offset() throws Exception {
    CharsetDecoder decoder = Charset.forName("UTF-16").newDecoder();
    byte[] arr = encode("UTF-16", "Android");
    ByteBuffer inBuffer = ByteBuffer.wrap(arr, 0, arr.length).slice();
    CharBuffer outBuffer = CharBuffer.allocate(arr.length);
    decoder.reset();
    CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true);
    assertFalse(coderResult.toString(), coderResult.isError());
    decoder.flush(outBuffer);
    outBuffer.flip();
    assertEquals("Android", outBuffer.toString().trim());
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 65 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project robovm by robovm.

the class CharsetDecoderTest method test_replaceWith.

// None of the harmony or jtreg tests actually check that replaceWith does the right thing!
public void test_replaceWith() throws Exception {
    CharsetDecoder d = Charset.forName("UTF-16").newDecoder();
    d.replaceWith("x");
    d.onMalformedInput(CodingErrorAction.REPLACE);
    d.onUnmappableCharacter(CodingErrorAction.REPLACE);
    ByteBuffer in = ByteBuffer.wrap(new byte[] { 109, 97, 109 });
    assertEquals("浡x", d.decode(in).toString());
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CharsetDecoder (java.nio.charset.CharsetDecoder)90 CharBuffer (java.nio.CharBuffer)45 ByteBuffer (java.nio.ByteBuffer)33 CoderResult (java.nio.charset.CoderResult)25 Charset (java.nio.charset.Charset)24 InputStreamReader (java.io.InputStreamReader)11 CharacterCodingException (java.nio.charset.CharacterCodingException)9 IOException (java.io.IOException)8 BufferedReader (java.io.BufferedReader)5 Properties (java.util.Properties)5 RegisterRequestProcessor (com.linkedin.databus.container.request.RegisterRequestProcessor)4 LogicalSource (com.linkedin.databus.core.data_model.LogicalSource)4 ChunkedWritableByteChannel (com.linkedin.databus2.core.container.ChunkedWritableByteChannel)4 DatabusRequest (com.linkedin.databus2.core.container.request.DatabusRequest)4 SchemaRegistryService (com.linkedin.databus2.schemas.SchemaRegistryService)4 SourceIdNameRegistry (com.linkedin.databus2.schemas.SourceIdNameRegistry)4 InputStream (java.io.InputStream)4 Reader (java.io.Reader)4 ArrayList (java.util.ArrayList)4 RegisterResponseEntry (com.linkedin.databus2.core.container.request.RegisterResponseEntry)3