From aaaf81ed99eaf83630eab2bedebb454819bfd4d0 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 6 Dec 2018 15:53:57 +0100 Subject: [PATCH] Polishing --- .../core/io/buffer/DataBuffer.java | 20 +++++++++---------- .../core/io/buffer/DataBufferFactory.java | 5 +++-- .../core/io/buffer/DataBufferUtils.java | 4 +--- .../core/io/buffer/DefaultDataBuffer.java | 5 +---- .../io/buffer/DefaultDataBufferFactory.java | 19 +++++++----------- .../core/io/buffer/NettyDataBuffer.java | 4 ++-- .../io/buffer/NettyDataBufferFactory.java | 4 +++- .../core/io/buffer/PooledDataBuffer.java | 14 +++++++------ 8 files changed, 35 insertions(+), 40 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffer.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffer.java index 052be52e15..6818c1be1a 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffer.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBuffer.java @@ -25,10 +25,10 @@ import java.util.function.IntPredicate; * Basic abstraction over byte buffers. * *

{@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: * *

* 0 <= @@ -41,8 +41,8 @@ import java.util.function.IntPredicate; * similar to {@code StringBuilder}. * *

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 - * not be {@linkplain DataBufferUtils#release(DataBuffer) released} when the - * input stream is {@linkplain InputStream#close() closed}. + * not 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) */ diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferFactory.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferFactory.java index b13df2e2f3..fe257ca022 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferFactory.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferFactory.java @@ -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 dataBuffers); + } diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferUtils.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferUtils.java index 848334a78d..d19e166d9a 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferUtils.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferUtils.java @@ -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 write( - Publisher source, AsynchronousFileChannel channel) { + public static Flux write(Publisher source, AsynchronousFileChannel channel) { return write(source, channel, 0); } - /** * Write the given stream of {@link DataBuffer DataBuffers} to the given {@code AsynchronousFileChannel}. * Does not close the channel when the flux is terminated, and does diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java index eb511f4b3f..37fe545843 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBuffer.java @@ -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; } diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBufferFactory.java b/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBufferFactory.java index 0525cee6d0..c384c0a6ee 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBufferFactory.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/DefaultDataBufferFactory.java @@ -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} - *

This implementation creates a single {@link DefaultDataBuffer} to contain the data - * in {@code dataBuffers}. + *

This implementation creates a single {@link DefaultDataBuffer} + * to contain the data in {@code dataBuffers}. */ @Override public DefaultDataBuffer join(List 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 + ")"; diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java b/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java index 2fc9d43ddc..5f7cc8ac8c 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java @@ -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))); } diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBufferFactory.java b/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBufferFactory.java index 10272d8db7..fcff2f340a 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBufferFactory.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBufferFactory.java @@ -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 + ")"; } + } diff --git a/spring-core/src/main/java/org/springframework/core/io/buffer/PooledDataBuffer.java b/spring-core/src/main/java/org/springframework/core/io/buffer/PooledDataBuffer.java index 4c83ec37f6..1386167f54 100644 --- a/spring-core/src/main/java/org/springframework/core/io/buffer/PooledDataBuffer.java +++ b/spring-core/src/main/java/org/springframework/core/io/buffer/PooledDataBuffer.java @@ -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();