From a4be950e372eae9acac64c0baacb3a9274752df7 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 2 Feb 2016 13:55:07 +0100 Subject: [PATCH] Polishing --- .../core/io/buffer/DataBuffer.java | 2 +- .../io/buffer/DefaultDataBufferAllocator.java | 35 ++++++++++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DataBuffer.java b/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DataBuffer.java index 49f5f557e0..591b6c3cc9 100644 --- a/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DataBuffer.java +++ b/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DataBuffer.java @@ -92,7 +92,7 @@ public interface DataBuffer { 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. * @param buffers the byte buffers to write into this buffer * @return this buffer diff --git a/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DefaultDataBufferAllocator.java b/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DefaultDataBufferAllocator.java index 2c414e0de3..a9b2b56729 100644 --- a/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DefaultDataBufferAllocator.java +++ b/spring-web-reactive/src/main/java/org/springframework/core/io/buffer/DefaultDataBufferAllocator.java @@ -18,18 +18,29 @@ package org.springframework.core.io.buffer; 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 */ 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; private final boolean preferDirect; + private final int defaultInitialCapacity; + /** * 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 - * 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 * false} otherwise */ 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.defaultInitialCapacity = defaultInitialCapacity; } @Override - public DataBuffer allocateBuffer() { - return allocateBuffer(DEFAULT_INITIAL_CAPACITY); + public DefaultDataBuffer allocateBuffer() { + return allocateBuffer(this.defaultInitialCapacity); } @Override @@ -60,7 +85,7 @@ public class DefaultDataBufferAllocator implements DataBufferAllocator { } @Override - public DataBuffer wrap(ByteBuffer byteBuffer) { + public DefaultDataBuffer wrap(ByteBuffer byteBuffer) { ByteBuffer sliced = byteBuffer.slice(); return new DefaultDataBuffer(sliced, 0, byteBuffer.remaining()); }