Search in sources :

Example 11 with CharsetDecoder

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

the class CharsetDecoderTest method testCharsetDecoder.

/*
     * test constructor
     */
public void testCharsetDecoder() {
    // default value
    decoder = new MockCharsetDecoder(cs, (float) AVER_BYTES, MAX_BYTES);
    // normal case
    CharsetDecoder ec = new MockCharsetDecoder(cs, 1, MAX_BYTES);
    assertSame(ec.charset(), cs);
    assertEquals(1.0, ec.averageCharsPerByte(), 0.0);
    assertTrue(ec.maxCharsPerByte() == MAX_BYTES);
    /*
         * ------------------------ Exceptional cases -------------------------
         */
    // Normal case: null charset
    ec = new MockCharsetDecoder(null, 1, MAX_BYTES);
    assertNull(ec.charset());
    assertEquals(1.0, ec.averageCharsPerByte(), 0.0);
    assertTrue(ec.maxCharsPerByte() == MAX_BYTES);
    ec = new MockCharsetDecoder(new CharsetEncoderTest.MockCharset("mock", new String[0]), 1, MAX_BYTES);
    // Illegal Argument: zero length
    try {
        ec = new MockCharsetDecoder(cs, 0, MAX_BYTES);
        fail("should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    }
    try {
        ec = new MockCharsetDecoder(cs, 1, 0);
        fail("should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    }
    // Illegal Argument: negative length
    try {
        ec = new MockCharsetDecoder(cs, -1, MAX_BYTES);
        fail("should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    }
    try {
        ec = new MockCharsetDecoder(cs, 1, -1);
        fail("should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    }
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder)

Example 12 with CharsetDecoder

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

the class CharsetDecoderTest method test_ByteArray_decode_with_offset_using_facade_method.

// http://code.google.com/p/android/issues/detail?id=4237
public void test_ByteArray_decode_with_offset_using_facade_method() 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;
    CharBuffer outBuffer = decoder.decode(ByteBuffer.wrap(arr, offset, arr.length - offset));
    assertEquals("Android", outBuffer.toString().trim());
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer)

Example 13 with CharsetDecoder

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

Example 14 with CharsetDecoder

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

the class CharsetDecoderTest method testInvalidDecoding.

public void testInvalidDecoding() throws IOException {
    byte[][] invalidSequences = new byte[][] { // overlong NULL
    { (byte) 0xC0, (byte) 0x80 }, // overlong ascii 'A'
    { (byte) 0xC0, (byte) 0xC1 }, // overlong "/../"
    { (byte) 0x2F, (byte) 0xC0, (byte) 0xAE, (byte) 0x2E, (byte) 0x2F }, // Invalid encoding 2r11111000 (sequence too long)
    { (byte) 0xF8 }, // Invalid encoding 2r10000000 (sequence too short)
    { (byte) 0x80 } };
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    /*
         * When bytebuffer has a backing array...
         */
    for (byte[] bytes : invalidSequences) {
        try {
            CharBuffer cb = decoder.decode(ByteBuffer.wrap(bytes));
            fail("No exception thrown on " + Arrays.toString(bytes) + " '" + cb + "'");
        } catch (MalformedInputException expected) {
        }
    }
    /*
         * When bytebuffer has _not_ got a backing array...
         */
    for (byte[] bytes : invalidSequences) {
        try {
            ByteBuffer bb = ByteBuffer.allocateDirect(8);
            bb.put(bytes).flip();
            CharBuffer cb = decoder.decode(bb);
            fail("No exception thrown on " + Arrays.toString(bytes) + " '" + cb + "'");
        } catch (MalformedInputException expected) {
        }
    }
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) MalformedInputException(java.nio.charset.MalformedInputException) ByteBuffer(java.nio.ByteBuffer)

Example 15 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project lombok by rzwitserloot.

the class EmptyLombokFileObject method getDecoder.

@Override
public CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
    CodingErrorAction action = ignoreEncodingErrors ? CodingErrorAction.REPLACE : CodingErrorAction.REPORT;
    return decoder.onMalformedInput(action).onUnmappableCharacter(action);
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CodingErrorAction(java.nio.charset.CodingErrorAction)

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