List of usage examples for io.netty.buffer ByteBuf readInt
public abstract int readInt();
From source file:com.datastax.driver.core.LZ4Compressor.java
License:Apache License
private ByteBuf decompressDirect(ByteBuf input) throws IOException { // If the input is direct we will allocate a direct output buffer as well as this will allow us to use // LZ4Compressor.decompress and so eliminate memory copies. int readable = input.readableBytes(); int uncompressedLength = input.readInt(); ByteBuffer in = inputNioBuffer(input); // Increase reader index. input.readerIndex(input.writerIndex()); ByteBuf output = input.alloc().directBuffer(uncompressedLength); try {/* ww w.j av a2 s . com*/ ByteBuffer out = outputNioBuffer(output); int read = decompressor.decompress(in, in.position(), out, out.position(), out.remaining()); if (read != readable - INTEGER_BYTES) throw new IOException("Compressed lengths mismatch"); // Set the writer index so the amount of written bytes is reflected output.writerIndex(output.writerIndex() + uncompressedLength); } catch (Exception e) { // release output buffer so we not leak and rethrow exception. output.release(); throw new IOException(e); } return output; }
From source file:com.datastax.driver.core.LZ4Compressor.java
License:Apache License
private ByteBuf decompressHeap(ByteBuf input) throws IOException { // Not a direct buffer so use byte arrays... byte[] in = input.array(); int len = input.readableBytes(); int uncompressedLength = input.readInt(); int inOffset = input.arrayOffset() + input.readerIndex(); // Increase reader index. input.readerIndex(input.writerIndex()); // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and so // can eliminate the overhead of allocate a new byte[]. ByteBuf output = input.alloc().heapBuffer(uncompressedLength); try {/* www . j a v a 2s . co m*/ int offset = output.arrayOffset() + output.writerIndex(); byte out[] = output.array(); int read = decompressor.decompress(in, inOffset, out, offset, uncompressedLength); if (read != len - INTEGER_BYTES) throw new IOException("Compressed lengths mismatch"); // Set the writer index so the amount of written bytes is reflected output.writerIndex(output.writerIndex() + uncompressedLength); } catch (Exception e) { // release output buffer so we not leak and rethrow exception. output.release(); throw new IOException(e); } return output; }
From source file:com.Da_Technomancer.crossroads.API.packets.Message.java
License:Creative Commons License
private static int readInt(ByteBuf buf) { return buf.readInt(); }
From source file:com.Da_Technomancer.crossroads.API.packets.Message.java
License:Creative Commons License
private static byte[][] readByte2DArray(ByteBuf buf) { int outerSize = buf.readInt(); int innerSize = buf.readInt(); byte[][] out = new byte[outerSize][innerSize]; for (int i = 0; i < outerSize; i++) { for (int j = 0; j < innerSize; j++) { out[i][j] = buf.readByte();/*from ww w. ja v a 2 s .c o m*/ } } return out; }
From source file:com.Da_Technomancer.crossroads.API.packets.Message.java
License:Creative Commons License
private static int[] readIntArray(ByteBuf buf) { int size = buf.readInt(); int[] out = new int[size]; for (int i = 0; i < size; i++) { out[i] = buf.readInt();/*from w w w . j a va2 s. co m*/ } return out; }
From source file:com.Da_Technomancer.crossroads.API.packets.Message.java
License:Creative Commons License
private static double[] readDoubleArray(ByteBuf buf) { int size = buf.readInt(); double[] out = new double[size]; for (int i = 0; i < size; i++) { out[i] = buf.readDouble();// w w w .j av a 2 s. c o m } return out; }
From source file:com.dianping.cat.message.spi.codec.NativeMessageCodec.java
License:Open Source License
@Override public MessageTree decode(ByteBuf buf) { buf.readInt(); // read the length of the message tree DefaultMessageTree tree = new DefaultMessageTree(); Context ctx = new Context(tree); Codec.HEADER.decode(ctx, buf); Message msg = decodeMessage(ctx, buf); tree.setMessage(msg);/* ww w .ja va 2s . co m*/ tree.setBuffer(buf); return tree; }
From source file:com.digitalpetri.opcua.stack.core.channel.headers.AsymmetricSecurityHeader.java
License:Apache License
public static AsymmetricSecurityHeader decode(ByteBuf buffer) { /* SecurityPolicyUri */ int securityPolicyUriLength = buffer.readInt(); String securityPolicyUri = new String(buffer.readBytes(securityPolicyUriLength).array(), Charset.forName("UTF-8")); /* SenderCertificate */ int senderCertificateLength = buffer.readInt(); byte[] senderCertificate = senderCertificateLength >= 0 ? buffer.readBytes(senderCertificateLength).array() : null;//from w ww. ja va2 s . c o m /* ReceiverCertificateThumbprint */ int thumbprintLength = buffer.readInt(); byte[] receiverCertificateThumbprint = thumbprintLength >= 0 ? buffer.readBytes(thumbprintLength).array() : null; return new AsymmetricSecurityHeader(securityPolicyUri, new ByteString(senderCertificate), new ByteString(receiverCertificateThumbprint)); }
From source file:com.dingwang.netty.decoder.TimeDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { System.out.println("==========decode"); if (in.readableBytes() < 4) { return;//from w ww . j av a 2 s.co m } out.add(new UnixTime(in.readInt())); }
From source file:com.dingwang.rpc.decode.RpcDecoder.java
License:Open Source License
@Override public final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return;// w w w.jav a2 s. c o m } in.markReaderIndex(); int dataLength = in.readInt(); if (dataLength < 0) { ctx.close(); } if (in.readableBytes() < dataLength) { in.resetReaderIndex(); } byte[] data = new byte[dataLength]; in.readBytes(data); Object obj = SerializationUtil.deserialize(data, genericClass); out.add(obj); }