Search in sources :

Example 41 with CharBuffer

use of java.nio.CharBuffer in project robovm by robovm.

the class AbstractCharsetTestCase method internalTestDecode.

/*
	 * Test decoding.
	 */
protected void internalTestDecode(byte[] input, char[] output) {
    CharBuffer chb = this.testingCharset.decode(ByteBuffer.wrap(input));
    int i = 0;
    chb.rewind();
    while (chb.hasRemaining() && i < output.length) {
        assertEquals(output[i], chb.get());
        i++;
    }
    assertFalse(chb.hasRemaining());
    assertEquals(output.length, i);
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 42 with CharBuffer

use of java.nio.CharBuffer in project robovm by robovm.

the class CharsetDecoderTest method implTestDecodeByteBuffer.

void implTestDecodeByteBuffer() throws CharacterCodingException {
    // Null pointer
    try {
        decoder.decode(null);
        fail("should throw null pointer exception");
    } catch (NullPointerException e) {
    }
    // empty input buffer
    CharBuffer out = decoder.decode(ByteBuffer.allocate(0));
    assertCharBufferValue("", out);
    // normal case
    ByteBuffer in = getByteBuffer();
    out = decoder.decode(in);
    assertEquals(0, out.position());
    assertEquals(getString().length(), out.limit());
    assertEquals(getString().length(), out.remaining());
    assertCharBufferValue(getString(), out);
    // normal read only case
    in = getByteBuffer().asReadOnlyBuffer();
    out = decoder.decode(in);
    assertEquals(out.position(), 0);
    assertEquals(out.limit(), getString().length());
    assertEquals(out.remaining(), getString().length());
    assertCharBufferValue(getString(), out);
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 43 with CharBuffer

use of java.nio.CharBuffer in project robovm by robovm.

the class CharsetDecoderTest method testDecodeFalseIllegalState.

// test illegal states for two decode method with endOfInput is false
public void testDecodeFalseIllegalState() throws CharacterCodingException {
    ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 });
    CharBuffer out = CharBuffer.allocate(5);
    // Normal case: just created
    decoder.decode(in, out, false);
    in.rewind();
    out.rewind();
    // Illegal state: just after decode facade
    decoder.reset();
    decoder.decode(in);
    in.rewind();
    try {
        decoder.decode(in, out, false);
        fail("should illegal state");
    } catch (IllegalStateException e) {
    }
    in.rewind();
    out.rewind();
    // Illegal state: just after decode with that endOfInput is true
    decoder.reset();
    decoder.decode(in, CharBuffer.allocate(30), true);
    in.rewind();
    try {
        decoder.decode(in, out, false);
        fail("should illegal state");
    } catch (IllegalStateException e) {
    }
    in.rewind();
    out.rewind();
    // Normal case:just after decode with that endOfInput is false
    decoder.reset();
    decoder.decode(in, CharBuffer.allocate(30), false);
    in.rewind();
    decoder.decode(in, out, false);
    in.rewind();
    out.rewind();
    // Illegal state: just after flush
    decoder.reset();
    decoder.decode(in, CharBuffer.allocate(30), true);
    in.rewind();
    decoder.flush(CharBuffer.allocate(10));
    try {
        decoder.decode(in, out, false);
        fail("should illegal state");
    } catch (IllegalStateException e) {
    }
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 44 with CharBuffer

use of java.nio.CharBuffer in project robovm by robovm.

the class CharsetDecoderTest method testDecodeByteBufferException.

public void testDecodeByteBufferException() throws CharacterCodingException, UnsupportedEncodingException {
    CharBuffer out;
    ByteBuffer in;
    String replaceStr = decoder.replacement() + getString();
    // MalformedException:
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    in = getMalformedByteBuffer();
    if (in != null) {
        try {
            CharBuffer buffer = decoder.decode(in);
            assertTrue(buffer.remaining() > 0);
            fail("should throw MalformedInputException");
        } catch (MalformedInputException e) {
        }
        decoder.reset();
        in.rewind();
        decoder.onMalformedInput(CodingErrorAction.IGNORE);
        out = decoder.decode(in);
        assertCharBufferValue(getString(), out);
        decoder.reset();
        in.rewind();
        decoder.onMalformedInput(CodingErrorAction.REPLACE);
        out = decoder.decode(in);
        assertCharBufferValue(replaceStr, out);
    }
    // Unmapped Exception:
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    in = getUnmappedByteBuffer();
    if (in != null) {
        try {
            decoder.decode(in);
            fail("should throw UnmappableCharacterException");
        } catch (UnmappableCharacterException e) {
        }
        decoder.reset();
        in.rewind();
        decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
        out = decoder.decode(in);
        assertCharBufferValue(getString(), out);
        decoder.reset();
        in.rewind();
        decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
        out = decoder.decode(in);
        assertCharBufferValue(replaceStr, out);
    }
    // RuntimeException
    try {
        decoder.decode(getExceptionByteArray());
        fail("should throw runtime exception");
    } catch (RuntimeException e) {
    }
}
Also used : UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) CharBuffer(java.nio.CharBuffer) MalformedInputException(java.nio.charset.MalformedInputException) ByteBuffer(java.nio.ByteBuffer)

Example 45 with CharBuffer

use of java.nio.CharBuffer in project robovm by robovm.

the class CharsetDecoderTest method testFlush.

/*
     * test flush
     */
public void testFlush() throws CharacterCodingException {
    CharBuffer out = CharBuffer.allocate(10);
    ByteBuffer in = ByteBuffer.wrap(new byte[] { 12, 12 });
    decoder.decode(in, out, true);
    assertSame(CoderResult.UNDERFLOW, decoder.flush(out));
    decoder.reset();
    decoder.decode((ByteBuffer) in.rewind(), (CharBuffer) out.rewind(), true);
    assertSame(CoderResult.UNDERFLOW, decoder.flush(CharBuffer.allocate(10)));
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

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