List of usage examples for io.netty.buffer CompositeByteBuf writerIndex
@Override public CompositeByteBuf writerIndex(int writerIndex)
From source file:cn.wantedonline.puppy.httpserver.component.HttpObjectAggregator.java
License:Apache License
@Override protected void decode(final ChannelHandlerContext ctx, HttpObject msg, List<Object> out) throws Exception { AggregatedFullHttpMessage currentMessage = this.currentMessage; if (msg instanceof HttpMessage) { tooLongFrameFound = false;//from ww w .j a v a2 s. co m assert currentMessage == null; HttpMessage m = (HttpMessage) msg; // Handle the 'Expect: 100-continue' header if necessary. if (is100ContinueExpected(m)) { if (HttpHeaders.getContentLength(m, 0) > maxContentLength) { tooLongFrameFound = true; final ChannelFuture future = ctx.writeAndFlush(EXPECTATION_FAILED.duplicate().retain()); future.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { ctx.fireExceptionCaught(future.cause()); } } }); if (closeOnExpectationFailed) { future.addListener(ChannelFutureListener.CLOSE); } ctx.pipeline().fireUserEventTriggered(HttpExpectationFailedEvent.INSTANCE); return; } ctx.writeAndFlush(CONTINUE.duplicate().retain()).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { ctx.fireExceptionCaught(future.cause()); } } }); } if (!m.getDecoderResult().isSuccess()) { removeTransferEncodingChunked(m); out.add(toFullMessage(m)); this.currentMessage = null; return; } if (msg instanceof HttpRequest) { HttpRequest header = (HttpRequest) msg; this.currentMessage = currentMessage = new AggregatedFullHttpRequest(header, ctx.alloc().compositeBuffer(maxCumulationBufferComponents), null); } else if (msg instanceof HttpResponse) { HttpResponse header = (HttpResponse) msg; this.currentMessage = currentMessage = new AggregatedFullHttpResponse(header, Unpooled.compositeBuffer(maxCumulationBufferComponents), null); } else { throw new Error(); } // A streamed message - initialize the cumulative buffer, and wait for incoming chunks. removeTransferEncodingChunked(currentMessage); } else if (msg instanceof HttpContent) { if (tooLongFrameFound) { if (msg instanceof LastHttpContent) { this.currentMessage = null; } // already detect the too long frame so just discard the content return; } assert currentMessage != null; // Merge the received chunk into the content of the current message. HttpContent chunk = (HttpContent) msg; CompositeByteBuf content = (CompositeByteBuf) currentMessage.content(); if (content.readableBytes() > maxContentLength - chunk.content().readableBytes()) { tooLongFrameFound = true; // release current message to prevent leaks currentMessage.release(); this.currentMessage = null; throw new TooLongFrameException("HTTP content length exceeded " + maxContentLength + " bytes."); } // Append the content of the chunk if (chunk.content().isReadable()) { chunk.retain(); content.addComponent(chunk.content()); content.writerIndex(content.writerIndex() + chunk.content().readableBytes()); } final boolean last; if (!chunk.getDecoderResult().isSuccess()) { currentMessage.setDecoderResult(DecoderResult.failure(chunk.getDecoderResult().cause())); last = true; } else { last = chunk instanceof LastHttpContent; } if (last) { this.currentMessage = null; // Merge trailing headers into the message. if (chunk instanceof LastHttpContent) { LastHttpContent trailer = (LastHttpContent) chunk; currentMessage.setTrailingHeaders(trailer.trailingHeaders()); } else { currentMessage.setTrailingHeaders(new DefaultHttpHeaders()); } // Set the 'Content-Length' header. If one isn't already set. // This is important as HEAD responses will use a 'Content-Length' header which // does not match the actual body, but the number of bytes that would be // transmitted if a GET would have been used. // // See rfc2616 14.13 Content-Length if (!isContentLengthSet(currentMessage)) { currentMessage.headers().set(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(content.readableBytes())); } // All done out.add(currentMessage); } } else { throw new Error(); } }
From source file:com.couchbase.client.core.message.kv.subdoc.multi.SubMultiLookupRequest.java
License:Apache License
private static ByteBuf encode(List<LookupCommand> commands) { CompositeByteBuf compositeBuf = Unpooled.compositeBuffer(commands.size()); //FIXME pooled allocator? for (LookupCommand command : commands) { byte[] pathBytes = command.path().getBytes(CharsetUtil.UTF_8); short pathLength = (short) pathBytes.length; ByteBuf commandBuf = Unpooled.buffer(4 + pathLength); //FIXME a way of using the pooled allocator? commandBuf.writeByte(command.opCode()); commandBuf.writeByte(0); //no flags supported for lookup commandBuf.writeShort(pathLength); //no value length commandBuf.writeBytes(pathBytes); compositeBuf.addComponent(commandBuf); compositeBuf.writerIndex(compositeBuf.writerIndex() + commandBuf.readableBytes()); }/* w w w . ja va2 s . com*/ return compositeBuf; }
From source file:com.couchbase.client.core.message.kv.subdoc.multi.SubMultiMutationRequest.java
License:Apache License
private static ByteBuf encode(List<MutationCommand> commands) { //FIXME a way of using the pooled allocator? CompositeByteBuf compositeBuf = Unpooled.compositeBuffer(commands.size()); for (MutationCommand command : commands) { byte[] pathBytes = command.path().getBytes(CharsetUtil.UTF_8); short pathLength = (short) pathBytes.length; ByteBuf commandBuf = Unpooled.buffer(4 + pathLength + command.fragment().readableBytes()); commandBuf.writeByte(command.opCode()); if (command.createIntermediaryPath()) { commandBuf.writeByte(KeyValueHandler.SUBDOC_BITMASK_MKDIR_P); //0 | SUBDOC_BITMASK_MKDIR_P } else {//from w w w .ja va 2 s .com commandBuf.writeByte(0); } commandBuf.writeShort(pathLength); commandBuf.writeInt(command.fragment().readableBytes()); commandBuf.writeBytes(pathBytes); //copy the fragment but don't move indexes (in case it is retained and reused) commandBuf.writeBytes(command.fragment(), command.fragment().readerIndex(), command.fragment().readableBytes()); //eagerly release the fragment once it's been copied command.fragment().release(); //add the command to the composite buffer compositeBuf.addComponent(commandBuf); compositeBuf.writerIndex(compositeBuf.writerIndex() + commandBuf.readableBytes()); } return compositeBuf; }
From source file:com.couchbase.client.core.message.kv.subdoc.simple.AbstractSubdocRequest.java
License:Apache License
protected ByteBuf createContent(ByteBuf pathByteBuf, ByteBuf... restOfContent) { if (restOfContent == null || restOfContent.length == 0) { return pathByteBuf; } else {/*w ww . j av a 2 s .co m*/ CompositeByteBuf composite = Unpooled.compositeBuffer(1 + restOfContent.length); composite.addComponent(pathByteBuf); composite.writerIndex(composite.writerIndex() + pathByteBuf.readableBytes()); for (ByteBuf component : restOfContent) { composite.addComponent(component); composite.writerIndex(composite.writerIndex() + component.readableBytes()); } return composite; } }
From source file:com.digitalpetri.opcua.stack.core.channel.ChunkDecoder.java
License:Apache License
private ByteBuf decode(Delegate delegate, SecureChannel channel, List<ByteBuf> chunkBuffers) throws UaException { CompositeByteBuf composite = BufferUtil.compositeBuffer(); int signatureSize = delegate.getSignatureSize(channel); int cipherTextBlockSize = delegate.getCipherTextBlockSize(channel); boolean encrypted = delegate.isEncryptionEnabled(channel); boolean signed = delegate.isSigningEnabled(channel); for (ByteBuf chunkBuffer : chunkBuffers) { char chunkType = (char) chunkBuffer.getByte(3); chunkBuffer.skipBytes(SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE); delegate.readSecurityHeader(channel, chunkBuffer); if (encrypted) { decryptChunk(delegate, channel, chunkBuffer); }/*from w ww . ja va2 s .c om*/ int encryptedStart = chunkBuffer.readerIndex(); chunkBuffer.readerIndex(0); if (signed) { delegate.verifyChunk(channel, chunkBuffer); } int paddingSize = encrypted ? getPaddingSize(cipherTextBlockSize, signatureSize, chunkBuffer) : 0; int bodyEnd = chunkBuffer.readableBytes() - signatureSize - paddingSize; chunkBuffer.readerIndex(encryptedStart); SequenceHeader sequenceHeader = SequenceHeader.decode(chunkBuffer); long sequenceNumber = sequenceHeader.getSequenceNumber(); lastRequestId = sequenceHeader.getRequestId(); if (lastSequenceNumber == -1) { lastSequenceNumber = sequenceNumber; } else { if (lastSequenceNumber + 1 != sequenceNumber) { String message = String.format("expected sequence number %s but received %s", lastSequenceNumber + 1, sequenceNumber); logger.error(message); logger.error(ByteBufUtil.hexDump(chunkBuffer, 0, chunkBuffer.writerIndex())); throw new UaException(StatusCodes.Bad_SecurityChecksFailed, message); } lastSequenceNumber = sequenceNumber; } ByteBuf bodyBuffer = chunkBuffer.readSlice(bodyEnd - chunkBuffer.readerIndex()); if (chunkType == 'A') { ErrorMessage errorMessage = ErrorMessage.decode(bodyBuffer); throw new MessageAbortedException(errorMessage.getError(), errorMessage.getReason()); } composite.addComponent(bodyBuffer); composite.writerIndex(composite.writerIndex() + bodyBuffer.readableBytes()); } return composite.order(ByteOrder.LITTLE_ENDIAN); }
From source file:com.mastfrog.acteur.server.HttpObjectAggregator.java
License:Open Source License
@Override protected void decode(final ChannelHandlerContext ctx, HttpObject msg, List<Object> out) throws Exception { FullHttpMessage currentMessage = this.currentMessage; if (msg instanceof HttpMessage) { tooLongFrameFound = false;//from w ww .j a v a 2s .c om assert currentMessage == null; HttpMessage m = (HttpMessage) msg; // Handle the 'Expect: 100-continue' header if necessary. // TODO: Respond with 413 Request Entity Too Large // and discard the traffic or close the connection. // No need to notify the upstream handlers - just log. // If decoding a response, just throw an exception. if (is100ContinueExpected(m)) { ByteBuf buf = CONTINUE_LINE.duplicate(); buf.retain(); ctx.writeAndFlush(buf).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { ctx.fireExceptionCaught(future.cause()); } } }); } if (!m.getDecoderResult().isSuccess()) { removeTransferEncodingChunked(m); out.add(toFullMessage(m)); this.currentMessage = null; return; } if (msg instanceof HttpRequest) { HttpRequest header = (HttpRequest) msg; this.currentMessage = currentMessage = new DefaultFullHttpRequest(header.getProtocolVersion(), header.getMethod(), header.getUri(), Unpooled.compositeBuffer(maxCumulationBufferComponents)); } else if (msg instanceof HttpResponse) { HttpResponse header = (HttpResponse) msg; this.currentMessage = currentMessage = new DefaultFullHttpResponse(header.getProtocolVersion(), header.getStatus(), Unpooled.compositeBuffer(maxCumulationBufferComponents)); } else { throw new Error(); } currentMessage.headers().set(m.headers()); // A streamed message - initialize the cumulative buffer, and wait for incoming chunks. removeTransferEncodingChunked(currentMessage); } else if (msg instanceof HttpContent) { if (tooLongFrameFound) { if (msg instanceof LastHttpContent) { this.currentMessage = null; } // already detect the too long frame so just discard the content return; } assert currentMessage != null; // Merge the received chunk into the content of the current message. HttpContent chunk = (HttpContent) msg; CompositeByteBuf content = (CompositeByteBuf) currentMessage.content(); if (content.readableBytes() > maxContentLength - chunk.content().readableBytes()) { tooLongFrameFound = true; // release current message to prevent leaks currentMessage.release(); this.currentMessage = null; throw new TooLongFrameException("HTTP content length exceeded " + maxContentLength + " bytes."); } // Append the content of the chunk if (chunk.content().isReadable()) { chunk.retain(); content.addComponent(chunk.content()); content.writerIndex(content.writerIndex() + chunk.content().readableBytes()); } final boolean last; if (!chunk.getDecoderResult().isSuccess()) { currentMessage.setDecoderResult(DecoderResult.failure(chunk.getDecoderResult().cause())); last = true; } else { last = chunk instanceof LastHttpContent; } if (last) { this.currentMessage = null; // Merge trailing headers into the message. if (chunk instanceof LastHttpContent) { LastHttpContent trailer = (LastHttpContent) chunk; currentMessage.headers().add(trailer.trailingHeaders()); } // Set the 'Content-Length' header. currentMessage.headers().set(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(content.readableBytes())); // All done out.add(currentMessage); } } else { throw new Error(); } }
From source file:com.mongodb.connection.netty.NettyStream.java
License:Apache License
@Override public void writeAsync(final List<ByteBuf> buffers, final AsyncCompletionHandler<Void> handler) { CompositeByteBuf composite = PooledByteBufAllocator.DEFAULT.compositeBuffer(); for (ByteBuf cur : buffers) { io.netty.buffer.ByteBuf byteBuf = ((NettyByteBuf) cur).asByteBuf(); composite.addComponent(byteBuf.retain()); composite.writerIndex(composite.writerIndex() + byteBuf.writerIndex()); }/*from w w w .j a v a 2s.co m*/ channel.writeAndFlush(composite).addListener(new ChannelFutureListener() { @Override public void operationComplete(final ChannelFuture future) throws Exception { if (!future.isSuccess()) { handler.failed(future.cause()); } else { handler.completed(null); } } }); }
From source file:com.mongodb.connection.netty.NettyStream.java
License:Apache License
@Override public void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler) { ByteBuf buffer = null;// w w w . j a v a 2s .c om Throwable exceptionResult = null; synchronized (this) { exceptionResult = pendingException; if (exceptionResult == null) { if (!hasBytesAvailable(numBytes)) { pendingReader = new PendingReader(numBytes, handler); } else { CompositeByteBuf composite = allocator.compositeBuffer(pendingInboundBuffers.size()); int bytesNeeded = numBytes; for (Iterator<io.netty.buffer.ByteBuf> iter = pendingInboundBuffers.iterator(); iter .hasNext();) { io.netty.buffer.ByteBuf next = iter.next(); int bytesNeededFromCurrentBuffer = Math.min(next.readableBytes(), bytesNeeded); if (bytesNeededFromCurrentBuffer == next.readableBytes()) { composite.addComponent(next); iter.remove(); } else { next.retain(); composite.addComponent(next.readSlice(bytesNeededFromCurrentBuffer)); } composite.writerIndex(composite.writerIndex() + bytesNeededFromCurrentBuffer); bytesNeeded -= bytesNeededFromCurrentBuffer; if (bytesNeeded == 0) { break; } } buffer = new NettyByteBuf(composite).flip(); } } } if (exceptionResult != null) { handler.failed(exceptionResult); } if (buffer != null) { handler.completed(buffer); } }
From source file:com.talent.nio.communicate.receive.DecodeRunnable.java
License:Open Source License
@Override public void run() { while (getMsgQueue().size() > 0) { ByteBuf queuedatas = null;//ww w . ja v a 2 s .c om CompositeByteBuf datas = Unpooled.compositeBuffer(); if (lastDatas != null) { channelContext.getStatVo().setCurrentOgnzTimestamp(SystemTimer.currentTimeMillis()); lastDatas.readerIndex(0); datas.addComponents(lastDatas); lastDatas = null; } int count = 0; label_2: while ((queuedatas = getMsgQueue().poll()) != null) { queuedatas = queuedatas.order(channelContext.getByteOrder()); if (DebugUtils.isNeedDebug(channelContext)) { // long xx = 999999999999999999L; log.error("queuedatas:" + ArrayUtils.toString(queuedatas)); } datas.addComponents(queuedatas); channelContext.getStatVo().setCurrentOgnzTimestamp(SystemTimer.currentTimeMillis()); count++; if (needLength != -1) // ???? { if (datas.capacity() < needLength) // ?? { // log.error("??----capacity:{}, needLength:{}", datas.capacity(), needLength); continue; } else { // log.error("?----capacity:{}, needLength:{}", datas.capacity(), needLength); break label_2; } } else // ??? { if (count == 50) { log.warn( "???{}???{}", count, getMsgQueue().size()); break label_2; } } } channelContext.getStatVo().setCurrentOgnzTimestamp(SystemTimer.currentTimeMillis()); PacketWithMeta packetWithMeta = null; try { // ByteBuffer buffer = ByteBuffer.wrap(datas); datas.writerIndex(datas.capacity()); datas.readerIndex(0); packetWithMeta = channelContext.getDecoder().decode(datas, channelContext); needLength = -1; if (packetWithMeta == null) { // ??? lastDatas = datas; lastDatas.readerIndex(0); if (DebugUtils.isNeedDebug(channelContext)) { log.error("???:{}", lastDatas); } } else if (packetWithMeta.getPackets() == null || packetWithMeta.getPackets().size() == 0) { // ??? lastDatas = datas; lastDatas.readerIndex(0); needLength = packetWithMeta.getNeedLength(); if (DebugUtils.isNeedDebug(channelContext)) { log.error("????:{}", needLength); } } else { int len = packetWithMeta.getPacketLenght(); // lastDatas = new byte[datas.capacity() - len]; // System.arraycopy(datas, len, lastDatas, 0, // lastDatas.length); if (datas.capacity() - len > 0) { lastDatas = datas.copy(len, datas.capacity() - len); if (DebugUtils.isNeedDebug(channelContext)) { log.error("??:{}, {}", datas.capacity() - len, lastDatas); } } else { lastDatas = null; if (DebugUtils.isNeedDebug(channelContext)) { log.error("??:{}", lastDatas); } } processMsgAndStat(packetWithMeta.getPackets(), len, false); } } catch (DecodeException e) { log.error(e.getMessage(), e); channelContext.getErrorPackageHandler().handle(channelContext.getSocketChannel(), channelContext, e.getMessage()); } } }
From source file:com.uber.tchannel.codecs.CodecUtils.java
License:Open Source License
public static ByteBuf writeArgs(ByteBufAllocator allocator, ByteBuf header, List<ByteBuf> args) { int writableBytes = TFrame.MAX_FRAME_PAYLOAD_LENGTH - header.readableBytes(); List<ByteBuf> bufs = new ArrayList<>(7); bufs.add(header);//w ww .ja v a 2 s . c om while (!args.isEmpty()) { ByteBuf arg = args.get(0); int len = writeArg(allocator, arg, writableBytes, bufs); writableBytes -= len; if (writableBytes <= TFrame.FRAME_SIZE_LENGTH) { break; } if (arg.readableBytes() == 0) { args.remove(0); } } CompositeByteBuf comp = allocator.compositeBuffer(); comp.addComponents(bufs); comp.writerIndex(TFrame.MAX_FRAME_PAYLOAD_LENGTH - writableBytes); return comp; }