- Common ways to obtain ByteBuf
private void myMethod () {ByteBuf b =
Unpooled.buffer()
ChannelHandlerContext ctx;ctx.alloc().buffer()
- Smart code suggestions by Codota
}
@Override public int readableBytes() { if (terminated) { return buffer.readableBytes(); } else { return Integer.MAX_VALUE - buffer.readerIndex(); } }
@Override protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { if (sourceInputLog.isTraceEnabled()) { sourceInputLog.trace("Recv network data: {} bytes via input '{}' <{}> from remote address {}", msg.readableBytes(), sourceInputName, sourceInputId, ctx.channel().remoteAddress()); } } }
@Override public boolean decode(ByteBuf in) { int i = in.readableBytes(); if (i < 3) { return false; } i -= 3; this.identified = (in.readByte() == 1); short len = in.readShort(); if (len > 0) { if (i < len) { return false; } byte[] bs = new byte[len]; in.readBytes(bs); this.setVersion(new String(bs, UTF8)); } return true; } }
if (component.readableBytes() > wrapSizeLimit) { throw new RpcException(String.format("Component Chunk size: %d is greater than the wrapSizeLimit: %d", component.readableBytes(), wrapSizeLimit)); component.getBytes(component.readerIndex(), origMsg, 0, component.readableBytes()); if(logger.isTraceEnabled()) { logger.trace("Trying to encrypt chunk of size:{} with wrapSizeLimit:{} and chunkMode: {}", component.readableBytes(), wrapSizeLimit); final byte[] wrappedMsg = saslCodec.wrap(origMsg, 0, component.readableBytes()); if(logger.isTraceEnabled()) { logger.trace("Successfully encrypted message, original size: {} Final Size: {}", component.readableBytes(), wrappedMsg.length); msg.skipBytes(component.readableBytes()); component.skipBytes(component.readableBytes());
LOG.trace("Attempting to decode DNS record [{}]", dnsRecord); try { final ByteBuf byteBuf = dnsRawRecord.content(); ipAddressBytes = new byte[byteBuf.readableBytes()]; int readerIndex = byteBuf.readerIndex(); byteBuf.getBytes(readerIndex, ipAddressBytes); } finally { LOG.trace("The IP address has [{}] bytes", ipAddressBytes.length); LOG.trace("The resulting IP address is [{}]", ipAddress.getHostAddress());
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws IOException { if (in.readableBytes() == 0) { loggerWire.trace("in.readableBytes() == 0"); return; } loggerWire.trace("Decoding frame (" + in.readableBytes() + " bytes)"); List<FrameCodec.Frame> frames = frameCodec.readFrames(in); // Check if a full frame was available. If not, we'll try later when more bytes come in. if (frames == null || frames.isEmpty()) return; for (int i = 0; i < frames.size(); i++) { FrameCodec.Frame frame = frames.get(i); channel.getNodeStatistics().rlpxInMessages.add(); } out.addAll(frames); }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return; } in.markReaderIndex(); int msgSize = in.readInt(); checkSize(msgSize); if (in.readableBytes() < msgSize) { // Incomplete message in buffer. in.resetReaderIndex(); return; } try { ByteBuffer nioBuffer = maybeDecrypt(in.nioBuffer(in.readerIndex(), msgSize)); Input kryoIn = new Input(new ByteBufferInputStream(nioBuffer)); Object msg = kryos.get().readClassAndObject(kryoIn); LOG.trace("Decoded message of type {} ({} bytes)", msg != null ? msg.getClass().getName() : msg, msgSize); out.add(msg); } finally { in.skipBytes(msgSize); } }
@Override public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception if (in.readableBytes() < 8) int compressedFileSize = copy.readInt(); if (size + 3 + breaks > in.readableBytes()) logger.trace("Index {} archive {}: Not enough data yet {} > {}", index, file, size + 3 + breaks, in.readableBytes()); return; int bytesToRead = Math.min(bytesInBlock, size - compressedData.writerIndex()); logger.trace("{}/{}: reading block {}/{}, read so far this block: {}, file status: {}/{}", index, file, (totalRead % CHUNK_SIZE), CHUNK_SIZE, logger.trace("{}/{}: done downloading file, remaining buffer {}", index, file, in.readableBytes());
protected void decode(ChannelHandlerContext ctx, IsoTPMessage in, List<Object> out) { if (logger.isTraceEnabled()) { logger.trace("Got Data: {}", ByteBufUtil.hexDump(in.getUserData())); if (userData.readableBytes() == 0) { return; userData.readShort(); // Reserved (is always constant 0x0000) short tpduReference = userData.readShort(); short headerParametersLength = userData.readShort(); short userDataLength = userData.readShort(); byte errorClass = 0;
@Override public Object decode(ByteBuf buf, State state) throws IOException { int decompressSize = buf.readInt(); ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize); try { LZ4SafeDecompressor decompressor = factory.safeDecompressor(); ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes()); int pos = outBuffer.position(); decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer); int compressedLength = outBuffer.position() - pos; out.writerIndex(compressedLength); return innerCodec.getValueDecoder().decode(out, state); } finally { out.release(); } } };
private void decodeV1(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { long startTime = System.currentTimeMillis(); in.resetReaderIndex(); in.skipBytes(2);// skip magic num byte messageType = (byte) in.readShort(); long requestId = in.readLong(); int dataLength = in.readInt(); // FIXME 如果dataLength过大,可能导致问题 if (in.readableBytes() < dataLength) { in.resetReaderIndex(); return; } checkMaxContext(dataLength, ctx, messageType == MotanConstants.FLAG_REQUEST, requestId); byte[] data = new byte[dataLength]; in.readBytes(data); decode(data, out, messageType == MotanConstants.FLAG_REQUEST, requestId).setStartTime(startTime); }
@Override protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> list) throws Exception { if (in.readableBytes() < RemotingHeader.MIN_HEADER_SIZE + RemotingHeader.LENGTH_FIELD) return; int magicCode = in.getInt(in.readerIndex() + RemotingHeader.LENGTH_FIELD); if (DEFAULT_MAGIC_CODE != magicCode) { throw new IOException("Illegal Data, MagicCode=" + Integer.toHexString(magicCode)); int total = in.readInt(); if (in.readableBytes() < total) { in.resetReaderIndex(); return; short headerSize = in.readShort(); RemotingHeader remotingHeader = decodeHeader(in);
@Override public boolean decode(ByteBuf in) { int i = in.readableBytes(); try { short len = in.readShort(); if (len > 0) { byte[] bs = new byte[len]; this.setVersion(new String(bs, UTF8)); len = in.readShort(); if (len > 0) { byte[] bs = new byte[len]; this.setApplicationId(new String(bs, UTF8)); len = in.readShort(); if (len > 0) { byte[] bs = new byte[len]; LOGGER.debug(in.writerIndex() == in.readerIndex() ? "true" : "false" + this);
if (LOG.isTraceEnabled()) { LOG.trace("0x{} buf {}", Long.toHexString(sessionId), ByteBufUtil.hexDump(buf)); queuedBuffer = channel.alloc().buffer(buf.readableBytes()); if (LOG.isTraceEnabled()) { LOG.trace("0x{} queuedBuffer {}", Long.toHexString(sessionId), ByteBufUtil.hexDump(queuedBuffer)); if (LOG.isTraceEnabled()) { LOG.trace("Before copy {}", buf); queuedBuffer = channel.alloc().buffer(buf.readableBytes());
public void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws IOException { logger.trace("Channel closed before decoding the message of {} bytes", msg.readableBytes()); msg.skipBytes(msg.readableBytes()); return; if(logger.isTraceEnabled()) { logger.trace("Trying to decrypt the encrypted message of size: {} with maxWrappedSize", msg.readableBytes()); msg.getBytes(msg.readerIndex(), lengthOctets.array(), 0, RpcConstants.LENGTH_FIELD_LENGTH); final int wrappedMsgLength = lengthOctets.getInt(0); msg.skipBytes(RpcConstants.LENGTH_FIELD_LENGTH); assert(msg.readableBytes() == wrappedMsgLength); msg.getBytes(msg.readerIndex(), wrappedMsg, 0, wrappedMsgLength); if(logger.isTraceEnabled()) { logger.trace("Successfully decrypted incoming message. Length after decryption: {}", decodedMsg.length);
cbuf.addComponent(previousBuf.copy()); cbuf.addComponent(in); cbuf.writerIndex(previousBuf.readableBytes() + in.readableBytes()); data = cbuf; if (data.readableBytes() == 1 && isSingleByteFrame(data, data.readerIndex())) { out.add(createSingleByteFrame(data)); } else { boolean foundFrame = false; for (int searchStartIx = data.readerIndex(); searchStartIx < data.readerIndex() + data.readableBytes(); searchStartIx++) { if (data.getByte(searchStartIx) == DataFrame.START_OF_FRAME) { int frameEndIx = scanForFrame(data, searchStartIx); in.readBytes(in.readableBytes()); logger.trace("Done processing received data: {}", out);
@Override public HttpResponse getResponse() { LOG.trace("CommandName: " + COMMAND_NAME + ": getHttpResponse.."); ByteBuf data = Unpooled.copiedBuffer(responseBody); LOG.warn("Response data: {}", Arrays.toString(data.array())); FullHttpResponse httpResponse = new DefaultFullHttpResponse(HTTP_1_1, OK, data); httpResponse.headers().set(CONTENT_TYPE, CommonEpConstans.RESPONSE_CONTENT_TYPE); httpResponse.headers().set(CONTENT_LENGTH, data.readableBytes()); LOG.warn("Response size: {}", data.readableBytes()); httpResponse .headers() .set(CommonEpConstans.RESPONSE_TYPE, CommonEpConstans.RESPONSE_TYPE_OPERATION); if (responseSignature != null) { httpResponse .headers() .set(CommonEpConstans.SIGNATURE_HEADER_NAME, encodeBase64String(responseSignature)); } if (isNeedConnectionClose()) { httpResponse.headers().set(CONNECTION, HttpHeaders.Values.CLOSE); } else { if (HttpHeaders.isKeepAlive(getRequest())) { httpResponse.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } else { httpResponse.headers().set(CONNECTION, HttpHeaders.Values.CLOSE); } } return httpResponse; }
@Override public long skip(long bytes) throws IOException { int readable = buffer.readableBytes(); if (readable < bytes) { bytes = readable; } buffer.readerIndex((int) (buffer.readerIndex() + bytes)); return bytes; }
@Override public Object decode(ByteBuf buf, State state) throws IOException { int decompressSize = buf.readInt(); ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize); try { LZ4SafeDecompressor decompressor = factory.safeDecompressor(); ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes()); int pos = outBuffer.position(); decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer); int compressedLength = outBuffer.position() - pos; out.writerIndex(compressedLength); return innerCodec.getValueDecoder().decode(out, state); } finally { out.release(); } } };
@Override public boolean decode(ByteBuf in) { int leftLen = in.readableBytes(); int read = 0; int xidLen = in.readShort(); if (xidLen > 0) { if (leftLen < xidLen) { leftLen --; int resourceIdLen = in.readShort(); if (resourceIdLen > 0) { if (leftLen < resourceIdLen) { int applicationDataLen = in.readInt(); if (applicationDataLen > 0) { if (leftLen < applicationDataLen) {