List of usage examples for io.netty.buffer ByteBuf readableBytes
public abstract int readableBytes();
From source file:com.github.pgasync.impl.netty.ByteBufMessageDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() == 0) { return;// w ww . ja va2 s . com } byte id = in.readByte(); int length = in.readInt(); Decoder<?> decoder = DECODERS.get(id); try { if (decoder != null) { ByteBuffer buffer = in.nioBuffer(); out.add(decoder.read(buffer)); in.skipBytes(buffer.position()); } else { in.skipBytes(length - 4); } } catch (Throwable t) { // broad catch as otherwise the exception is silently dropped ctx.fireExceptionCaught(t); } }
From source file:com.github.pgasync.impl.netty.NettyPgProtocolStream.java
License:Apache License
ChannelHandler newSslInitiator() { return new ByteToMessageDecoder() { @Override/*from w w w . j av a 2 s .co m*/ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 1) { return; } if ('S' != in.readByte()) { ctx.fireExceptionCaught( new IllegalStateException("SSL required but not supported by backend server")); return; } ctx.pipeline().remove(this); ctx.pipeline().addFirst(SslContextBuilder.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE).build().newHandler(ctx.alloc())); } }; }
From source file:com.github.spapageo.jannel.channel.ChannelBufferUtils.java
License:Open Source License
/** * Reads a new octet string from the byte buffer using the UTF-8 encoding * @param byteBuffer the bytes to read the string from * @param charset the charset to use in order to decode the string * @return the created string//from w w w. j av a 2 s. co m */ @Nonnull public static String readOctetStringToString(ByteBuf byteBuffer, Charset charset) { if (byteBuffer.readableBytes() < 4) throw new NotEnoughDataDecoderException("Not enough bytes to read the octet string size"); int stringSize = byteBuffer.readInt(); checkOctetStringSize(byteBuffer, stringSize); if (stringSize == -1) return EMPTY_STRING; return byteBuffer.readSlice(stringSize).toString(charset); }
From source file:com.github.spapageo.jannel.channel.ChannelBufferUtils.java
License:Open Source License
/** * Read a 4 byte integer from the byte buffer * @param byteBuffer the bytes to read from * @return the integer/*from w w w . jav a 2 s .com*/ */ public static int readInt(ByteBuf byteBuffer) { if (byteBuffer.readableBytes() < 4) throw new NotEnoughDataDecoderException("Not enough bytes to read the integer"); return byteBuffer.readInt(); }
From source file:com.github.spapageo.jannel.channel.ChannelBufferUtils.java
License:Open Source License
/** * Reads a data segment from the byte buffer * @param byteBuffer the bytes to read from * @return the data segment/*from w ww .java 2s . c om*/ */ @Nonnull public static ByteBuf readOctetStringToBytes(ByteBuf byteBuffer) { if (byteBuffer.readableBytes() < 4) throw new NotEnoughDataDecoderException("Not enough bytes to read the octet string size"); int dataSize = byteBuffer.readInt(); if (dataSize == -1) return Unpooled.EMPTY_BUFFER; checkOctetStringSize(byteBuffer, dataSize); return byteBuffer.readBytes(dataSize); }
From source file:com.github.spapageo.jannel.channel.ChannelBufferUtils.java
License:Open Source License
/** * Writes a byte array to output buffer/*from w w w. j av a 2 s. co m*/ * @param input the input byte array * @param output the output buffer */ public static void writeBytesToOctetString(@Nullable ByteBuf input, ByteBuf output) { if (input == null) { output.writeInt(-1); return; } output.writeInt(input.readableBytes()); output.writeBytes(input); }
From source file:com.github.spapageo.jannel.channel.ChannelBufferUtils.java
License:Open Source License
private static void checkOctetStringSize(ByteBuf byteBuffer, int stringSize) { if (stringSize < -1 || stringSize > byteBuffer.readableBytes()) throw new StringSizeException("The octet string size read was " + stringSize + " and the readable bytes were " + byteBuffer.readableBytes()); }
From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java
License:Open Source License
@Test public void testReadOctetStringToBytesWhenOctetStringSizeIsMinus1ReturnsEmptyString() throws Exception { byte[] input = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff }; ByteBuf inputBuffer = Unpooled.copiedBuffer(input); ByteBuf readBytes = ChannelBufferUtils.readOctetStringToBytes(inputBuffer); assertEquals("Decoded byte array size is not 0", 0, readBytes.readableBytes()); inputBuffer.release();// www. j a va2s. c o m }
From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java
License:Open Source License
@Test public void testReadOctetStringToBytesWhenOctetStringSizeIsZeroReturnsEmptyArray() throws Exception { byte[] input = { 0x00, 0x00, 0x00, 0x00 }; ByteBuf inputBuffer = Unpooled.copiedBuffer(input); ByteBuf readBytes = ChannelBufferUtils.readOctetStringToBytes(inputBuffer); assertEquals("Read byte array size is not 0", 0, readBytes.readableBytes()); inputBuffer.release();/*from ww w . j a va2 s . com*/ }
From source file:com.github.sparkfy.network.client.StreamInterceptor.java
License:Apache License
@Override public boolean handle(ByteBuf buf) throws Exception { int toRead = (int) Math.min(buf.readableBytes(), byteCount - bytesRead); ByteBuffer nioBuffer = buf.readSlice(toRead).nioBuffer(); int available = nioBuffer.remaining(); callback.onData(streamId, nioBuffer); bytesRead += available;//from w ww . ja va 2 s . c o m if (bytesRead > byteCount) { RuntimeException re = new IllegalStateException( String.format("Read too many bytes? Expected %d, but read %d.", byteCount, bytesRead)); callback.onFailure(streamId, re); handler.deactivateStream(); throw re; } else if (bytesRead == byteCount) { handler.deactivateStream(); callback.onComplete(streamId); } return bytesRead != byteCount; }