/** * Release the payload {@code DataBuffer} which is useful on runtimes * (e.g. Netty) with pooled buffers such as Netty. A shortcut for: * <pre> * DataBuffer payload = message.getPayload(); * DataBufferUtils.release(payload); * </pre> * @see DataBufferUtils#release(DataBuffer) */ public void release() { DataBufferUtils.release(this.payload); }
private Consumer<DataBuffer> expectDataBuffer(byte[] expected) { return actual -> { byte[] actualBytes = new byte[actual.readableByteCount()]; actual.read(actualBytes); assertArrayEquals(expected, actualBytes); DataBufferUtils.release(actual); }; } }
@Override public DataBuffer allocateBuffer(int initialCapacity) { return allocateBufferInternal(this.delegate.allocateBuffer(initialCapacity)); }
private DataBuffer stringBuffer(String value) { byte[] bytes = value.getBytes(StandardCharsets.UTF_8); DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length); buffer.write(bytes); return buffer; }
/** * Creates a new {@code LeakAwareDataBufferFactory} by wrapping a * {@link DefaultDataBufferFactory}. */ public LeakAwareDataBufferFactory() { this(new DefaultDataBufferFactory()); }
@Override public DataBuffer ensureCapacity(int length) { if (length > writableByteCount()) { int newCapacity = calculateCapacity(this.writePosition + length); capacity(newCapacity); } return this; }
@Override public void write(byte[] bytes, int off, int len) throws IOException { DefaultDataBuffer.this.write(bytes, off, len); } }
@Override public DataBuffer join(List<? extends DataBuffer> dataBuffers) { return new LeakAwareDataBuffer(this.delegate.join(dataBuffers), this); }
/** * Obtain a {@code AsynchronousFileChannel} from the given supplier, and read it into a * {@code Flux} of {@code DataBuffer}s. Closes the channel when the flux is terminated. * @param channelSupplier the supplier for the channel to read from * @param dataBufferFactory the factory to create data buffers with * @param bufferSize the maximum size of the data buffers * @return a flux of data buffers read from the given channel */ public static Flux<DataBuffer> readAsynchronousFileChannel( Callable<AsynchronousFileChannel> channelSupplier, DataBufferFactory dataBufferFactory, int bufferSize) { return readAsynchronousFileChannel(channelSupplier, 0, dataBufferFactory, bufferSize); }
@Override public DefaultDataBuffer writePosition(int writePosition) { assertIndex(writePosition >= this.readPosition, "'writePosition' %d must be >= %d", writePosition, this.readPosition); assertIndex(writePosition <= this.capacity, "'writePosition' %d must be <= %d", writePosition, this.capacity); this.writePosition = writePosition; return this; }
/** * Wrap the given Netty {@link ByteBuf} in a {@code NettyDataBuffer}. * @param byteBuf the Netty byte buffer to wrap * @return the wrapped buffer */ public NettyDataBuffer wrap(ByteBuf byteBuf) { return new NettyDataBuffer(byteBuf, this); }
private DataBuffer generateNewLine() { DataBuffer buffer = this.bufferFactory.allocateBuffer(2); buffer.write((byte)'\r'); buffer.write((byte)'\n'); return buffer; }
@Override protected void discardData(DataBuffer dataBuffer) { DataBufferUtils.release(dataBuffer); } }
@Override public DataBuffer allocateBuffer() { return allocateBufferInternal(this.delegate.allocateBuffer()); }
private void checkIndex(int index, int length) { assertIndex(index >= 0, "index %d must be >= 0", index); assertIndex(length >= 0, "length %d must be >= 0", index); assertIndex(index <= this.capacity, "index %d must be <= %d", index, this.capacity); assertIndex(length <= this.capacity, "length %d must be <= %d", index, this.capacity); }
private DataBuffer generateLastLine(byte[] boundary) { DataBuffer buffer = this.bufferFactory.allocateBuffer(boundary.length + 6); buffer.write((byte)'-'); buffer.write((byte)'-'); buffer.write(boundary); buffer.write((byte)'-'); buffer.write((byte)'-'); buffer.write((byte)'\r'); buffer.write((byte)'\n'); return buffer; }
@Override protected void discardData(DataBuffer dataBuffer) { DataBufferUtils.release(dataBuffer); } }
protected DataBuffer byteBuffer(byte[] value) { DataBuffer buffer = this.bufferFactory.allocateBuffer(value.length); buffer.write(value); return buffer; }