List of usage examples for io.netty.buffer ByteBuf setInt
public abstract ByteBuf setInt(int index, int value);
From source file:com.irh.material.basics.netty.chapter14_1.codec.NettyMessageEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf sendBuf) throws Exception { if (msg == null || msg.getHeader() == null) { throw new Exception("The encode message is null"); }//from w w w . j a v a2 s. c o m sendBuf.writeInt((msg.getHeader().getCrcCode())); sendBuf.writeInt((msg.getHeader().getLength())); sendBuf.writeLong((msg.getHeader().getSessionID())); sendBuf.writeByte((msg.getHeader().getType())); sendBuf.writeByte((msg.getHeader().getPriority())); sendBuf.writeInt((msg.getHeader().getAttachment().size())); String key = null; byte[] keyArray = null; Object value = null; for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) { key = param.getKey(); keyArray = key.getBytes("UTF-8"); sendBuf.writeInt(keyArray.length); sendBuf.writeBytes(keyArray); value = param.getValue(); marshallingEncoder.encode(value, sendBuf); } key = null; keyArray = null; value = null; if (msg.getBody() != null) { marshallingEncoder.encode(msg.getBody(), sendBuf); } else sendBuf.writeInt(0); sendBuf.setInt(4, sendBuf.readableBytes() - 8); }
From source file:com.ldp.nettydemo.netty.codec.NettyMessageEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf sendBuf) throws Exception { if (msg == null || msg.getHeader() == null) throw new Exception("The encode message is null"); sendBuf.writeInt((msg.getHeader().getCrcCode())); sendBuf.writeInt((msg.getHeader().getLength())); sendBuf.writeLong((msg.getHeader().getSessionID())); sendBuf.writeByte((msg.getHeader().getType())); sendBuf.writeByte((msg.getHeader().getPriority())); sendBuf.writeInt((msg.getHeader().getAttachment().size())); String key = null;//from w ww . j ava 2 s . c o m byte[] keyArray = null; Object value = null; for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) { key = param.getKey(); keyArray = key.getBytes("UTF-8"); sendBuf.writeInt(keyArray.length); sendBuf.writeBytes(keyArray); value = param.getValue(); sendBuf.writeBytes(ByteObjConverter.ObjectToByte(value)); // marshallingEncoder.encode(value, sendBuf); } key = null; keyArray = null; value = null; if (msg.getBody() != null) { sendBuf.writeBytes(ByteObjConverter.ObjectToByte(msg.getBody())); // marshallingEncoder.encode(msg.getBody(), sendBuf); } else sendBuf.writeInt(0); sendBuf.setInt(4, sendBuf.readableBytes() - 8); }
From source file:com.linecorp.armeria.server.grpc.ArmeriaServerCall.java
License:Apache License
private HttpData serializeTrailersAsMessage(HttpHeaders trailers) { final ByteBuf serialized = ctx.alloc().buffer(); boolean success = false; try {// w w w. j a va 2s . c o m serialized.writeByte(TRAILERS_FRAME_HEADER); // Skip, we'll set this after serializing the headers. serialized.writeInt(0); for (Map.Entry<AsciiString, String> trailer : trailers) { encodeHeader(trailer.getKey(), trailer.getValue(), serialized); } final int messageSize = serialized.readableBytes() - 5; serialized.setInt(1, messageSize); success = true; } finally { if (!success) { serialized.release(); } } return new ByteBufHttpData(serialized, true); }
From source file:com.nanxiaoqiang.test.netty.protocol.demo1.codec.NettyMessageEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf sendBuf) throws Exception { if (msg == null || msg.getHeader() == null) throw new Exception("The encode message is null"); // Header Start sendBuf.writeInt((msg.getHeader().getCrcCode()));// crc sendBuf.writeInt((msg.getHeader().getLength()));// ? sendBuf.writeLong((msg.getHeader().getSessionID())); sendBuf.writeByte((msg.getHeader().getType())); sendBuf.writeByte((msg.getHeader().getPriority())); sendBuf.writeInt((msg.getHeader().getAttachment().size())); String key = null;/* w w w . j av a 2s .c o m*/ byte[] keyArray = null; Object value = null; for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) { key = param.getKey(); keyArray = key.getBytes("UTF-8"); sendBuf.writeInt(keyArray.length); sendBuf.writeBytes(keyArray); value = param.getValue(); marshallingEncoder.encode(value, sendBuf); } key = null; keyArray = null; value = null; // Head End // Body Start if (msg.getBody() != null) {// ? marshallingEncoder.encode(msg.getBody(), sendBuf); } else // ? sendBuf.writeInt(0);// 0int sendBuf.setInt(4, sendBuf.readableBytes() - 8);// 0~3CRC4~7??8? }
From source file:com.necla.simba.server.gateway.server.frontend.FrontendFrameEncoder.java
License:Apache License
private void compress(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) { LOG.debug("compress here"); byte[] inAry = new byte[msg.readableBytes()]; msg.readBytes(inAry);//from ww w . j av a 2s . c o m int sizeEstimate = (int) Math.ceil(inAry.length * 1.001) + 12 + 4; LOG.debug("compress here2"); out.ensureWritable(sizeEstimate); int beginIndex = out.writerIndex(); out.writerIndex(beginIndex + 4); try { deflater.setInput(inAry); while (!deflater.needsInput()) { LOG.debug("compress here3333"); int numBytes = deflater.deflate(encodeBuf, 0, encodeBuf.length); LOG.debug("Compressed numBytes=" + numBytes); out.writeBytes(encodeBuf, 0, numBytes); LOG.debug("compress here4"); } deflater.finish(); while (!deflater.finished()) { int numBytes = deflater.deflate(encodeBuf, 0, encodeBuf.length); out.writeBytes(encodeBuf, 0, numBytes); LOG.debug("compress here5"); } deflater.reset(); int len = out.writerIndex() - beginIndex - 4; Stats.sent(out.writerIndex() + beginIndex + 4); LOG.debug("Compressed len=" + len); len |= (1 << 30); out.setInt(beginIndex, len); } catch (Exception e) { LOG.debug("Exception" + e); } }
From source file:com.phei.netty.protocol.netty.codec.NettyMessageEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf sendBuf) throws Exception { if (msg == null || msg.getHeader() == null) throw new Exception("The encode message is null"); sendBuf.writeInt((msg.getHeader().getCrcCode())); sendBuf.writeInt((msg.getHeader().getLength())); sendBuf.writeLong((msg.getHeader().getSessionID())); sendBuf.writeByte((msg.getHeader().getType())); sendBuf.writeByte((msg.getHeader().getPriority())); sendBuf.writeInt((msg.getHeader().getAttachment().size())); String key = null;// w w w. ja v a 2s . c o m byte[] keyArray = null; Object value = null; for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) { key = param.getKey(); keyArray = key.getBytes("UTF-8"); sendBuf.writeInt(keyArray.length); sendBuf.writeBytes(keyArray); value = param.getValue(); marshallingEncoder.encode(value, sendBuf); } key = null; keyArray = null; value = null; //body?4int if (msg.getBody() != null) { marshallingEncoder.encode(msg.getBody(), sendBuf); } else { sendBuf.writeInt(0); } sendBuf.setInt(4, sendBuf.readableBytes() - 8); }
From source file:com.quavo.util.buf.ByteBufUtils.java
License:Open Source License
/** * Deciphers the specified {@link ByteBuf} with the given key. * //from w w w .j av a 2 s . c om * @param buffer The {@link ByteBuf}. * @param key The key. * @throws IllegalArgumentException if the key is not exactly 4 elements long. */ private static void decipherXTEA(ByteBuf buffer, int start, int end, int[] key) { if (key.length != 4) { throw new IllegalArgumentException(); } int numQuads = (end - start) / 8; for (int i = 0; i < numQuads; i++) { int sum = 0x9E3779B9 * 32; int v0 = buffer.getInt(start + i * 8); int v1 = buffer.getInt(start + i * 8 + 4); for (int j = 0; j < 32; j++) { v1 -= (((v0 << 4) ^ (v0 >>> 5)) + v0) ^ (sum + key[(sum >>> 11) & 3]); sum -= 0x9E3779B9; v0 -= (((v1 << 4) ^ (v1 >>> 5)) + v1) ^ (sum + key[sum & 3]); } buffer.setInt(start + i * 8, v0); buffer.setInt(start + i * 8 + 4, v1); } }
From source file:com.turn.ttorrent.client.io.PeerMessageCodec.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, PeerMessage value, ByteBuf out) throws Exception { // LOG.info("encode: " + value); int lengthIndex = out.writerIndex(); out.writeInt(0);/*ww w. ja v a 2 s. c om*/ int startIndex = out.writerIndex(); value.toWire(out, listener.getExtendedMessageTypes()); out.setInt(lengthIndex, out.writerIndex() - startIndex); }
From source file:com.twitter.http2.HttpFrameDecoderTest.java
License:Apache License
private void setReservedBits(ByteBuf frame) { frame.setInt(5, frame.getInt(5) | 0x80000000); }
From source file:com.yahoo.pulsar.client.impl.ProducerImpl.java
License:Apache License
/** * Strips checksum from {@link OpSendMsg} command if present else ignore it. * //from w w w. j a v a2 s . c o m * @param op */ private void stripChecksum(OpSendMsg op) { op.cmd.markReaderIndex(); int totalMsgBufSize = op.cmd.readableBytes(); DoubleByteBuf msg = getDoubleByteBuf(op.cmd); if (msg != null) { ByteBuf headerFrame = msg.getFirst(); msg.markReaderIndex(); headerFrame.markReaderIndex(); try { headerFrame.skipBytes(4); // skip [total-size] int cmdSize = (int) headerFrame.readUnsignedInt(); // verify if checksum present headerFrame.skipBytes(cmdSize); if (!hasChecksum(headerFrame)) { headerFrame.resetReaderIndex(); return; } int headerSize = 4 + 4 + cmdSize; // [total-size] [cmd-length] [cmd-size] int checksumSize = 4 + 2; // [magic-number] [checksum-size] int checksumMark = (headerSize + checksumSize); // [header-size] [checksum-size] int metaPayloadSize = (totalMsgBufSize - checksumMark); // metadataPayload = totalSize - checksumMark int newTotalFrameSizeLength = 4 + cmdSize + metaPayloadSize; // new total-size without checksum headerFrame.resetReaderIndex(); int headerFrameSize = headerFrame.readableBytes(); headerFrame.setInt(0, newTotalFrameSizeLength); // rewrite new [total-size] ByteBuf metadata = headerFrame.slice(checksumMark, headerFrameSize - checksumMark); // sliced only // metadata headerFrame.writerIndex(headerSize); // set headerFrame write-index to overwrite metadata over checksum metadata.readBytes(headerFrame, metadata.readableBytes()); headerFrame.capacity(headerFrameSize - checksumSize); // reduce capacity by removed checksum bytes headerFrame.resetReaderIndex(); } finally { op.cmd.resetReaderIndex(); } } else { log.warn("[{}] Failed while casting {} into DoubleByteBuf", producerName, op.cmd.getClass().getName()); } }