Polishing

This commit is contained in:
Arjen Poutsma 2016-02-02 13:55:07 +01:00
parent 199252cda6
commit a4be950e37
2 changed files with 31 additions and 6 deletions

View File

@ -92,7 +92,7 @@ public interface DataBuffer {
DataBuffer write(byte[] source, int offset, int length); DataBuffer write(byte[] source, int offset, int length);
/** /**
* Writes one or more {@link DataBuffer} to this buffer, starting at the current * Writes one or more {@code DataBuffer}s to this buffer, starting at the current
* writing position. * writing position.
* @param buffers the byte buffers to write into this buffer * @param buffers the byte buffers to write into this buffer
* @return this buffer * @return this buffer

View File

@ -18,18 +18,29 @@ package org.springframework.core.io.buffer;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import org.springframework.util.Assert;
/** /**
* Default implementation of the {@code DataBufferAllocator} interface. * Default implementation of the {@code DataBufferAllocator} interface. Allows for
* specification of the default initial capacity at construction time, as well as whether
* heap-based or direct buffers are to be preferred.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
*/ */
public class DefaultDataBufferAllocator implements DataBufferAllocator { public class DefaultDataBufferAllocator implements DataBufferAllocator {
/**
* The default capacity when none is specified.
* @see #DefaultDataBufferAllocator()
* @see #DefaultDataBufferAllocator(boolean)
*/
public static final int DEFAULT_INITIAL_CAPACITY = 256; public static final int DEFAULT_INITIAL_CAPACITY = 256;
private final boolean preferDirect; private final boolean preferDirect;
private final int defaultInitialCapacity;
/** /**
* Creates a new {@code DefaultDataBufferAllocator} with default settings. * Creates a new {@code DefaultDataBufferAllocator} with default settings.
*/ */
@ -39,17 +50,31 @@ public class DefaultDataBufferAllocator implements DataBufferAllocator {
/** /**
* Creates a new {@code DefaultDataBufferAllocator}, indicating whether direct buffers * Creates a new {@code DefaultDataBufferAllocator}, indicating whether direct buffers
* should be created by {@link #allocateBuffer(int)}. * should be created by {@link #allocateBuffer()} and {@link #allocateBuffer(int)}.
* @param preferDirect {@code true} if direct buffers are to be preferred; {@code * @param preferDirect {@code true} if direct buffers are to be preferred; {@code
* false} otherwise * false} otherwise
*/ */
public DefaultDataBufferAllocator(boolean preferDirect) { public DefaultDataBufferAllocator(boolean preferDirect) {
this(preferDirect, DEFAULT_INITIAL_CAPACITY);
}
/**
* Creates a new {@code DefaultDataBufferAllocator}, indicating whether direct buffers
* should be created by {@link #allocateBuffer()} and {@link #allocateBuffer(int)},
* and what the capacity is to be used for {@link #allocateBuffer()}.
* @param preferDirect {@code true} if direct buffers are to be preferred; {@code
* false} otherwise
*/
public DefaultDataBufferAllocator(boolean preferDirect, int defaultInitialCapacity) {
Assert.isTrue(defaultInitialCapacity > 0,
"'defaultInitialCapacity' should be larger than 0");
this.preferDirect = preferDirect; this.preferDirect = preferDirect;
this.defaultInitialCapacity = defaultInitialCapacity;
} }
@Override @Override
public DataBuffer allocateBuffer() { public DefaultDataBuffer allocateBuffer() {
return allocateBuffer(DEFAULT_INITIAL_CAPACITY); return allocateBuffer(this.defaultInitialCapacity);
} }
@Override @Override
@ -60,7 +85,7 @@ public class DefaultDataBufferAllocator implements DataBufferAllocator {
} }
@Override @Override
public DataBuffer wrap(ByteBuffer byteBuffer) { public DefaultDataBuffer wrap(ByteBuffer byteBuffer) {
ByteBuffer sliced = byteBuffer.slice(); ByteBuffer sliced = byteBuffer.slice();
return new DefaultDataBuffer(sliced, 0, byteBuffer.remaining()); return new DefaultDataBuffer(sliced, 0, byteBuffer.remaining());
} }