Polishing

This commit is contained in:
Juergen Hoeller 2018-12-06 15:53:57 +01:00
parent 6a012147c4
commit aaaf81ed99
8 changed files with 35 additions and 40 deletions

View File

@ -25,10 +25,10 @@ import java.util.function.IntPredicate;
* Basic abstraction over byte buffers. * Basic abstraction over byte buffers.
* *
* <p>{@code DataBuffer}s has a separate {@linkplain #readPosition() read} and * <p>{@code DataBuffer}s has a separate {@linkplain #readPosition() read} and
* {@linkplain #writePosition() write} position, as opposed to {@code ByteBuffer}'s single * {@linkplain #writePosition() write} position, as opposed to {@code ByteBuffer}'s
* {@linkplain ByteBuffer#position() position}. As such, the {@code DataBuffer} does not require * single {@linkplain ByteBuffer#position() position}. As such, the {@code DataBuffer}
* a {@linkplain ByteBuffer#flip() flip} to read after writing. In general, the following invariant * does not require a {@linkplain ByteBuffer#flip() flip} to read after writing. In general,
* holds for the read and write positions, and the capacity: * the following invariant holds for the read and write positions, and the capacity:
* *
* <blockquote> * <blockquote>
* <tt>0</tt> <tt>&lt;=</tt> * <tt>0</tt> <tt>&lt;=</tt>
@ -41,8 +41,8 @@ import java.util.function.IntPredicate;
* similar to {@code StringBuilder}. * similar to {@code StringBuilder}.
* *
* <p>The main purpose of the {@code DataBuffer} abstraction is to provide a convenient wrapper * <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 * 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). * can also be used on non-Netty platforms (i.e. Servlet containers).
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 5.0 * @since 5.0
@ -239,8 +239,8 @@ public interface DataBuffer {
ByteBuffer asByteBuffer(); ByteBuffer asByteBuffer();
/** /**
* Expose a subsequence of this buffer's bytes as a {@link ByteBuffer}. Data between this * Expose a subsequence of this buffer's bytes as a {@link ByteBuffer}. Data between
* {@code DataBuffer} and the returned {@code ByteBuffer} is shared; though * this {@code DataBuffer} and the returned {@code ByteBuffer} is shared; though
* changes in the returned buffer's {@linkplain ByteBuffer#position() position} * changes in the returned buffer's {@linkplain ByteBuffer#position() position}
* will not be reflected in the reading nor writing position of this data buffer. * 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 * @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 * 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 * shared between the returned stream and this data buffer. The underlying buffer will
* <strong>not</strong> be {@linkplain DataBufferUtils#release(DataBuffer) released} when the * <strong>not</strong> be {@linkplain DataBufferUtils#release(DataBuffer) released}
* input stream is {@linkplain InputStream#close() closed}. * when the input stream is {@linkplain InputStream#close() closed}.
* @return this data buffer as an input stream * @return this data buffer as an input stream
* @see #asInputStream(boolean) * @see #asInputStream(boolean)
*/ */

View File

@ -20,8 +20,8 @@ import java.nio.ByteBuffer;
import java.util.List; import java.util.List;
/** /**
* A factory for {@link DataBuffer DataBuffers}, allowing for allocation and wrapping of * A factory for {@link DataBuffer DataBuffers}, allowing for allocation and
* data buffers. * wrapping of data buffers.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 5.0 * @since 5.0
@ -74,4 +74,5 @@ public interface DataBufferFactory {
* @since 5.0.3 * @since 5.0.3
*/ */
DataBuffer join(List<? extends DataBuffer> dataBuffers); DataBuffer join(List<? extends DataBuffer> dataBuffers);
} }

View File

@ -267,12 +267,10 @@ public abstract class DataBufferUtils {
* process when subscribed to, and that publishes any writing errors and the completion signal * process when subscribed to, and that publishes any writing errors and the completion signal
* @since 5.1 * @since 5.1
*/ */
public static Flux<DataBuffer> write( public static Flux<DataBuffer> write(Publisher<DataBuffer> source, AsynchronousFileChannel channel) {
Publisher<DataBuffer> source, AsynchronousFileChannel channel) {
return write(source, channel, 0); return write(source, channel, 0);
} }
/** /**
* Write the given stream of {@link DataBuffer DataBuffers} to the given {@code AsynchronousFileChannel}. * 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 * Does <strong>not</strong> close the channel when the flux is terminated, and does

View File

@ -294,10 +294,7 @@ public class DefaultDataBuffer implements DataBuffer {
@Override @Override
public DefaultDataBuffer write(DataBuffer... buffers) { public DefaultDataBuffer write(DataBuffer... buffers) {
if (!ObjectUtils.isEmpty(buffers)) { if (!ObjectUtils.isEmpty(buffers)) {
ByteBuffer[] byteBuffers = write(Arrays.stream(buffers).map(DataBuffer::asByteBuffer).toArray(ByteBuffer[]::new));
Arrays.stream(buffers).map(DataBuffer::asByteBuffer)
.toArray(ByteBuffer[]::new);
write(byteBuffers);
} }
return this; return this;
} }

View File

@ -87,40 +87,35 @@ public class DefaultDataBufferFactory implements DataBufferFactory {
ByteBuffer byteBuffer = (this.preferDirect ? ByteBuffer byteBuffer = (this.preferDirect ?
ByteBuffer.allocateDirect(initialCapacity) : ByteBuffer.allocateDirect(initialCapacity) :
ByteBuffer.allocate(initialCapacity)); ByteBuffer.allocate(initialCapacity));
return DefaultDataBuffer.fromEmptyByteBuffer(this, byteBuffer); return DefaultDataBuffer.fromEmptyByteBuffer(this, byteBuffer);
} }
@Override @Override
public DefaultDataBuffer wrap(ByteBuffer byteBuffer) { public DefaultDataBuffer wrap(ByteBuffer byteBuffer) {
ByteBuffer sliced = byteBuffer.slice(); return DefaultDataBuffer.fromFilledByteBuffer(this, byteBuffer.slice());
return DefaultDataBuffer.fromFilledByteBuffer(this, sliced);
} }
@Override @Override
public DefaultDataBuffer wrap(byte[] bytes) { public DefaultDataBuffer wrap(byte[] bytes) {
ByteBuffer wrapper = ByteBuffer.wrap(bytes); return DefaultDataBuffer.fromFilledByteBuffer(this, ByteBuffer.wrap(bytes));
return DefaultDataBuffer.fromFilledByteBuffer(this, wrapper);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
* <p>This implementation creates a single {@link DefaultDataBuffer} to contain the data * <p>This implementation creates a single {@link DefaultDataBuffer}
* in {@code dataBuffers}. * to contain the data in {@code dataBuffers}.
*/ */
@Override @Override
public DefaultDataBuffer join(List<? extends DataBuffer> dataBuffers) { public DefaultDataBuffer join(List<? extends DataBuffer> dataBuffers) {
Assert.notEmpty(dataBuffers, "'dataBuffers' must not be empty"); Assert.notEmpty(dataBuffers, "DataBuffer List must not be empty");
int capacity = dataBuffers.stream().mapToInt(DataBuffer::readableByteCount).sum();
int capacity = dataBuffers.stream()
.mapToInt(DataBuffer::readableByteCount)
.sum();
DefaultDataBuffer result = allocateBuffer(capacity); DefaultDataBuffer result = allocateBuffer(capacity);
dataBuffers.forEach(result::write); dataBuffers.forEach(result::write);
dataBuffers.forEach(DataBufferUtils::release); dataBuffers.forEach(DataBufferUtils::release);
return result; return result;
} }
@Override @Override
public String toString() { public String toString() {
return "DefaultDataBufferFactory (preferDirect=" + this.preferDirect + ")"; return "DefaultDataBufferFactory (preferDirect=" + this.preferDirect + ")";

View File

@ -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 * @param byteBuf the buffer to base this buffer on
*/ */
NettyDataBuffer(ByteBuf byteBuf, NettyDataBufferFactory dataBufferFactory) { NettyDataBuffer(ByteBuf byteBuf, NettyDataBufferFactory dataBufferFactory) {
@ -279,7 +279,7 @@ public class NettyDataBuffer implements PooledDataBuffer {
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
return (this == other || (other instanceof NettyDataBuffer && return (this == other || (other instanceof NettyDataBuffer &&
this.byteBuf.equals(((NettyDataBuffer) other).byteBuf))); this.byteBuf.equals(((NettyDataBuffer) other).byteBuf)));
} }

View File

@ -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 * @param byteBufAllocator the factory to use
* @see io.netty.buffer.PooledByteBufAllocator * @see io.netty.buffer.PooledByteBufAllocator
* @see io.netty.buffer.UnpooledByteBufAllocator * @see io.netty.buffer.UnpooledByteBufAllocator
@ -52,6 +52,7 @@ public class NettyDataBufferFactory implements DataBufferFactory {
this.byteBufAllocator = byteBufAllocator; this.byteBufAllocator = byteBufAllocator;
} }
/** /**
* Return the {@code ByteBufAllocator} used by this factory. * Return the {@code ByteBufAllocator} used by this factory.
*/ */
@ -133,4 +134,5 @@ public class NettyDataBufferFactory implements DataBufferFactory {
public String toString() { public String toString() {
return "NettyDataBufferFactory (" + this.byteBufAllocator + ")"; return "NettyDataBufferFactory (" + this.byteBufAllocator + ")";
} }
} }

View File

@ -17,8 +17,8 @@
package org.springframework.core.io.buffer; package org.springframework.core.io.buffer;
/** /**
* Extension of {@link DataBuffer} that allows for buffer that share a memory * Extension of {@link DataBuffer} that allows for buffer that share
* pool. Introduces methods for reference counting. * a memory pool. Introduces methods for reference counting.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 5.0 * @since 5.0
@ -26,7 +26,8 @@ package org.springframework.core.io.buffer;
public interface PooledDataBuffer extends DataBuffer { 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 * @since 5.1
*/ */
boolean isAllocated(); boolean isAllocated();
@ -38,9 +39,10 @@ public interface PooledDataBuffer extends DataBuffer {
PooledDataBuffer retain(); PooledDataBuffer retain();
/** /**
* Decrease the reference count for this buffer by one, and deallocate it * Decrease the reference count for this buffer by one,
* once the count reaches zero. * and deallocate it once the count reaches zero.
* @return {@code true} if the buffer was deallocated; {@code false} otherwise. * @return {@code true} if the buffer was deallocated;
* {@code false} otherwise
*/ */
boolean release(); boolean release();