@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { cBuf = new CompositeByteBuf(ctx.alloc(), false, Integer.MAX_VALUE); }
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (msg instanceof ByteBuf) { ByteBuf buf = (ByteBuf) msg; cBuf.addComponent(buf); cBuf.writerIndex(cBuf.writerIndex() + buf.readableBytes()); } else { ctx.write(msg); } }
@Override public void flush(ChannelHandlerContext ctx) throws Exception { if (cBuf.isReadable()) { byte[] b = new byte[cBuf.readableBytes()]; cBuf.readBytes(b); cBuf.discardReadComponents(); byte[] wrapped = saslClient.wrap(b, 0, b.length); ByteBuf buf = ctx.alloc().ioBuffer(4 + wrapped.length); buf.writeInt(wrapped.length); buf.writeBytes(wrapped); ctx.write(buf); } ctx.flush(); }
ensureAccessible(); final int readerIndex = readerIndex(); if (readerIndex == 0) { return this; int writerIndex = writerIndex(); if (readerIndex == writerIndex && writerIndex == capacity()) { for (Component c: components) { c.freeIfNecessary(); setIndex(0, 0); adjustMarkers(readerIndex); return this; int firstComponentId = toComponentIndex(readerIndex); for (int i = 0; i < firstComponentId; i ++) { components.get(i).freeIfNecessary(); updateComponentOffsets(0); setIndex(readerIndex - offset, writerIndex - offset); adjustMarkers(offset); return this;
@Override public CompositeByteBuf capacity(int newCapacity) { checkNewCapacity(newCapacity); int oldCapacity = capacity(); if (newCapacity > oldCapacity) { final int paddingLength = newCapacity - oldCapacity; int nComponents = components.size(); if (nComponents < maxNumComponents) { padding = allocBuffer(paddingLength); padding.setIndex(0, paddingLength); addComponent0(false, components.size(), padding); } else { padding = allocBuffer(paddingLength); padding.setIndex(0, paddingLength); addComponent0(false, components.size(), padding); consolidateIfNeeded(); if (readerIndex() > newCapacity) { setIndex(newCapacity, newCapacity); } else if (writerIndex() > newCapacity) { writerIndex(newCapacity);
continue; fullCompressedContent.addComponent(true, partCompressedContent); if (fullCompressedContent.numComponents() <= 0) { fullCompressedContent.release(); throw new CodecException("cannot read compressed buffer"); int realLength = fullCompressedContent.readableBytes() - FRAME_TAIL.length; compressedContent = fullCompressedContent.slice(0, realLength); } else { compressedContent = fullCompressedContent;
@Override public CompositeByteBuf readBytes(byte[] dst, int dstIndex, int length) { wrapped.readBytes(dst, dstIndex, length); return this; }
@Override public CompositeByteBuf addComponent(boolean increaseWriterIndex, ByteBuf buffer) { wrapped.addComponent(increaseWriterIndex, buffer); return this; }
continue; compositeUncompressedContent.addComponent(true, partUncompressedContent); if (readable && compositeUncompressedContent.numComponents() <= 0) { compositeUncompressedContent.release(); throw new CodecException("cannot read uncompressed buffer");
@Override protected short _getShort(int index) { Component c = findComponent(index); if (index + 2 <= c.endOffset) { return c.buf.getShort(index - c.offset); } else if (order() == ByteOrder.BIG_ENDIAN) { return (short) ((_getByte(index) & 0xff) << 8 | _getByte(index + 1) & 0xff); } else { return (short) (_getByte(index) & 0xff | (_getByte(index + 1) & 0xff) << 8); } }
@Override public ByteBuffer[] nioBuffers() { return nioBuffers(readerIndex(), readableBytes()); }
@Override protected int _getUnsignedMedium(int index) { Component c = findComponent(index); if (index + 3 <= c.endOffset) { return c.buf.getUnsignedMedium(index - c.offset); } else if (order() == ByteOrder.BIG_ENDIAN) { return (_getShort(index) & 0xffff) << 8 | _getByte(index + 2) & 0xff; } else { return _getShort(index) & 0xFFFF | (_getByte(index + 2) & 0xFF) << 16; } }
@Override protected int _getUnsignedMediumLE(int index) { Component c = findComponent(index); if (index + 3 <= c.endOffset) { return c.buf.getUnsignedMediumLE(index - c.offset); } else if (order() == ByteOrder.BIG_ENDIAN) { return _getShortLE(index) & 0xffff | (_getByte(index + 2) & 0xff) << 16; } else { return (_getShortLE(index) & 0xffff) << 8 | _getByte(index + 2) & 0xff; } }
@Override protected ByteBuf compose(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next) { final int wrapDataSize = SslHandler.this.wrapDataSize; if (cumulation instanceof CompositeByteBuf) { CompositeByteBuf composite = (CompositeByteBuf) cumulation; int numComponents = composite.numComponents(); if (numComponents == 0 || !attemptCopyToCumulation(composite.internalComponent(numComponents - 1), next, wrapDataSize)) { composite.addComponent(true, next); } return composite; } return attemptCopyToCumulation(cumulation, next, wrapDataSize) ? cumulation : composeIntoComposite(alloc, cumulation, next); }
public ByteBuf toByteBuf() { return Unpooled.compositeBuffer().addComponents(value).writerIndex(size()).readerIndex(0); }
/** * Compose {@code cumulation} and {@code next} into a new {@link CompositeByteBuf}. */ protected final ByteBuf composeIntoComposite(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next) { // Create a composite buffer to accumulate this pair and potentially all the buffers // in the queue. Using +2 as we have already dequeued current and next. CompositeByteBuf composite = alloc.compositeBuffer(size() + 2); try { composite.addComponent(true, cumulation); composite.addComponent(true, next); } catch (Throwable cause) { composite.release(); PlatformDependent.throwException(cause); } return composite; }
@Override protected ByteBuf composeFirst(ByteBufAllocator allocator, ByteBuf first) { if (first instanceof CompositeByteBuf) { CompositeByteBuf composite = (CompositeByteBuf) first; first = allocator.directBuffer(composite.readableBytes()); try { first.writeBytes(composite); } catch (Throwable cause) { first.release(); PlatformDependent.throwException(cause); } composite.release(); } return first; }
boolean wasAdded = false; try { checkComponentIndex(cIndex); wasAdded = true; if (readableBytes != 0) { updateComponentOffsets(cIndex); writerIndex(writerIndex() + buffer.readableBytes());
@Override public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { cBuf.release(); cBuf = null; } }
@Override protected long _getLong(int index) { Component c = findComponent(index); if (index + 8 <= c.endOffset) { return c.buf.getLong(index - c.offset); } else if (order() == ByteOrder.BIG_ENDIAN) { return (_getInt(index) & 0xffffffffL) << 32 | _getInt(index + 4) & 0xffffffffL; } else { return _getInt(index) & 0xFFFFFFFFL | (_getInt(index + 4) & 0xFFFFFFFFL) << 32; } }