Search in sources :

Example 11 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project netty by netty.

the class ByteBufUtil method decodeString.

private static void decodeString(CharsetDecoder decoder, ByteBuffer src, CharBuffer dst) {
    try {
        CoderResult cr = decoder.decode(src, dst, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        cr = decoder.flush(dst);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
    } catch (CharacterCodingException x) {
        throw new IllegalStateException(x);
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) CoderResult(java.nio.charset.CoderResult)

Example 12 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project voltdb by VoltDB.

the class ByteBufUtil method encodeString0.

static ByteBuf encodeString0(ByteBufAllocator alloc, boolean enforceHeap, CharBuffer src, Charset charset) {
    final CharsetEncoder encoder = CharsetUtil.encoder(charset);
    int length = (int) ((double) src.remaining() * encoder.maxBytesPerChar());
    boolean release = true;
    final ByteBuf dst;
    if (enforceHeap) {
        dst = alloc.heapBuffer(length);
    } else {
        dst = alloc.buffer(length);
    }
    try {
        final ByteBuffer dstBuf = dst.internalNioBuffer(0, length);
        final int pos = dstBuf.position();
        CoderResult cr = encoder.encode(src, dstBuf, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        cr = encoder.flush(dstBuf);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        dst.writerIndex(dst.writerIndex() + dstBuf.position() - pos);
        release = false;
        return dst;
    } catch (CharacterCodingException x) {
        throw new IllegalStateException(x);
    } finally {
        if (release) {
            dst.release();
        }
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 13 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project async-http-client by AsyncHttpClient.

the class Utf8ByteBufCharsetDecoder method handleSplitCharBuffer.

private void handleSplitCharBuffer(ByteBuffer nioBuffer, boolean endOfInput) throws CharacterCodingException {
    // TODO we could save charSize
    int missingBytes = charSize(splitCharBuffer.get(0)) - splitCharBuffer.position();
    if (nioBuffer.remaining() < missingBytes) {
        if (endOfInput) {
            throw new CharacterCodingException();
        }
        // still not enough bytes
        splitCharBuffer.put(nioBuffer);
    } else {
        // FIXME better way?
        for (int i = 0; i < missingBytes; i++) {
            splitCharBuffer.put(nioBuffer.get());
        }
        splitCharBuffer.flip();
        CoderResult res = decoder.decode(splitCharBuffer, charBuffer, endOfInput && !nioBuffer.hasRemaining());
        if (res.isError()) {
            res.throwException();
        }
        splitCharBuffer.clear();
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) CoderResult(java.nio.charset.CoderResult)

Example 14 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project jeromq by zeromq.

the class TestZMQ method testByteBufferRecv.

@Test
public void testByteBufferRecv() throws InterruptedException, IOException, CharacterCodingException {
    int port = Utils.findOpenPort();
    ZMQ.Context context = ZMQ.context(1);
    ByteBuffer bb = ByteBuffer.allocate(6).order(ByteOrder.nativeOrder());
    ZMQ.Socket push = null;
    ZMQ.Socket pull = null;
    try {
        push = context.socket(ZMQ.PUSH);
        pull = context.socket(ZMQ.PULL);
        pull.bind("tcp://*:" + port);
        push.connect("tcp://localhost:" + port);
        push.send("PING".getBytes(ZMQ.CHARSET), 0);
        pull.recvByteBuffer(bb, 0);
        bb.flip();
        byte[] b = new byte[bb.remaining()];
        bb.duplicate().get(b);
        assertEquals("PING", new String(b, ZMQ.CHARSET));
    } finally {
        try {
            push.close();
        } catch (Exception ignore) {
            ignore.printStackTrace();
        }
        try {
            pull.close();
        } catch (Exception ignore) {
            ignore.printStackTrace();
        }
        try {
            context.term();
        } catch (Exception ignore) {
            ignore.printStackTrace();
        }
    }
}
Also used : Socket(org.zeromq.ZMQ.Socket) Context(org.zeromq.ZMQ.Context) ByteBuffer(java.nio.ByteBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) IOException(java.io.IOException) Test(org.junit.Test)

Example 15 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project undertow by undertow-io.

the class BlockingWriterSenderImpl method writeBuffer.

private boolean writeBuffer(final ByteBuffer buffer, final IoCallback callback) {
    StringBuilder builder = new StringBuilder();
    try {
        builder.append(charsetDecoder.decode(buffer));
    } catch (CharacterCodingException e) {
        callback.onException(exchange, this, e);
        return false;
    }
    String data = builder.toString();
    writer.write(data);
    if (writer.checkError()) {
        callback.onException(exchange, this, new IOException());
        return false;
    }
    return true;
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) IOException(java.io.IOException)

Aggregations

CharacterCodingException (java.nio.charset.CharacterCodingException)196 ByteBuffer (java.nio.ByteBuffer)114 CharBuffer (java.nio.CharBuffer)48 CharsetDecoder (java.nio.charset.CharsetDecoder)44 IOException (java.io.IOException)34 CharsetEncoder (java.nio.charset.CharsetEncoder)31 CoderResult (java.nio.charset.CoderResult)30 Charset (java.nio.charset.Charset)27 InputStream (java.io.InputStream)9 Date (java.util.Date)9 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)8 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)6 Path (java.nio.file.Path)6 ParseException (java.text.ParseException)6 Test (org.junit.Test)6 CoreException (org.eclipse.core.runtime.CoreException)5 HumanReadableException (com.facebook.buck.util.HumanReadableException)4 OutputStream (java.io.OutputStream)4