List of usage examples for io.netty.buffer ByteBuf readBytes
public abstract ByteBuf readBytes(ByteBuffer dst);
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 ww w. ja v a 2 s. c om /* 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.digitalpetri.opcua.stack.core.serialization.OpcUaDataTypeEncoding.java
License:Apache License
@Override public ByteString encodeToByteString(Object object, NodeId encodingTypeId) { EncoderDelegate<Object> delegate = DelegateRegistry.getEncoder(encodingTypeId); ByteBuf buffer = allocator.buffer().order(ByteOrder.LITTLE_ENDIAN); BinaryEncoder encoder = new BinaryEncoder(); encoder.setBuffer(buffer);//ww w . j ava 2 s .co m delegate.encode(object, encoder); byte[] bs = new byte[buffer.readableBytes()]; buffer.readBytes(bs); buffer.release(); return ByteString.of(bs); }
From source file:com.dingwang.netty.decoder.PersonDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in == null || in.readableBytes() <= 0) { System.out.println("##################"); return;/* www .java 2s .co m*/ } byte[] src = new byte[in.readableBytes()]; in.readBytes(src); String message = new String(src, "UTF-8"); Person p = new Gson().fromJson(message, Person.class); System.out.println("message======" + message); out.add(p); }
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.j a v a 2s . c om*/ } 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); }
From source file:com.dinstone.jrpc.transport.netty4.TransportProtocolDecoder.java
License:Apache License
private byte[] readFrame(ByteBuf in) { if (in.readableBytes() > 4) { in.markReaderIndex();/*from w w w . jav a 2s . c o m*/ int len = in.readInt(); if (len > maxObjectSize) { throw new IllegalStateException( "The encoded object is too big: " + len + " (> " + maxObjectSize + ")"); } else if (len < 1) { throw new IllegalStateException("The encoded object is too small: " + len + " (< 1)"); } if (in.readableBytes() < len) { in.resetReaderIndex(); return null; } byte[] rpcBytes = new byte[len]; in.readBytes(rpcBytes); return rpcBytes; } return null; }
From source file:com.dinstone.rpc.netty.RpcProtocolDecoder.java
License:Apache License
private byte[] readFrame(ByteBuf in) { int remaining = in.readableBytes(); if (remaining < 4) { return null; }//from w ww . ja va2s . c o m int objectSize = in.getInt(0); if (objectSize > maxObjectSize) { throw new IllegalArgumentException( "The encoded object is too big: " + objectSize + " (> " + maxObjectSize + ')'); } if (remaining - 4 >= objectSize) { objectSize = in.readInt(); // RPC object size byte[] rpcBytes = new byte[objectSize]; in.readBytes(rpcBytes); return rpcBytes; } return null; }
From source file:com.doctor.netty5.example.factorial_algorithm.BigIntegerDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // ? ?F + + -> 1 + 4 + ?? // ??///from www .j a v a 2s .c o m if (in.readableBytes() < 5) { return; // F + } in.markReaderIndex(); short magicNumber = in.readUnsignedByte();// ?F if (magicNumber != 'F') { in.resetReaderIndex(); throw new RuntimeException("magicNumber must be 'F' "); } // ??? int dataLength = in.readInt(); if (in.readableBytes() < dataLength) { in.resetReaderIndex(); return; } byte[] data = new byte[dataLength]; in.readBytes(data); out.add(new BigInteger(data)); }
From source file:com.dwarf.netty.guide.factorial.BigIntegerDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { // Wait until the length prefix is available. if (in.readableBytes() < 5) { return;/* ww w . j ava2s. c o m*/ } in.markReaderIndex(); // Check the magic number. int magicNumber = in.readUnsignedByte(); if (magicNumber != 'F') { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid magic number: " + magicNumber); } // Wait until the whole data is available. int dataLength = in.readInt(); if (in.readableBytes() < dataLength) { in.resetReaderIndex(); return; } // Convert the received data into a new BigInteger. byte[] decoded = new byte[dataLength]; in.readBytes(decoded); out.add(new BigInteger(decoded)); }
From source file:com.ebay.jetstream.messaging.transport.netty.compression.MessageCompressionHandler.java
License:MIT License
/** * Invoked when {@link Channel#write(Object)} is called. *///from w w w . j av a2 s . c o m public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { try { Attribute<Boolean> attr = ctx.channel().attr(EventProducer.m_eckey); Boolean enableCompression = attr.get(); if ((enableCompression != null) && (enableCompression == true)) { ByteBuf chbuf = (ByteBuf) msg; int msglen = chbuf.readableBytes(); ExtendedChannelPromise epromise = (ExtendedChannelPromise) promise; epromise.setRawBytes(msglen); byte[] compressed = Snappy.rawCompress(chbuf.readBytes(msglen).array(), msglen); epromise.setCompressedBytes(compressed.length + 4); chbuf.release(); // need to release the original buffer - do I need to check if this this a ref counted buffer ByteBuf sendbuf = ctx.alloc().buffer(); sendbuf.writeInt(compressed.length); sendbuf.writeBytes(compressed); ctx.write(sendbuf, promise); m_totalMessagesCompressed.increment(); } else { ctx.write(msg, promise); } } catch (Throwable t) { m_totalMessagesDropped.increment(); LOGGER.debug("Failed to compress message - " + t.getLocalizedMessage()); } }
From source file:com.ebay.jetstream.messaging.transport.netty.compression.MessageDecompressionHandler.java
License:MIT License
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf frame = (ByteBuf) super.decode(ctx, in); if (frame == null) { return null; }//from w ww.j a v a 2 s . c om byte[] uncompressedbuf; if (m_allocBuf) uncompressedbuf = new byte[m_tmpBufSz]; else uncompressedbuf = m_tmpBuf; int framelen = frame.readableBytes(); int len = 0; try { len = Snappy.rawUncompress(frame.readBytes(framelen).array(), 0, framelen, uncompressedbuf, 0); } catch (Throwable t) { LOGGER.debug("Failed to uncompress - " + t.getLocalizedMessage()); frame.release(); return null; } frame.release(); ByteBuf buf = ctx.alloc().directBuffer(len); return buf.writeBytes(uncompressedbuf, 0, len); }