Skip to content

Commit 6eb0a60

Browse files
committed
Polishing
1 parent 5bbbc82 commit 6eb0a60

File tree

3 files changed

+45
-40
lines changed

3 files changed

+45
-40
lines changed

spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferUtils.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ public static Flux<DataBuffer> readAsynchronousFileChannel(Callable<Asynchronous
139139
DataBuffer dataBuffer = dataBufferFactory.allocateBuffer(bufferSize);
140140
ByteBuffer byteBuffer = dataBuffer.asByteBuffer(0, bufferSize);
141141

142-
143142
Flux<DataBuffer> result = Flux.using(channelSupplier,
144143
channel -> Flux.create(sink -> {
145144
AsynchronousFileChannelReadCompletionHandler completionHandler =
@@ -265,7 +264,7 @@ public static Flux<DataBuffer> write(Publisher<DataBuffer> source, WritableByteC
265264
* @param channel the channel to write to
266265
* @return a flux containing the same buffers as in {@code source}, that starts the writing
267266
* process when subscribed to, and that publishes any writing errors and the completion signal
268-
* @since 5.1
267+
* @since 5.0.10
269268
*/
270269
public static Flux<DataBuffer> write(Publisher<DataBuffer> source, AsynchronousFileChannel channel) {
271270
return write(source, channel, 0);
@@ -329,7 +328,6 @@ public static Flux<DataBuffer> takeUntilByteCount(Publisher<DataBuffer> publishe
329328

330329
return Flux.defer(() -> {
331330
AtomicLong countDown = new AtomicLong(maxByteCount);
332-
333331
return Flux.from(publisher)
334332
.map(buffer -> {
335333
long remainder = countDown.addAndGet(-buffer.readableByteCount());
@@ -359,7 +357,6 @@ public static Flux<DataBuffer> skipUntilByteCount(Publisher<DataBuffer> publishe
359357

360358
return Flux.defer(() -> {
361359
AtomicLong countDown = new AtomicLong(maxByteCount);
362-
363360
return Flux.from(publisher)
364361
.skipUntil(buffer -> {
365362
long remainder = countDown.addAndGet(-buffer.readableByteCount());

spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.netty.buffer.ByteBufOutputStream;
2727

2828
import org.springframework.util.Assert;
29+
import org.springframework.util.ObjectUtils;
2930

3031
/**
3132
* Implementation of the {@code DataBuffer} interface that wraps a Netty
@@ -68,7 +69,7 @@ public NettyDataBufferFactory factory() {
6869

6970
@Override
7071
public int indexOf(IntPredicate predicate, int fromIndex) {
71-
Assert.notNull(predicate, "'predicate' must not be null");
72+
Assert.notNull(predicate, "IntPredicate must not be null");
7273
if (fromIndex < 0) {
7374
fromIndex = 0;
7475
}
@@ -81,7 +82,7 @@ else if (fromIndex >= this.byteBuf.writerIndex()) {
8182

8283
@Override
8384
public int lastIndexOf(IntPredicate predicate, int fromIndex) {
84-
Assert.notNull(predicate, "'predicate' must not be null");
85+
Assert.notNull(predicate, "IntPredicate must not be null");
8586
if (fromIndex < 0) {
8687
return -1;
8788
}
@@ -174,9 +175,7 @@ public NettyDataBuffer write(byte[] source, int offset, int length) {
174175

175176
@Override
176177
public NettyDataBuffer write(DataBuffer... buffers) {
177-
Assert.notNull(buffers, "'buffers' must not be null");
178-
179-
if (buffers.length > 0) {
178+
if (!ObjectUtils.isEmpty(buffers)) {
180179
if (hasNettyDataBuffers(buffers)) {
181180
ByteBuf[] nativeBuffers = new ByteBuf[buffers.length];
182181
for (int i = 0 ; i < buffers.length; i++) {
@@ -196,9 +195,9 @@ public NettyDataBuffer write(DataBuffer... buffers) {
196195
return this;
197196
}
198197

199-
private static boolean hasNettyDataBuffers(DataBuffer[] dataBuffers) {
200-
for (DataBuffer dataBuffer : dataBuffers) {
201-
if (!(dataBuffer instanceof NettyDataBuffer)) {
198+
private static boolean hasNettyDataBuffers(DataBuffer[] buffers) {
199+
for (DataBuffer buffer : buffers) {
200+
if (!(buffer instanceof NettyDataBuffer)) {
202201
return false;
203202
}
204203
}
@@ -207,25 +206,25 @@ private static boolean hasNettyDataBuffers(DataBuffer[] dataBuffers) {
207206

208207
@Override
209208
public NettyDataBuffer write(ByteBuffer... buffers) {
210-
Assert.notNull(buffers, "'buffers' must not be null");
211-
212-
for (ByteBuffer buffer : buffers) {
213-
this.byteBuf.writeBytes(buffer);
209+
if (!ObjectUtils.isEmpty(buffers)) {
210+
for (ByteBuffer buffer : buffers) {
211+
this.byteBuf.writeBytes(buffer);
212+
}
214213
}
215214
return this;
216215
}
217216

218217
/**
219-
* Writes one or more Netty {@link ByteBuf ByteBufs} to this buffer, starting at the current
220-
* writing position.
218+
* Writes one or more Netty {@link ByteBuf ByteBufs} to this buffer,
219+
* starting at the current writing position.
221220
* @param byteBufs the buffers to write into this buffer
222221
* @return this buffer
223222
*/
224223
public NettyDataBuffer write(ByteBuf... byteBufs) {
225-
Assert.notNull(byteBufs, "'byteBufs' must not be null");
226-
227-
for (ByteBuf byteBuf : byteBufs) {
228-
this.byteBuf.writeBytes(byteBuf);
224+
if (!ObjectUtils.isEmpty(byteBufs)) {
225+
for (ByteBuf byteBuf : byteBufs) {
226+
this.byteBuf.writeBytes(byteBuf);
227+
}
229228
}
230229
return this;
231230
}

spring-web/src/main/java/org/springframework/http/CacheControl.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -253,48 +253,55 @@ public CacheControl staleIfError(long staleIfError, TimeUnit unit) {
253253

254254

255255
/**
256-
* Return the "Cache-Control" header value.
257-
* @return {@code null} if no directive was added, or the header value otherwise
256+
* Return the "Cache-Control" header value, if any.
257+
* @return the header value, or {@code null} if no directive was added
258258
*/
259259
@Nullable
260260
public String getHeaderValue() {
261-
StringBuilder ccValue = new StringBuilder();
261+
String headerValue = toHeaderValue();
262+
return (StringUtils.hasText(headerValue) ? headerValue : null);
263+
}
264+
265+
/**
266+
* Return the "Cache-Control" header value.
267+
* @return the header value (potentially empty)
268+
*/
269+
private String toHeaderValue() {
270+
StringBuilder headerValue = new StringBuilder();
262271
if (this.maxAge != -1) {
263-
appendDirective(ccValue, "max-age=" + Long.toString(this.maxAge));
272+
appendDirective(headerValue, "max-age=" + this.maxAge);
264273
}
265274
if (this.noCache) {
266-
appendDirective(ccValue, "no-cache");
275+
appendDirective(headerValue, "no-cache");
267276
}
268277
if (this.noStore) {
269-
appendDirective(ccValue, "no-store");
278+
appendDirective(headerValue, "no-store");
270279
}
271280
if (this.mustRevalidate) {
272-
appendDirective(ccValue, "must-revalidate");
281+
appendDirective(headerValue, "must-revalidate");
273282
}
274283
if (this.noTransform) {
275-
appendDirective(ccValue, "no-transform");
284+
appendDirective(headerValue, "no-transform");
276285
}
277286
if (this.cachePublic) {
278-
appendDirective(ccValue, "public");
287+
appendDirective(headerValue, "public");
279288
}
280289
if (this.cachePrivate) {
281-
appendDirective(ccValue, "private");
290+
appendDirective(headerValue, "private");
282291
}
283292
if (this.proxyRevalidate) {
284-
appendDirective(ccValue, "proxy-revalidate");
293+
appendDirective(headerValue, "proxy-revalidate");
285294
}
286295
if (this.sMaxAge != -1) {
287-
appendDirective(ccValue, "s-maxage=" + Long.toString(this.sMaxAge));
296+
appendDirective(headerValue, "s-maxage=" + this.sMaxAge);
288297
}
289298
if (this.staleIfError != -1) {
290-
appendDirective(ccValue, "stale-if-error=" + Long.toString(this.staleIfError));
299+
appendDirective(headerValue, "stale-if-error=" + this.staleIfError);
291300
}
292301
if (this.staleWhileRevalidate != -1) {
293-
appendDirective(ccValue, "stale-while-revalidate=" + Long.toString(this.staleWhileRevalidate));
302+
appendDirective(headerValue, "stale-while-revalidate=" + this.staleWhileRevalidate);
294303
}
295-
296-
String ccHeaderValue = ccValue.toString();
297-
return (StringUtils.hasText(ccHeaderValue) ? ccHeaderValue : null);
304+
return headerValue.toString();
298305
}
299306

300307
private void appendDirective(StringBuilder builder, String value) {
@@ -304,8 +311,10 @@ private void appendDirective(StringBuilder builder, String value) {
304311
builder.append(value);
305312
}
306313

314+
307315
@Override
308316
public String toString() {
309-
return "CacheControl [" + getHeaderValue() + "]";
317+
return "CacheControl [" + toHeaderValue() + "]";
310318
}
319+
311320
}

0 commit comments

Comments
 (0)