Skip to content

Commit 99662bc

Browse files
committed
Polishing
1 parent 45e520e commit 99662bc

File tree

4 files changed

+65
-93
lines changed

4 files changed

+65
-93
lines changed

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

Lines changed: 53 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public abstract class DataBufferUtils {
5656

5757
private static final Consumer<DataBuffer> RELEASE_CONSUMER = DataBufferUtils::release;
5858

59+
5960
//---------------------------------------------------------------------
6061
// Reading
6162
//---------------------------------------------------------------------
@@ -74,31 +75,31 @@ public abstract class DataBufferUtils {
7475
* {@link #readInputStream(Callable, DataBufferFactory, int)}, to be removed in Spring 5.1
7576
*/
7677
@Deprecated
77-
public static Flux<DataBuffer> read(InputStream inputStream,
78-
DataBufferFactory dataBufferFactory, int bufferSize) {
78+
public static Flux<DataBuffer> read(
79+
InputStream inputStream, DataBufferFactory dataBufferFactory, int bufferSize) {
80+
7981
return readInputStream(() -> inputStream, dataBufferFactory, bufferSize);
8082
}
8183

8284
/**
83-
* Obtain a {@link InputStream} from the given supplier, and read it into a {@code Flux} of
84-
* {@code DataBuffer}s. Closes the input stream when the flux is terminated.
85+
* Obtain a {@link InputStream} from the given supplier, and read it into a {@code Flux}
86+
* of {@code DataBuffer}s. Closes the input stream when the flux is terminated.
8587
* @param inputStreamSupplier the supplier for the input stream to read from
8688
* @param dataBufferFactory the factory to create data buffers with
8789
* @param bufferSize the maximum size of the data buffers
8890
* @return a flux of data buffers read from the given channel
8991
*/
90-
public static Flux<DataBuffer> readInputStream(Callable<InputStream> inputStreamSupplier,
91-
DataBufferFactory dataBufferFactory, int bufferSize) {
92+
public static Flux<DataBuffer> readInputStream(
93+
Callable<InputStream> inputStreamSupplier, DataBufferFactory dataBufferFactory, int bufferSize) {
9294

9395
Assert.notNull(inputStreamSupplier, "'inputStreamSupplier' must not be null");
9496

95-
return readByteChannel(() -> Channels.newChannel(inputStreamSupplier.call()),
96-
dataBufferFactory, bufferSize);
97+
return readByteChannel(() -> Channels.newChannel(inputStreamSupplier.call()), dataBufferFactory, bufferSize);
9798
}
9899

99100
/**
100-
* Read the given {@code ReadableByteChannel} into a <strong>read-once</strong> {@code Flux} of
101-
* {@code DataBuffer}s. Closes the channel when the flux is terminated.
101+
* Read the given {@code ReadableByteChannel} into a <strong>read-once</strong> {@code Flux}
102+
* of {@code DataBuffer}s. Closes the channel when the flux is terminated.
102103
* <p>The resulting {@code Flux} can only be subscribed to once. See
103104
* {@link #readByteChannel(Callable, DataBufferFactory, int)} for a variant that supports
104105
* multiple subscriptions.
@@ -110,8 +111,9 @@ public static Flux<DataBuffer> readInputStream(Callable<InputStream> inputStream
110111
* {@link #readByteChannel(Callable, DataBufferFactory, int)}, to be removed in Spring 5.1
111112
*/
112113
@Deprecated
113-
public static Flux<DataBuffer> read(ReadableByteChannel channel,
114-
DataBufferFactory dataBufferFactory, int bufferSize) {
114+
public static Flux<DataBuffer> read(
115+
ReadableByteChannel channel, DataBufferFactory dataBufferFactory, int bufferSize) {
116+
115117
return readByteChannel(() -> channel, dataBufferFactory, bufferSize);
116118
}
117119

@@ -123,8 +125,8 @@ public static Flux<DataBuffer> read(ReadableByteChannel channel,
123125
* @param bufferSize the maximum size of the data buffers
124126
* @return a flux of data buffers read from the given channel
125127
*/
126-
public static Flux<DataBuffer> readByteChannel(Callable<ReadableByteChannel> channelSupplier,
127-
DataBufferFactory dataBufferFactory, int bufferSize) {
128+
public static Flux<DataBuffer> readByteChannel(
129+
Callable<ReadableByteChannel> channelSupplier, DataBufferFactory dataBufferFactory, int bufferSize) {
128130

129131
Assert.notNull(channelSupplier, "'channelSupplier' must not be null");
130132
Assert.notNull(dataBufferFactory, "'dataBufferFactory' must not be null");
@@ -156,8 +158,9 @@ public static Flux<DataBuffer> readByteChannel(Callable<ReadableByteChannel> cha
156158
* Spring 5.1
157159
*/
158160
@Deprecated
159-
public static Flux<DataBuffer> read(AsynchronousFileChannel channel,
160-
DataBufferFactory dataBufferFactory, int bufferSize) {
161+
public static Flux<DataBuffer> read(
162+
AsynchronousFileChannel channel, DataBufferFactory dataBufferFactory, int bufferSize) {
163+
161164
return readAsynchronousFileChannel(() -> channel, dataBufferFactory, bufferSize);
162165
}
163166

@@ -178,8 +181,9 @@ public static Flux<DataBuffer> read(AsynchronousFileChannel channel,
178181
* in Spring 5.1
179182
*/
180183
@Deprecated
181-
public static Flux<DataBuffer> read(AsynchronousFileChannel channel,
182-
long position, DataBufferFactory dataBufferFactory, int bufferSize) {
184+
public static Flux<DataBuffer> read(
185+
AsynchronousFileChannel channel, long position, DataBufferFactory dataBufferFactory, int bufferSize) {
186+
183187
return readAsynchronousFileChannel(() -> channel, position, dataBufferFactory, bufferSize);
184188
}
185189

@@ -192,24 +196,22 @@ public static Flux<DataBuffer> read(AsynchronousFileChannel channel,
192196
* @return a flux of data buffers read from the given channel
193197
*/
194198
public static Flux<DataBuffer> readAsynchronousFileChannel(
195-
Callable<AsynchronousFileChannel> channelSupplier,
196-
DataBufferFactory dataBufferFactory, int bufferSize) {
199+
Callable<AsynchronousFileChannel> channelSupplier, DataBufferFactory dataBufferFactory, int bufferSize) {
197200

198201
return readAsynchronousFileChannel(channelSupplier, 0, dataBufferFactory, bufferSize);
199202
}
200203

201204
/**
202205
* Obtain a {@code AsynchronousFileChannel} from the given supplier, and read it into a
203-
* {@code Flux} of {@code DataBuffer}s, starting at the given position. Closes the channel when
204-
* the flux is terminated.
206+
* {@code Flux} of {@code DataBuffer}s, starting at the given position. Closes the
207+
* channel when the flux is terminated.
205208
* @param channelSupplier the supplier for the channel to read from
206209
* @param position the position to start reading from
207210
* @param dataBufferFactory the factory to create data buffers with
208211
* @param bufferSize the maximum size of the data buffers
209212
* @return a flux of data buffers read from the given channel
210213
*/
211-
public static Flux<DataBuffer> readAsynchronousFileChannel(
212-
Callable<AsynchronousFileChannel> channelSupplier,
214+
public static Flux<DataBuffer> readAsynchronousFileChannel(Callable<AsynchronousFileChannel> channelSupplier,
213215
long position, DataBufferFactory dataBufferFactory, int bufferSize) {
214216

215217
Assert.notNull(channelSupplier, "'channelSupplier' must not be null");
@@ -242,8 +244,8 @@ public static Flux<DataBuffer> readAsynchronousFileChannel(
242244
* @param bufferSize the maximum size of the data buffers
243245
* @return a flux of data buffers read from the given channel
244246
*/
245-
public static Flux<DataBuffer> read(Resource resource,
246-
DataBufferFactory dataBufferFactory, int bufferSize) {
247+
public static Flux<DataBuffer> read(
248+
Resource resource, DataBufferFactory dataBufferFactory, int bufferSize) {
247249

248250
return read(resource, 0, dataBufferFactory, bufferSize);
249251
}
@@ -262,13 +264,12 @@ public static Flux<DataBuffer> read(Resource resource,
262264
* @param bufferSize the maximum size of the data buffers
263265
* @return a flux of data buffers read from the given channel
264266
*/
265-
public static Flux<DataBuffer> read(Resource resource, long position,
266-
DataBufferFactory dataBufferFactory, int bufferSize) {
267+
public static Flux<DataBuffer> read(
268+
Resource resource, long position, DataBufferFactory dataBufferFactory, int bufferSize) {
267269

268270
try {
269271
if (resource.isFile()) {
270272
File file = resource.getFile();
271-
272273
return readAsynchronousFileChannel(
273274
() -> AsynchronousFileChannel.open(file.toPath(), StandardOpenOption.READ),
274275
position, dataBufferFactory, bufferSize);
@@ -283,8 +284,6 @@ public static Flux<DataBuffer> read(Resource resource, long position,
283284
}
284285

285286

286-
287-
288287
//---------------------------------------------------------------------
289288
// Writing
290289
//---------------------------------------------------------------------
@@ -295,16 +294,13 @@ public static Flux<DataBuffer> read(Resource resource, long position,
295294
* <strong>not</strong> {@linkplain #release(DataBuffer) release} the data buffers in the
296295
* source. If releasing is required, then subscribe to the returned {@code Flux} with a
297296
* {@link #releaseConsumer()}.
298-
* <p>Note that the writing process does not start until the returned {@code Flux} is subscribed
299-
* to.
297+
* <p>Note that the writing process does not start until the returned {@code Flux} is subscribed to.
300298
* @param source the stream of data buffers to be written
301299
* @param outputStream the output stream to write to
302300
* @return a flux containing the same buffers as in {@code source}, that starts the writing
303301
* process when subscribed to, and that publishes any writing errors and the completion signal
304302
*/
305-
public static Flux<DataBuffer> write(Publisher<DataBuffer> source,
306-
OutputStream outputStream) {
307-
303+
public static Flux<DataBuffer> write(Publisher<DataBuffer> source, OutputStream outputStream) {
308304
Assert.notNull(source, "'source' must not be null");
309305
Assert.notNull(outputStream, "'outputStream' must not be null");
310306

@@ -318,21 +314,17 @@ public static Flux<DataBuffer> write(Publisher<DataBuffer> source,
318314
* <strong>not</strong> {@linkplain #release(DataBuffer) release} the data buffers in the
319315
* source. If releasing is required, then subscribe to the returned {@code Flux} with a
320316
* {@link #releaseConsumer()}.
321-
* <p>Note that the writing process does not start until the returned {@code Flux} is subscribed
322-
* to.
317+
* <p>Note that the writing process does not start until the returned {@code Flux} is subscribed to.
323318
* @param source the stream of data buffers to be written
324319
* @param channel the channel to write to
325320
* @return a flux containing the same buffers as in {@code source}, that starts the writing
326321
* process when subscribed to, and that publishes any writing errors and the completion signal
327322
*/
328-
public static Flux<DataBuffer> write(Publisher<DataBuffer> source,
329-
WritableByteChannel channel) {
330-
323+
public static Flux<DataBuffer> write(Publisher<DataBuffer> source, WritableByteChannel channel) {
331324
Assert.notNull(source, "'source' must not be null");
332325
Assert.notNull(channel, "'channel' must not be null");
333326

334327
Flux<DataBuffer> flux = Flux.from(source);
335-
336328
return Flux.create(sink ->
337329
flux.subscribe(dataBuffer -> {
338330
try {
@@ -357,40 +349,36 @@ public static Flux<DataBuffer> write(Publisher<DataBuffer> source,
357349
* <strong>not</strong> {@linkplain #release(DataBuffer) release} the data buffers in the
358350
* source. If releasing is required, then subscribe to the returned {@code Flux} with a
359351
* {@link #releaseConsumer()}.
360-
* <p>Note that the writing process does not start until the returned {@code Flux} is subscribed
361-
* to.
352+
* <p>Note that the writing process does not start until the returned {@code Flux} is subscribed to.
362353
* @param source the stream of data buffers to be written
363354
* @param channel the channel to write to
364355
* @return a flux containing the same buffers as in {@code source}, that starts the writing
365356
* process when subscribed to, and that publishes any writing errors and the completion signal
366357
*/
367-
public static Flux<DataBuffer> write(Publisher<DataBuffer> source, AsynchronousFileChannel channel,
368-
long position) {
358+
public static Flux<DataBuffer> write(
359+
Publisher<DataBuffer> source, AsynchronousFileChannel channel, long position) {
369360

370361
Assert.notNull(source, "'source' must not be null");
371362
Assert.notNull(channel, "'channel' must not be null");
372363
Assert.isTrue(position >= 0, "'position' must be >= 0");
373364

374365
Flux<DataBuffer> flux = Flux.from(source);
375-
376366
return Flux.create(sink -> {
377-
BaseSubscriber<DataBuffer> subscriber =
378-
new AsynchronousFileChannelWriteCompletionHandler(sink, channel, position);
379-
flux.subscribe(subscriber);
367+
flux.subscribe(new AsynchronousFileChannelWriteCompletionHandler(sink, channel, position));
380368
});
381369
}
382370

383-
384371
private static void closeChannel(@Nullable Channel channel) {
385-
try {
386-
if (channel != null && channel.isOpen()) {
372+
if (channel != null && channel.isOpen()) {
373+
try {
387374
channel.close();
388375
}
389-
}
390-
catch (IOException ignored) {
376+
catch (IOException ignored) {
377+
}
391378
}
392379
}
393380

381+
394382
//---------------------------------------------------------------------
395383
// Various
396384
//---------------------------------------------------------------------
@@ -484,10 +472,7 @@ public static <T extends DataBuffer> T retain(T dataBuffer) {
484472
* @return {@code true} if the buffer was released; {@code false} otherwise.
485473
*/
486474
public static boolean release(@Nullable DataBuffer dataBuffer) {
487-
if (dataBuffer instanceof PooledDataBuffer) {
488-
return ((PooledDataBuffer) dataBuffer).release();
489-
}
490-
return false;
475+
return (dataBuffer instanceof PooledDataBuffer && ((PooledDataBuffer) dataBuffer).release());
491476
}
492477

493478
/**
@@ -520,18 +505,16 @@ public static Mono<DataBuffer> join(Publisher<DataBuffer> dataBuffers) {
520505
}
521506

522507

523-
private static class ReadableByteChannelGenerator
524-
implements Consumer<SynchronousSink<DataBuffer>> {
508+
private static class ReadableByteChannelGenerator implements Consumer<SynchronousSink<DataBuffer>> {
525509

526510
private final ReadableByteChannel channel;
527511

528512
private final DataBufferFactory dataBufferFactory;
529513

530514
private final int bufferSize;
531515

532-
533-
public ReadableByteChannelGenerator(ReadableByteChannel channel,
534-
DataBufferFactory dataBufferFactory, int bufferSize) {
516+
public ReadableByteChannelGenerator(
517+
ReadableByteChannel channel, DataBufferFactory dataBufferFactory, int bufferSize) {
535518

536519
this.channel = channel;
537520
this.dataBufferFactory = dataBufferFactory;
@@ -563,7 +546,6 @@ public void accept(SynchronousSink<DataBuffer> sink) {
563546
}
564547
}
565548
}
566-
567549
}
568550

569551

@@ -582,10 +564,9 @@ private static class AsynchronousFileChannelReadCompletionHandler
582564

583565
private final AtomicBoolean disposed = new AtomicBoolean();
584566

567+
public AsynchronousFileChannelReadCompletionHandler(AsynchronousFileChannel channel,
568+
FluxSink<DataBuffer> sink, long position, DataBufferFactory dataBufferFactory, int bufferSize) {
585569

586-
private AsynchronousFileChannelReadCompletionHandler(
587-
AsynchronousFileChannel channel, FluxSink<DataBuffer> sink,
588-
long position, DataBufferFactory dataBufferFactory, int bufferSize) {
589570
this.channel = channel;
590571
this.sink = sink;
591572
this.position = new AtomicLong(position);
@@ -599,10 +580,8 @@ public void completed(Integer read, DataBuffer dataBuffer) {
599580
long pos = this.position.addAndGet(read);
600581
dataBuffer.writePosition(read);
601582
this.sink.next(dataBuffer);
602-
603583
if (!this.disposed.get()) {
604-
DataBuffer newDataBuffer =
605-
this.dataBufferFactory.allocateBuffer(this.bufferSize);
584+
DataBuffer newDataBuffer = this.dataBufferFactory.allocateBuffer(this.bufferSize);
606585
ByteBuffer newByteBuffer = newDataBuffer.asByteBuffer(0, this.bufferSize);
607586
this.channel.read(newByteBuffer, pos, newDataBuffer, this);
608587
}
@@ -618,12 +597,10 @@ public void failed(Throwable exc, DataBuffer dataBuffer) {
618597
release(dataBuffer);
619598
this.sink.error(exc);
620599
}
621-
622600
}
623601

624602

625-
private static class AsynchronousFileChannelWriteCompletionHandler
626-
extends BaseSubscriber<DataBuffer>
603+
private static class AsynchronousFileChannelWriteCompletionHandler extends BaseSubscriber<DataBuffer>
627604
implements CompletionHandler<Integer, ByteBuffer> {
628605

629606
private final FluxSink<DataBuffer> sink;
@@ -639,6 +616,7 @@ private static class AsynchronousFileChannelWriteCompletionHandler
639616

640617
public AsynchronousFileChannelWriteCompletionHandler(
641618
FluxSink<DataBuffer> sink, AsynchronousFileChannel channel, long position) {
619+
642620
this.sink = sink;
643621
this.channel = channel;
644622
this.position = new AtomicLong(position);
@@ -653,7 +631,6 @@ protected void hookOnSubscribe(Subscription subscription) {
653631
protected void hookOnNext(DataBuffer value) {
654632
this.dataBuffer = value;
655633
ByteBuffer byteBuffer = value.asByteBuffer();
656-
657634
this.channel.write(byteBuffer, this.position.get(), byteBuffer, this);
658635
}
659636

@@ -678,7 +655,6 @@ public void completed(Integer written, ByteBuffer byteBuffer) {
678655
this.channel.write(byteBuffer, pos, byteBuffer, this);
679656
return;
680657
}
681-
682658
if (this.dataBuffer != null) {
683659
this.sink.next(this.dataBuffer);
684660
this.dataBuffer = null;
@@ -696,4 +672,5 @@ public void failed(Throwable exc, ByteBuffer byteBuffer) {
696672
this.sink.error(exc);
697673
}
698674
}
675+
699676
}

0 commit comments

Comments
 (0)