Polishing

This commit is contained in:
Juergen Hoeller
2018-12-11 11:14:23 +01:00
parent 693ad3c1f5
commit a9b453d79f
14 changed files with 75 additions and 80 deletions

View File

@@ -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>&lt;=</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
@@ -236,8 +236,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
@@ -250,8 +250,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)
*/

View File

@@ -20,8 +20,8 @@ import java.nio.ByteBuffer;
import java.util.List;
/**
* A factory for {@link DataBuffer}s, 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);
}

View File

@@ -359,12 +359,10 @@ public abstract class DataBufferUtils {
* process when subscribed to, and that publishes any writing errors and the completion signal
* @since 5.0.10
*/
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

View File

@@ -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;
}
@@ -381,6 +378,7 @@ public class DefaultDataBuffer implements DataBuffer {
}
/**
* Calculate the capacity of the buffer.
* @see io.netty.buffer.AbstractByteBufAllocator#calculateNewCapacity(int, int)
*/
private int calculateCapacity(int neededCapacity) {
@@ -430,7 +428,7 @@ public class DefaultDataBuffer implements DataBuffer {
@Override
public String toString() {
return String.format("DefaultDataBuffer (r: %d, w %d, c %d)",
return String.format("DefaultDataBuffer (r: %d, w: %d, c: %d)",
this.readPosition, this.writePosition, this.capacity);
}

View File

@@ -87,34 +87,28 @@ 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 DataBuffer 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 DataBuffer 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 dataBuffer = allocateBuffer(capacity);
DataBuffer result = dataBuffers.stream()
.map(o -> (DataBuffer) o)
@@ -123,6 +117,7 @@ public class DefaultDataBufferFactory implements DataBufferFactory {
return result;
}
@Override
public String toString() {
return "DefaultDataBufferFactory (preferDirect=" + this.preferDirect + ")";

View File

@@ -27,6 +27,7 @@ import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* Implementation of the {@code DataBuffer} interface that wraps a Netty
@@ -43,7 +44,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) {
@@ -69,7 +70,7 @@ public class NettyDataBuffer implements PooledDataBuffer {
@Override
public int indexOf(IntPredicate predicate, int fromIndex) {
Assert.notNull(predicate, "'predicate' must not be null");
Assert.notNull(predicate, "IntPredicate must not be null");
if (fromIndex < 0) {
fromIndex = 0;
}
@@ -82,7 +83,7 @@ public class NettyDataBuffer implements PooledDataBuffer {
@Override
public int lastIndexOf(IntPredicate predicate, int fromIndex) {
Assert.notNull(predicate, "'predicate' must not be null");
Assert.notNull(predicate, "IntPredicate must not be null");
if (fromIndex < 0) {
return -1;
}
@@ -175,9 +176,7 @@ public class NettyDataBuffer implements PooledDataBuffer {
@Override
public NettyDataBuffer write(DataBuffer... buffers) {
Assert.notNull(buffers, "'buffers' must not be null");
if (buffers.length > 0) {
if (!ObjectUtils.isEmpty(buffers)) {
if (hasNettyDataBuffers(buffers)) {
ByteBuf[] nativeBuffers = Arrays.stream(buffers)
.map(b -> ((NettyDataBuffer) b).getNativeBuffer())
@@ -194,9 +193,9 @@ public class NettyDataBuffer implements PooledDataBuffer {
return this;
}
private static boolean hasNettyDataBuffers(DataBuffer[] dataBuffers) {
for (DataBuffer dataBuffer : dataBuffers) {
if (!(dataBuffer instanceof NettyDataBuffer)) {
private static boolean hasNettyDataBuffers(DataBuffer[] buffers) {
for (DataBuffer buffer : buffers) {
if (!(buffer instanceof NettyDataBuffer)) {
return false;
}
}
@@ -205,25 +204,25 @@ public class NettyDataBuffer implements PooledDataBuffer {
@Override
public NettyDataBuffer write(ByteBuffer... buffers) {
Assert.notNull(buffers, "'buffers' must not be null");
for (ByteBuffer buffer : buffers) {
this.byteBuf.writeBytes(buffer);
if (!ObjectUtils.isEmpty(buffers)) {
for (ByteBuffer buffer : buffers) {
this.byteBuf.writeBytes(buffer);
}
}
return this;
}
/**
* Writes one or more Netty {@link ByteBuf}s to this buffer, starting at the current
* writing position.
* Writes one or more Netty {@link ByteBuf}s to this buffer,
* starting at the current writing position.
* @param byteBufs the buffers to write into this buffer
* @return this buffer
*/
public NettyDataBuffer write(ByteBuf... byteBufs) {
Assert.notNull(byteBufs, "'byteBufs' must not be null");
for (ByteBuf byteBuf : byteBufs) {
this.byteBuf.writeBytes(byteBuf);
if (!ObjectUtils.isEmpty(byteBufs)) {
for (ByteBuf byteBuf : byteBufs) {
this.byteBuf.writeBytes(byteBuf);
}
}
return this;
}
@@ -272,7 +271,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)));
}

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
* @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 + ")";
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@ public interface PooledDataBuffer extends DataBuffer {
/**
* Decrease the reference count for this buffer by one, and release it
* once the count reaches zero.
* @return {@code true} if the buffer was released; {@code false} otherwise.
* @return {@code true} if the buffer was released; {@code false} otherwise
*/
boolean release();