Skip to content

Commit a9b453d

Browse files
committed
Polishing
1 parent 693ad3c commit a9b453d

File tree

14 files changed

+75
-80
lines changed

14 files changed

+75
-80
lines changed

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
* Basic abstraction over byte buffers.
2626
*
2727
* <p>{@code DataBuffer}s has a separate {@linkplain #readPosition() read} and
28-
* {@linkplain #writePosition() write} position, as opposed to {@code ByteBuffer}'s single
29-
* {@linkplain ByteBuffer#position() position}. As such, the {@code DataBuffer} does not require
30-
* a {@linkplain ByteBuffer#flip() flip} to read after writing. In general, the following invariant
31-
* holds for the read and write positions, and the capacity:
28+
* {@linkplain #writePosition() write} position, as opposed to {@code ByteBuffer}'s
29+
* single {@linkplain ByteBuffer#position() position}. As such, the {@code DataBuffer}
30+
* does not require a {@linkplain ByteBuffer#flip() flip} to read after writing. In general,
31+
* the following invariant holds for the read and write positions, and the capacity:
3232
*
3333
* <blockquote>
3434
* <tt>0</tt> <tt>&lt;=</tt>
@@ -41,8 +41,8 @@
4141
* similar to {@code StringBuilder}.
4242
*
4343
* <p>The main purpose of the {@code DataBuffer} abstraction is to provide a convenient wrapper
44-
* around {@link ByteBuffer} that is similar to Netty's {@link io.netty.buffer.ByteBuf}, but that
45-
* can also be used on non-Netty platforms (i.e. Servlet).
44+
* around {@link ByteBuffer} which is similar to Netty's {@link io.netty.buffer.ByteBuf} but
45+
* can also be used on non-Netty platforms (i.e. Servlet containers).
4646
*
4747
* @author Arjen Poutsma
4848
* @since 5.0
@@ -236,8 +236,8 @@ public interface DataBuffer {
236236
ByteBuffer asByteBuffer();
237237

238238
/**
239-
* Expose a subsequence of this buffer's bytes as a {@link ByteBuffer}. Data between this
240-
* {@code DataBuffer} and the returned {@code ByteBuffer} is shared; though
239+
* Expose a subsequence of this buffer's bytes as a {@link ByteBuffer}. Data between
240+
* this {@code DataBuffer} and the returned {@code ByteBuffer} is shared; though
241241
* changes in the returned buffer's {@linkplain ByteBuffer#position() position}
242242
* will not be reflected in the reading nor writing position of this data buffer.
243243
* @param index the index at which to start the byte buffer
@@ -250,8 +250,8 @@ public interface DataBuffer {
250250
/**
251251
* Expose this buffer's data as an {@link InputStream}. Both data and read position are
252252
* shared between the returned stream and this data buffer. The underlying buffer will
253-
* <strong>not</strong> be {@linkplain DataBufferUtils#release(DataBuffer) released} when the
254-
* input stream is {@linkplain InputStream#close() closed}.
253+
* <strong>not</strong> be {@linkplain DataBufferUtils#release(DataBuffer) released}
254+
* when the input stream is {@linkplain InputStream#close() closed}.
255255
* @return this data buffer as an input stream
256256
* @see #asInputStream(boolean)
257257
*/

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import java.util.List;
2121

2222
/**
23-
* A factory for {@link DataBuffer}s, allowing for allocation and wrapping of
24-
* data buffers.
23+
* A factory for {@link DataBuffer DataBuffers}, allowing for allocation and
24+
* wrapping of data buffers.
2525
*
2626
* @author Arjen Poutsma
2727
* @since 5.0
@@ -74,4 +74,5 @@ public interface DataBufferFactory {
7474
* @since 5.0.3
7575
*/
7676
DataBuffer join(List<? extends DataBuffer> dataBuffers);
77+
7778
}

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,10 @@ public static Flux<DataBuffer> write(Publisher<DataBuffer> source, WritableByteC
359359
* process when subscribed to, and that publishes any writing errors and the completion signal
360360
* @since 5.0.10
361361
*/
362-
public static Flux<DataBuffer> write(
363-
Publisher<DataBuffer> source, AsynchronousFileChannel channel) {
362+
public static Flux<DataBuffer> write(Publisher<DataBuffer> source, AsynchronousFileChannel channel) {
364363
return write(source, channel, 0);
365364
}
366365

367-
368366
/**
369367
* Write the given stream of {@link DataBuffer DataBuffers} to the given {@code AsynchronousFileChannel}.
370368
* Does <strong>not</strong> close the channel when the flux is terminated, and does

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,7 @@ public DefaultDataBuffer write(byte[] source, int offset, int length) {
294294
@Override
295295
public DefaultDataBuffer write(DataBuffer... buffers) {
296296
if (!ObjectUtils.isEmpty(buffers)) {
297-
ByteBuffer[] byteBuffers =
298-
Arrays.stream(buffers).map(DataBuffer::asByteBuffer)
299-
.toArray(ByteBuffer[]::new);
300-
write(byteBuffers);
297+
write(Arrays.stream(buffers).map(DataBuffer::asByteBuffer).toArray(ByteBuffer[]::new));
301298
}
302299
return this;
303300
}
@@ -381,6 +378,7 @@ private void ensureCapacity(int length) {
381378
}
382379

383380
/**
381+
* Calculate the capacity of the buffer.
384382
* @see io.netty.buffer.AbstractByteBufAllocator#calculateNewCapacity(int, int)
385383
*/
386384
private int calculateCapacity(int neededCapacity) {
@@ -430,7 +428,7 @@ public int hashCode() {
430428

431429
@Override
432430
public String toString() {
433-
return String.format("DefaultDataBuffer (r: %d, w %d, c %d)",
431+
return String.format("DefaultDataBuffer (r: %d, w: %d, c: %d)",
434432
this.readPosition, this.writePosition, this.capacity);
435433
}
436434

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

+7-12
Original file line numberDiff line numberDiff line change
@@ -87,34 +87,28 @@ public DefaultDataBuffer allocateBuffer(int initialCapacity) {
8787
ByteBuffer byteBuffer = (this.preferDirect ?
8888
ByteBuffer.allocateDirect(initialCapacity) :
8989
ByteBuffer.allocate(initialCapacity));
90-
9190
return DefaultDataBuffer.fromEmptyByteBuffer(this, byteBuffer);
9291
}
9392

9493
@Override
9594
public DefaultDataBuffer wrap(ByteBuffer byteBuffer) {
96-
ByteBuffer sliced = byteBuffer.slice();
97-
return DefaultDataBuffer.fromFilledByteBuffer(this, sliced);
95+
return DefaultDataBuffer.fromFilledByteBuffer(this, byteBuffer.slice());
9896
}
9997

10098
@Override
10199
public DataBuffer wrap(byte[] bytes) {
102-
ByteBuffer wrapper = ByteBuffer.wrap(bytes);
103-
return DefaultDataBuffer.fromFilledByteBuffer(this, wrapper);
100+
return DefaultDataBuffer.fromFilledByteBuffer(this, ByteBuffer.wrap(bytes));
104101
}
105102

106103
/**
107104
* {@inheritDoc}
108-
* <p>This implementation creates a single {@link DefaultDataBuffer} to contain the data
109-
* in {@code dataBuffers}.
105+
* <p>This implementation creates a single {@link DefaultDataBuffer}
106+
* to contain the data in {@code dataBuffers}.
110107
*/
111108
@Override
112109
public DataBuffer join(List<? extends DataBuffer> dataBuffers) {
113-
Assert.notEmpty(dataBuffers, "'dataBuffers' must not be empty");
114-
115-
int capacity = dataBuffers.stream()
116-
.mapToInt(DataBuffer::readableByteCount)
117-
.sum();
110+
Assert.notEmpty(dataBuffers, "DataBuffer List must not be empty");
111+
int capacity = dataBuffers.stream().mapToInt(DataBuffer::readableByteCount).sum();
118112
DefaultDataBuffer dataBuffer = allocateBuffer(capacity);
119113
DataBuffer result = dataBuffers.stream()
120114
.map(o -> (DataBuffer) o)
@@ -123,6 +117,7 @@ public DataBuffer join(List<? extends DataBuffer> dataBuffers) {
123117
return result;
124118
}
125119

120+
126121
@Override
127122
public String toString() {
128123
return "DefaultDataBufferFactory (preferDirect=" + this.preferDirect + ")";

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

+19-20
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.netty.buffer.ByteBufOutputStream;
2828

2929
import org.springframework.util.Assert;
30+
import org.springframework.util.ObjectUtils;
3031

3132
/**
3233
* Implementation of the {@code DataBuffer} interface that wraps a Netty
@@ -43,7 +44,7 @@ public class NettyDataBuffer implements PooledDataBuffer {
4344

4445

4546
/**
46-
* Creates a new {@code NettyDataBuffer} based on the given {@code ByteBuff}.
47+
* Create a new {@code NettyDataBuffer} based on the given {@code ByteBuff}.
4748
* @param byteBuf the buffer to base this buffer on
4849
*/
4950
NettyDataBuffer(ByteBuf byteBuf, NettyDataBufferFactory dataBufferFactory) {
@@ -69,7 +70,7 @@ public NettyDataBufferFactory factory() {
6970

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

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

176177
@Override
177178
public NettyDataBuffer write(DataBuffer... buffers) {
178-
Assert.notNull(buffers, "'buffers' must not be null");
179-
180-
if (buffers.length > 0) {
179+
if (!ObjectUtils.isEmpty(buffers)) {
181180
if (hasNettyDataBuffers(buffers)) {
182181
ByteBuf[] nativeBuffers = Arrays.stream(buffers)
183182
.map(b -> ((NettyDataBuffer) b).getNativeBuffer())
@@ -194,9 +193,9 @@ public NettyDataBuffer write(DataBuffer... buffers) {
194193
return this;
195194
}
196195

197-
private static boolean hasNettyDataBuffers(DataBuffer[] dataBuffers) {
198-
for (DataBuffer dataBuffer : dataBuffers) {
199-
if (!(dataBuffer instanceof NettyDataBuffer)) {
196+
private static boolean hasNettyDataBuffers(DataBuffer[] buffers) {
197+
for (DataBuffer buffer : buffers) {
198+
if (!(buffer instanceof NettyDataBuffer)) {
200199
return false;
201200
}
202201
}
@@ -205,25 +204,25 @@ private static boolean hasNettyDataBuffers(DataBuffer[] dataBuffers) {
205204

206205
@Override
207206
public NettyDataBuffer write(ByteBuffer... buffers) {
208-
Assert.notNull(buffers, "'buffers' must not be null");
209-
210-
for (ByteBuffer buffer : buffers) {
211-
this.byteBuf.writeBytes(buffer);
207+
if (!ObjectUtils.isEmpty(buffers)) {
208+
for (ByteBuffer buffer : buffers) {
209+
this.byteBuf.writeBytes(buffer);
210+
}
212211
}
213212
return this;
214213
}
215214

216215
/**
217-
* Writes one or more Netty {@link ByteBuf}s to this buffer, starting at the current
218-
* writing position.
216+
* Writes one or more Netty {@link ByteBuf}s to this buffer,
217+
* starting at the current writing position.
219218
* @param byteBufs the buffers to write into this buffer
220219
* @return this buffer
221220
*/
222221
public NettyDataBuffer write(ByteBuf... byteBufs) {
223-
Assert.notNull(byteBufs, "'byteBufs' must not be null");
224-
225-
for (ByteBuf byteBuf : byteBufs) {
226-
this.byteBuf.writeBytes(byteBuf);
222+
if (!ObjectUtils.isEmpty(byteBufs)) {
223+
for (ByteBuf byteBuf : byteBufs) {
224+
this.byteBuf.writeBytes(byteBuf);
225+
}
227226
}
228227
return this;
229228
}
@@ -272,7 +271,7 @@ public boolean release() {
272271

273272
@Override
274273
public boolean equals(Object other) {
275-
return (this == other || (other instanceof NettyDataBuffer &&
274+
return (this == other || (other instanceof NettyDataBuffer &&
276275
this.byteBuf.equals(((NettyDataBuffer) other).byteBuf)));
277276
}
278277

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class NettyDataBufferFactory implements DataBufferFactory {
4242

4343

4444
/**
45-
* Creates a new {@code NettyDataBufferFactory} based on the given factory.
45+
* Create a new {@code NettyDataBufferFactory} based on the given factory.
4646
* @param byteBufAllocator the factory to use
4747
* @see io.netty.buffer.PooledByteBufAllocator
4848
* @see io.netty.buffer.UnpooledByteBufAllocator
@@ -52,6 +52,7 @@ public NettyDataBufferFactory(ByteBufAllocator byteBufAllocator) {
5252
this.byteBufAllocator = byteBufAllocator;
5353
}
5454

55+
5556
/**
5657
* Return the {@code ByteBufAllocator} used by this factory.
5758
*/
@@ -133,4 +134,5 @@ public static ByteBuf toByteBuf(DataBuffer buffer) {
133134
public String toString() {
134135
return "NettyDataBufferFactory (" + this.byteBufAllocator + ")";
135136
}
137+
136138
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@ public interface PooledDataBuffer extends DataBuffer {
3434
/**
3535
* Decrease the reference count for this buffer by one, and release it
3636
* once the count reaches zero.
37-
* @return {@code true} if the buffer was released; {@code false} otherwise.
37+
* @return {@code true} if the buffer was released; {@code false} otherwise
3838
*/
3939
boolean release();
4040

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

+13-16
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public List<MediaType> getAccept() {
449449
* @since 5.0
450450
*/
451451
public void setAcceptLanguage(List<Locale.LanguageRange> languages) {
452-
Assert.notNull(languages, "'languages' must not be null");
452+
Assert.notNull(languages, "LanguageRange List must not be null");
453453
DecimalFormat decimal = new DecimalFormat("0.0", DECIMAL_FORMAT_SYMBOLS);
454454
List<String> values = languages.stream()
455455
.map(range ->
@@ -762,7 +762,7 @@ public List<String> getConnection() {
762762
* @see #getContentDisposition()
763763
*/
764764
public void setContentDispositionFormData(String name, @Nullable String filename) {
765-
Assert.notNull(name, "'name' must not be null");
765+
Assert.notNull(name, "Name must not be null");
766766
ContentDisposition.Builder disposition = ContentDisposition.builder("form-data").name(name);
767767
if (filename != null) {
768768
disposition.filename(filename);
@@ -849,8 +849,8 @@ public long getContentLength() {
849849
*/
850850
public void setContentType(@Nullable MediaType mediaType) {
851851
if (mediaType != null) {
852-
Assert.isTrue(!mediaType.isWildcardType(), "'Content-Type' cannot contain wildcard type '*'");
853-
Assert.isTrue(!mediaType.isWildcardSubtype(), "'Content-Type' cannot contain wildcard subtype '*'");
852+
Assert.isTrue(!mediaType.isWildcardType(), "Content-Type cannot contain wildcard type '*'");
853+
Assert.isTrue(!mediaType.isWildcardSubtype(), "Content-Type cannot contain wildcard subtype '*'");
854854
set(CONTENT_TYPE, mediaType.toString());
855855
}
856856
else {
@@ -1222,13 +1222,6 @@ public void setDate(String headerName, long date) {
12221222
set(headerName, formatDate(date));
12231223
}
12241224

1225-
// Package private: also used in ResponseCookie..
1226-
static String formatDate(long date) {
1227-
Instant instant = Instant.ofEpochMilli(date);
1228-
ZonedDateTime time = ZonedDateTime.ofInstant(instant, GMT);
1229-
return DATE_FORMATTERS[0].format(time);
1230-
}
1231-
12321225
/**
12331226
* Parse the first header value for the given header name as a date,
12341227
* return -1 if there is no value, or raise {@link IllegalArgumentException}
@@ -1330,10 +1323,7 @@ public List<String> getValuesAsList(String headerName) {
13301323
List<String> result = new ArrayList<>();
13311324
for (String value : values) {
13321325
if (value != null) {
1333-
String[] tokens = StringUtils.tokenizeToStringArray(value, ",");
1334-
for (String token : tokens) {
1335-
result.add(token);
1336-
}
1326+
Collections.addAll(result, StringUtils.tokenizeToStringArray(value, ","));
13371327
}
13381328
}
13391329
return result;
@@ -1392,7 +1382,7 @@ protected String getFieldValues(String headerName) {
13921382
*/
13931383
protected String toCommaDelimitedString(List<String> headerValues) {
13941384
StringBuilder builder = new StringBuilder();
1395-
for (Iterator<String> it = headerValues.iterator(); it.hasNext(); ) {
1385+
for (Iterator<String> it = headerValues.iterator(); it.hasNext();) {
13961386
String val = it.next();
13971387
builder.append(val);
13981388
if (it.hasNext()) {
@@ -1565,4 +1555,11 @@ public static HttpHeaders readOnlyHttpHeaders(HttpHeaders headers) {
15651555
return (headers.readOnly ? headers : new HttpHeaders(headers, true));
15661556
}
15671557

1558+
// Package-private: used in ResponseCookie
1559+
static String formatDate(long date) {
1560+
Instant instant = Instant.ofEpochMilli(date);
1561+
ZonedDateTime time = ZonedDateTime.ofInstant(instant, GMT);
1562+
return DATE_FORMATTERS[0].format(time);
1563+
}
1564+
15681565
}

0 commit comments

Comments
 (0)