Polishing

This commit is contained in:
Juergen Hoeller
2018-12-12 11:16:53 +01:00
parent 5bbbc82e19
commit 6eb0a60df9
3 changed files with 45 additions and 40 deletions

View File

@@ -139,7 +139,6 @@ public abstract class DataBufferUtils {
DataBuffer dataBuffer = dataBufferFactory.allocateBuffer(bufferSize);
ByteBuffer byteBuffer = dataBuffer.asByteBuffer(0, bufferSize);
Flux<DataBuffer> result = Flux.using(channelSupplier,
channel -> Flux.create(sink -> {
AsynchronousFileChannelReadCompletionHandler completionHandler =
@@ -265,7 +264,7 @@ public abstract class DataBufferUtils {
* @param channel the channel to write to
* @return a flux containing the same buffers as in {@code source}, that starts the writing
* process when subscribed to, and that publishes any writing errors and the completion signal
* @since 5.1
* @since 5.0.10
*/
public static Flux<DataBuffer> write(Publisher<DataBuffer> source, AsynchronousFileChannel channel) {
return write(source, channel, 0);
@@ -329,7 +328,6 @@ public abstract class DataBufferUtils {
return Flux.defer(() -> {
AtomicLong countDown = new AtomicLong(maxByteCount);
return Flux.from(publisher)
.map(buffer -> {
long remainder = countDown.addAndGet(-buffer.readableByteCount());
@@ -359,7 +357,6 @@ public abstract class DataBufferUtils {
return Flux.defer(() -> {
AtomicLong countDown = new AtomicLong(maxByteCount);
return Flux.from(publisher)
.skipUntil(buffer -> {
long remainder = countDown.addAndGet(-buffer.readableByteCount());

View File

@@ -26,6 +26,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
@@ -68,7 +69,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;
}
@@ -81,7 +82,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;
}
@@ -174,9 +175,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 = new ByteBuf[buffers.length];
for (int i = 0 ; i < buffers.length; i++) {
@@ -196,9 +195,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;
}
}
@@ -207,25 +206,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 ByteBufs} to this buffer, starting at the current
* writing position.
* Writes one or more Netty {@link ByteBuf ByteBufs} 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;
}

View File

@@ -253,48 +253,55 @@ public class CacheControl {
/**
* Return the "Cache-Control" header value.
* @return {@code null} if no directive was added, or the header value otherwise
* Return the "Cache-Control" header value, if any.
* @return the header value, or {@code null} if no directive was added
*/
@Nullable
public String getHeaderValue() {
StringBuilder ccValue = new StringBuilder();
String headerValue = toHeaderValue();
return (StringUtils.hasText(headerValue) ? headerValue : null);
}
/**
* Return the "Cache-Control" header value.
* @return the header value (potentially empty)
*/
private String toHeaderValue() {
StringBuilder headerValue = new StringBuilder();
if (this.maxAge != -1) {
appendDirective(ccValue, "max-age=" + Long.toString(this.maxAge));
appendDirective(headerValue, "max-age=" + this.maxAge);
}
if (this.noCache) {
appendDirective(ccValue, "no-cache");
appendDirective(headerValue, "no-cache");
}
if (this.noStore) {
appendDirective(ccValue, "no-store");
appendDirective(headerValue, "no-store");
}
if (this.mustRevalidate) {
appendDirective(ccValue, "must-revalidate");
appendDirective(headerValue, "must-revalidate");
}
if (this.noTransform) {
appendDirective(ccValue, "no-transform");
appendDirective(headerValue, "no-transform");
}
if (this.cachePublic) {
appendDirective(ccValue, "public");
appendDirective(headerValue, "public");
}
if (this.cachePrivate) {
appendDirective(ccValue, "private");
appendDirective(headerValue, "private");
}
if (this.proxyRevalidate) {
appendDirective(ccValue, "proxy-revalidate");
appendDirective(headerValue, "proxy-revalidate");
}
if (this.sMaxAge != -1) {
appendDirective(ccValue, "s-maxage=" + Long.toString(this.sMaxAge));
appendDirective(headerValue, "s-maxage=" + this.sMaxAge);
}
if (this.staleIfError != -1) {
appendDirective(ccValue, "stale-if-error=" + Long.toString(this.staleIfError));
appendDirective(headerValue, "stale-if-error=" + this.staleIfError);
}
if (this.staleWhileRevalidate != -1) {
appendDirective(ccValue, "stale-while-revalidate=" + Long.toString(this.staleWhileRevalidate));
appendDirective(headerValue, "stale-while-revalidate=" + this.staleWhileRevalidate);
}
String ccHeaderValue = ccValue.toString();
return (StringUtils.hasText(ccHeaderValue) ? ccHeaderValue : null);
return headerValue.toString();
}
private void appendDirective(StringBuilder builder, String value) {
@@ -304,8 +311,10 @@ public class CacheControl {
builder.append(value);
}
@Override
public String toString() {
return "CacheControl [" + getHeaderValue() + "]";
return "CacheControl [" + toHeaderValue() + "]";
}
}