Search in sources :

Example 46 with CharBuffer

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

the class CharsetEncoderTest method testEncodeTrueIllegalState.

// test illegal states for two encode method with endOfInput is true
public void testEncodeTrueIllegalState() throws CharacterCodingException {
    CharBuffer in = CharBuffer.wrap("aaa");
    ByteBuffer out = ByteBuffer.allocate(5);
    // Normal case: just created
    encoder.encode(in, out, true);
    in.rewind();
    out.rewind();
    in.rewind();
    out.rewind();
    // Normal case: just after encode with that endOfInput is true
    assertSame(encoder, encoder.reset());
    encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState2"), ByteBuffer.allocate(30), true);
    encoder.encode(in, out, true);
    in.rewind();
    out.rewind();
    // Normal case:just after encode with that endOfInput is false
    assertSame(encoder, encoder.reset());
    encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState3"), ByteBuffer.allocate(30), false);
    encoder.encode(in, out, true);
    in.rewind();
    out.rewind();
    // Illegal state: just after flush
    assertSame(encoder, encoder.reset());
    encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState4"), ByteBuffer.allocate(30), true);
    encoder.flush(ByteBuffer.allocate(10));
    try {
        encoder.encode(in, out, true);
        fail("should illegal state");
    } catch (IllegalStateException e) {
    }
    // Normal case: after canEncode
    assertSame(encoder, encoder.reset());
    encoder.canEncode("񑠀");
    encoder.encode(in, out, true);
    in.rewind();
    out.rewind();
    assertSame(encoder, encoder.reset());
    encoder.canEncode('�');
    encoder.encode(in, out, true);
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 47 with CharBuffer

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

the class CharsetEncoderTest method testEncodeFacadeIllegalState.

// test illegal states for encode facade
public void testEncodeFacadeIllegalState() throws CharacterCodingException {
    // encode facade can be execute in anywhere
    CharBuffer in = CharBuffer.wrap("aaa");
    // Normal case: just created
    encoder.encode(in);
    in.rewind();
    // Normal case: just after encode facade
    encoder.encode(in);
    in.rewind();
    // Normal case: just after canEncode
    assertSame(encoder, encoder.reset());
    encoder.canEncode("񐠀");
    encoder.encode(in);
    in.rewind();
    assertSame(encoder, encoder.reset());
    encoder.canEncode('�');
    encoder.encode(in);
    in.rewind();
    // Normal case: just after encode with that endOfInput is true
    assertSame(encoder, encoder.reset());
    encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState2"), ByteBuffer.allocate(30), true);
    encoder.encode(in);
    in.rewind();
    // Normal case:just after encode with that endOfInput is false
    assertSame(encoder, encoder.reset());
    encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState3"), ByteBuffer.allocate(30), false);
    encoder.encode(in);
    in.rewind();
    // Normal case: just after flush
    assertSame(encoder, encoder.reset());
    encoder.encode(CharBuffer.wrap("testCanEncodeIllegalState4"), ByteBuffer.allocate(30), true);
    encoder.flush(ByteBuffer.allocate(10));
    encoder.encode(in);
    in.rewind();
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 48 with CharBuffer

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

the class CharsetEncoderTest method implTestEncodeCharBufferByteBufferbooleanException.

protected void implTestEncodeCharBufferByteBufferbooleanException(boolean endOfInput) throws CharacterCodingException {
    ByteBuffer out = ByteBuffer.allocate(100);
    // MalformedException:
    CharBuffer in = getMalformedCharBuffer();
    encoder.onMalformedInput(CodingErrorAction.REPORT);
    encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    if (in != null) {
        encoder.reset();
        CoderResult r = encoder.encode(in, out, endOfInput);
        assertTrue(r.isMalformed());
        encoder.reset();
        out.clear();
        in.rewind();
        encoder.onMalformedInput(CodingErrorAction.IGNORE);
        assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, endOfInput));
        assertCodingErrorAction(endOfInput, out, in, unibytes);
        encoder.reset();
        out.clear();
        in.rewind();
        encoder.onMalformedInput(CodingErrorAction.REPLACE);
        assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, endOfInput));
        assertCodingErrorAction(endOfInput, out, in, unibytesWithRep);
    } else {
        System.out.println("Cannot find malformed char buffer for " + cs.name());
    }
    // Unmapped Exception:
    in = getUnmapCharBuffer();
    encoder.onMalformedInput(CodingErrorAction.REPORT);
    encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    if (in != null) {
        encoder.reset();
        out.clear();
        assertTrue(encoder.encode(in, out, endOfInput).isUnmappable());
        encoder.reset();
        out.clear();
        in.rewind();
        encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
        assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, endOfInput));
        assertCodingErrorAction(endOfInput, out, in, unibytes);
        encoder.reset();
        out.clear();
        in.rewind();
        encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
        assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, endOfInput));
        assertCodingErrorAction(endOfInput, out, in, unibytesWithRep);
    } else {
        System.out.println("Cannot find unmapped char buffer for " + cs.name());
    }
    // RuntimeException
    try {
        encoder.encode(getExceptionCharBuffer());
        fail("should throw runtime exception");
    } catch (RuntimeException e) {
    }
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 49 with CharBuffer

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

the class CharsetTest method assertDecodes.

private void assertDecodes(Charset cs, String s, int... byteInts) throws Exception {
    ByteBuffer in = ByteBuffer.wrap(toByteArray(byteInts));
    CharBuffer out = cs.decode(in);
    assertEquals(s, out.toString());
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 50 with CharBuffer

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

the class Main method storeValues.

static void storeValues(ByteBuffer directBuf) {
    directBuf.position(0);
    ShortBuffer shortBuf = directBuf.asShortBuffer();
    CharBuffer charBuf = directBuf.asCharBuffer();
    IntBuffer intBuf = directBuf.asIntBuffer();
    FloatBuffer floatBuf = directBuf.asFloatBuffer();
    LongBuffer longBuf = directBuf.asLongBuffer();
    DoubleBuffer doubleBuf = directBuf.asDoubleBuffer();
    final byte byteValue = -5;
    final short shortValue = -1234;
    final char charValue = 49200;
    final int intValue = 0x12345678;
    final float floatValue = 3.14159f;
    final long longValue = 0x1122334455667788L;
    final double doubleValue = Double.MIN_VALUE;
    if (directBuf.put(1, byteValue).get(1) != byteValue) {
        throw new RuntimeException("byte get/store failed");
    }
    if (shortBuf.put(1, shortValue).get(1) != shortValue) {
        throw new RuntimeException("short get/store failed");
    }
    if (charBuf.put(2, charValue).get(2) != charValue) {
        throw new RuntimeException("char get/store failed");
    }
    if (intBuf.put(2, intValue).get(2) != intValue) {
        throw new RuntimeException("int get/store failed");
    }
    if (floatBuf.put(3, floatValue).get(3) != floatValue) {
        throw new RuntimeException("float get/store failed");
    }
    if (longBuf.put(2, longValue).get(2) != longValue) {
        throw new RuntimeException("long get/store failed");
    }
    if (doubleBuf.put(3, doubleValue).get(3) != doubleValue) {
        throw new RuntimeException("double get/store failed");
    }
    directBuf.position(0);
    char[] outBuf = new char[directBuf.limit() * 2];
    for (int i = 0; i < directBuf.limit(); i++) {
        byte b = directBuf.get();
        outBuf[i * 2] = hexChar((byte) ((b >> 4) & 0x0f));
        outBuf[i * 2 + 1] = hexChar((byte) (b & 0x0f));
    }
    System.out.println(new String(outBuf));
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) LongBuffer(java.nio.LongBuffer) CharBuffer(java.nio.CharBuffer) FloatBuffer(java.nio.FloatBuffer) IntBuffer(java.nio.IntBuffer) ShortBuffer(java.nio.ShortBuffer)

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