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);
}
}
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();
}
}
}
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();
}
}
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();
}
}
}
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;
}
Aggregations