Polishing
This commit is contained in:
parent
6a012147c4
commit
aaaf81ed99
|
@ -25,10 +25,10 @@ import java.util.function.IntPredicate;
|
|||
* Basic abstraction over byte buffers.
|
||||
*
|
||||
* <p>{@code DataBuffer}s has a separate {@linkplain #readPosition() read} and
|
||||
* {@linkplain #writePosition() write} position, as opposed to {@code ByteBuffer}'s single
|
||||
* {@linkplain ByteBuffer#position() position}. As such, the {@code DataBuffer} does not require
|
||||
* a {@linkplain ByteBuffer#flip() flip} to read after writing. In general, the following invariant
|
||||
* holds for the read and write positions, and the capacity:
|
||||
* {@linkplain #writePosition() write} position, as opposed to {@code ByteBuffer}'s
|
||||
* single {@linkplain ByteBuffer#position() position}. As such, the {@code DataBuffer}
|
||||
* does not require a {@linkplain ByteBuffer#flip() flip} to read after writing. In general,
|
||||
* the following invariant holds for the read and write positions, and the capacity:
|
||||
*
|
||||
* <blockquote>
|
||||
* <tt>0</tt> <tt><=</tt>
|
||||
|
@ -41,8 +41,8 @@ import java.util.function.IntPredicate;
|
|||
* similar to {@code StringBuilder}.
|
||||
*
|
||||
* <p>The main purpose of the {@code DataBuffer} abstraction is to provide a convenient wrapper
|
||||
* around {@link ByteBuffer} that is similar to Netty's {@link io.netty.buffer.ByteBuf}, but that
|
||||
* can also be used on non-Netty platforms (i.e. Servlet).
|
||||
* around {@link ByteBuffer} which is similar to Netty's {@link io.netty.buffer.ByteBuf} but
|
||||
* can also be used on non-Netty platforms (i.e. Servlet containers).
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 5.0
|
||||
|
@ -239,8 +239,8 @@ public interface DataBuffer {
|
|||
ByteBuffer asByteBuffer();
|
||||
|
||||
/**
|
||||
* Expose a subsequence of this buffer's bytes as a {@link ByteBuffer}. Data between this
|
||||
* {@code DataBuffer} and the returned {@code ByteBuffer} is shared; though
|
||||
* Expose a subsequence of this buffer's bytes as a {@link ByteBuffer}. Data between
|
||||
* this {@code DataBuffer} and the returned {@code ByteBuffer} is shared; though
|
||||
* changes in the returned buffer's {@linkplain ByteBuffer#position() position}
|
||||
* will not be reflected in the reading nor writing position of this data buffer.
|
||||
* @param index the index at which to start the byte buffer
|
||||
|
@ -253,8 +253,8 @@ public interface DataBuffer {
|
|||
/**
|
||||
* Expose this buffer's data as an {@link InputStream}. Both data and read position are
|
||||
* shared between the returned stream and this data buffer. The underlying buffer will
|
||||
* <strong>not</strong> be {@linkplain DataBufferUtils#release(DataBuffer) released} when the
|
||||
* input stream is {@linkplain InputStream#close() closed}.
|
||||
* <strong>not</strong> be {@linkplain DataBufferUtils#release(DataBuffer) released}
|
||||
* when the input stream is {@linkplain InputStream#close() closed}.
|
||||
* @return this data buffer as an input stream
|
||||
* @see #asInputStream(boolean)
|
||||
*/
|
||||
|
|
|
@ -20,8 +20,8 @@ import java.nio.ByteBuffer;
|
|||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A factory for {@link DataBuffer DataBuffers}, allowing for allocation and wrapping of
|
||||
* data buffers.
|
||||
* A factory for {@link DataBuffer DataBuffers}, allowing for allocation and
|
||||
* wrapping of data buffers.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 5.0
|
||||
|
@ -74,4 +74,5 @@ public interface DataBufferFactory {
|
|||
* @since 5.0.3
|
||||
*/
|
||||
DataBuffer join(List<? extends DataBuffer> dataBuffers);
|
||||
|
||||
}
|
||||
|
|
|
@ -267,12 +267,10 @@ public abstract class DataBufferUtils {
|
|||
* process when subscribed to, and that publishes any writing errors and the completion signal
|
||||
* @since 5.1
|
||||
*/
|
||||
public static Flux<DataBuffer> write(
|
||||
Publisher<DataBuffer> source, AsynchronousFileChannel channel) {
|
||||
public static Flux<DataBuffer> write(Publisher<DataBuffer> source, AsynchronousFileChannel channel) {
|
||||
return write(source, channel, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write the given stream of {@link DataBuffer DataBuffers} to the given {@code AsynchronousFileChannel}.
|
||||
* Does <strong>not</strong> close the channel when the flux is terminated, and does
|
||||
|
|
|
@ -294,10 +294,7 @@ public class DefaultDataBuffer implements DataBuffer {
|
|||
@Override
|
||||
public DefaultDataBuffer write(DataBuffer... buffers) {
|
||||
if (!ObjectUtils.isEmpty(buffers)) {
|
||||
ByteBuffer[] byteBuffers =
|
||||
Arrays.stream(buffers).map(DataBuffer::asByteBuffer)
|
||||
.toArray(ByteBuffer[]::new);
|
||||
write(byteBuffers);
|
||||
write(Arrays.stream(buffers).map(DataBuffer::asByteBuffer).toArray(ByteBuffer[]::new));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
|
|
@ -87,40 +87,35 @@ public class DefaultDataBufferFactory implements DataBufferFactory {
|
|||
ByteBuffer byteBuffer = (this.preferDirect ?
|
||||
ByteBuffer.allocateDirect(initialCapacity) :
|
||||
ByteBuffer.allocate(initialCapacity));
|
||||
|
||||
return DefaultDataBuffer.fromEmptyByteBuffer(this, byteBuffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultDataBuffer wrap(ByteBuffer byteBuffer) {
|
||||
ByteBuffer sliced = byteBuffer.slice();
|
||||
return DefaultDataBuffer.fromFilledByteBuffer(this, sliced);
|
||||
return DefaultDataBuffer.fromFilledByteBuffer(this, byteBuffer.slice());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultDataBuffer wrap(byte[] bytes) {
|
||||
ByteBuffer wrapper = ByteBuffer.wrap(bytes);
|
||||
return DefaultDataBuffer.fromFilledByteBuffer(this, wrapper);
|
||||
return DefaultDataBuffer.fromFilledByteBuffer(this, ByteBuffer.wrap(bytes));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>This implementation creates a single {@link DefaultDataBuffer} to contain the data
|
||||
* in {@code dataBuffers}.
|
||||
* <p>This implementation creates a single {@link DefaultDataBuffer}
|
||||
* to contain the data in {@code dataBuffers}.
|
||||
*/
|
||||
@Override
|
||||
public DefaultDataBuffer join(List<? extends DataBuffer> dataBuffers) {
|
||||
Assert.notEmpty(dataBuffers, "'dataBuffers' must not be empty");
|
||||
|
||||
int capacity = dataBuffers.stream()
|
||||
.mapToInt(DataBuffer::readableByteCount)
|
||||
.sum();
|
||||
Assert.notEmpty(dataBuffers, "DataBuffer List must not be empty");
|
||||
int capacity = dataBuffers.stream().mapToInt(DataBuffer::readableByteCount).sum();
|
||||
DefaultDataBuffer result = allocateBuffer(capacity);
|
||||
dataBuffers.forEach(result::write);
|
||||
dataBuffers.forEach(DataBufferUtils::release);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DefaultDataBufferFactory (preferDirect=" + this.preferDirect + ")";
|
||||
|
|
|
@ -42,7 +42,7 @@ public class NettyDataBuffer implements PooledDataBuffer {
|
|||
|
||||
|
||||
/**
|
||||
* Creates a new {@code NettyDataBuffer} based on the given {@code ByteBuff}.
|
||||
* Create a new {@code NettyDataBuffer} based on the given {@code ByteBuff}.
|
||||
* @param byteBuf the buffer to base this buffer on
|
||||
*/
|
||||
NettyDataBuffer(ByteBuf byteBuf, NettyDataBufferFactory dataBufferFactory) {
|
||||
|
@ -279,7 +279,7 @@ public class NettyDataBuffer implements PooledDataBuffer {
|
|||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return (this == other || (other instanceof NettyDataBuffer &&
|
||||
return (this == other || (other instanceof NettyDataBuffer &&
|
||||
this.byteBuf.equals(((NettyDataBuffer) other).byteBuf)));
|
||||
}
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ public class NettyDataBufferFactory implements DataBufferFactory {
|
|||
|
||||
|
||||
/**
|
||||
* Creates a new {@code NettyDataBufferFactory} based on the given factory.
|
||||
* Create a new {@code NettyDataBufferFactory} based on the given factory.
|
||||
* @param byteBufAllocator the factory to use
|
||||
* @see io.netty.buffer.PooledByteBufAllocator
|
||||
* @see io.netty.buffer.UnpooledByteBufAllocator
|
||||
|
@ -52,6 +52,7 @@ public class NettyDataBufferFactory implements DataBufferFactory {
|
|||
this.byteBufAllocator = byteBufAllocator;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the {@code ByteBufAllocator} used by this factory.
|
||||
*/
|
||||
|
@ -133,4 +134,5 @@ public class NettyDataBufferFactory implements DataBufferFactory {
|
|||
public String toString() {
|
||||
return "NettyDataBufferFactory (" + this.byteBufAllocator + ")";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,8 +17,8 @@
|
|||
package org.springframework.core.io.buffer;
|
||||
|
||||
/**
|
||||
* Extension of {@link DataBuffer} that allows for buffer that share a memory
|
||||
* pool. Introduces methods for reference counting.
|
||||
* Extension of {@link DataBuffer} that allows for buffer that share
|
||||
* a memory pool. Introduces methods for reference counting.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 5.0
|
||||
|
@ -26,7 +26,8 @@ package org.springframework.core.io.buffer;
|
|||
public interface PooledDataBuffer extends DataBuffer {
|
||||
|
||||
/**
|
||||
* Return {@code true} if this buffer is allocated; {@code false} if it has been deallocated.
|
||||
* Return {@code true} if this buffer is allocated;
|
||||
* {@code false} if it has been deallocated.
|
||||
* @since 5.1
|
||||
*/
|
||||
boolean isAllocated();
|
||||
|
@ -38,9 +39,10 @@ public interface PooledDataBuffer extends DataBuffer {
|
|||
PooledDataBuffer retain();
|
||||
|
||||
/**
|
||||
* Decrease the reference count for this buffer by one, and deallocate it
|
||||
* once the count reaches zero.
|
||||
* @return {@code true} if the buffer was deallocated; {@code false} otherwise.
|
||||
* Decrease the reference count for this buffer by one,
|
||||
* and deallocate it once the count reaches zero.
|
||||
* @return {@code true} if the buffer was deallocated;
|
||||
* {@code false} otherwise
|
||||
*/
|
||||
boolean release();
|
||||
|
||||
|
|
Loading…
Reference in New Issue