From 6eb0a60df99e56d9fa66d387d6c03a275722770e Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 12 Dec 2018 11:16:53 +0100 Subject: [PATCH] Polishing --- .../core/io/buffer/DataBufferUtils.java | 5 +-- .../core/io/buffer/NettyDataBuffer.java | 35 +++++++-------- .../springframework/http/CacheControl.java | 45 +++++++++++-------- 3 files changed, 45 insertions(+), 40 deletions(-) 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 d19e166d9a..5ecb84bcd6 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 @@ -139,7 +139,6 @@ public abstract class DataBufferUtils { DataBuffer dataBuffer = dataBufferFactory.allocateBuffer(bufferSize); ByteBuffer byteBuffer = dataBuffer.asByteBuffer(0, bufferSize); - Flux 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 write(Publisher 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()); 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 5f7cc8ac8c..798a3f397c 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 @@ -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; } diff --git a/spring-web/src/main/java/org/springframework/http/CacheControl.java b/spring-web/src/main/java/org/springframework/http/CacheControl.java index 5cf9873190..58437c4f11 100644 --- a/spring-web/src/main/java/org/springframework/http/CacheControl.java +++ b/spring-web/src/main/java/org/springframework/http/CacheControl.java @@ -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() + "]"; } + }