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 68bda3cd688..a38e11bea10 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 @@ -56,6 +56,7 @@ public abstract class DataBufferUtils { private static final Consumer RELEASE_CONSUMER = DataBufferUtils::release; + //--------------------------------------------------------------------- // Reading //--------------------------------------------------------------------- @@ -74,31 +75,31 @@ public abstract class DataBufferUtils { * {@link #readInputStream(Callable, DataBufferFactory, int)}, to be removed in Spring 5.1 */ @Deprecated - public static Flux read(InputStream inputStream, - DataBufferFactory dataBufferFactory, int bufferSize) { + public static Flux read( + InputStream inputStream, DataBufferFactory dataBufferFactory, int bufferSize) { + return readInputStream(() -> inputStream, dataBufferFactory, bufferSize); } /** - * Obtain a {@link InputStream} from the given supplier, and read it into a {@code Flux} of - * {@code DataBuffer}s. Closes the input stream when the flux is terminated. + * Obtain a {@link InputStream} from the given supplier, and read it into a {@code Flux} + * of {@code DataBuffer}s. Closes the input stream when the flux is terminated. * @param inputStreamSupplier the supplier for the input stream to read from * @param dataBufferFactory the factory to create data buffers with * @param bufferSize the maximum size of the data buffers * @return a flux of data buffers read from the given channel */ - public static Flux readInputStream(Callable inputStreamSupplier, - DataBufferFactory dataBufferFactory, int bufferSize) { + public static Flux readInputStream( + Callable inputStreamSupplier, DataBufferFactory dataBufferFactory, int bufferSize) { Assert.notNull(inputStreamSupplier, "'inputStreamSupplier' must not be null"); - return readByteChannel(() -> Channels.newChannel(inputStreamSupplier.call()), - dataBufferFactory, bufferSize); + return readByteChannel(() -> Channels.newChannel(inputStreamSupplier.call()), dataBufferFactory, bufferSize); } /** - * Read the given {@code ReadableByteChannel} into a read-once {@code Flux} of - * {@code DataBuffer}s. Closes the channel when the flux is terminated. + * Read the given {@code ReadableByteChannel} into a read-once {@code Flux} + * of {@code DataBuffer}s. Closes the channel when the flux is terminated. *

The resulting {@code Flux} can only be subscribed to once. See * {@link #readByteChannel(Callable, DataBufferFactory, int)} for a variant that supports * multiple subscriptions. @@ -110,8 +111,9 @@ public abstract class DataBufferUtils { * {@link #readByteChannel(Callable, DataBufferFactory, int)}, to be removed in Spring 5.1 */ @Deprecated - public static Flux read(ReadableByteChannel channel, - DataBufferFactory dataBufferFactory, int bufferSize) { + public static Flux read( + ReadableByteChannel channel, DataBufferFactory dataBufferFactory, int bufferSize) { + return readByteChannel(() -> channel, dataBufferFactory, bufferSize); } @@ -123,8 +125,8 @@ public abstract class DataBufferUtils { * @param bufferSize the maximum size of the data buffers * @return a flux of data buffers read from the given channel */ - public static Flux readByteChannel(Callable channelSupplier, - DataBufferFactory dataBufferFactory, int bufferSize) { + public static Flux readByteChannel( + Callable channelSupplier, DataBufferFactory dataBufferFactory, int bufferSize) { Assert.notNull(channelSupplier, "'channelSupplier' must not be null"); Assert.notNull(dataBufferFactory, "'dataBufferFactory' must not be null"); @@ -156,8 +158,9 @@ public abstract class DataBufferUtils { * Spring 5.1 */ @Deprecated - public static Flux read(AsynchronousFileChannel channel, - DataBufferFactory dataBufferFactory, int bufferSize) { + public static Flux read( + AsynchronousFileChannel channel, DataBufferFactory dataBufferFactory, int bufferSize) { + return readAsynchronousFileChannel(() -> channel, dataBufferFactory, bufferSize); } @@ -178,8 +181,9 @@ public abstract class DataBufferUtils { * in Spring 5.1 */ @Deprecated - public static Flux read(AsynchronousFileChannel channel, - long position, DataBufferFactory dataBufferFactory, int bufferSize) { + public static Flux read( + AsynchronousFileChannel channel, long position, DataBufferFactory dataBufferFactory, int bufferSize) { + return readAsynchronousFileChannel(() -> channel, position, dataBufferFactory, bufferSize); } @@ -192,24 +196,22 @@ public abstract class DataBufferUtils { * @return a flux of data buffers read from the given channel */ public static Flux readAsynchronousFileChannel( - Callable channelSupplier, - DataBufferFactory dataBufferFactory, int bufferSize) { + Callable channelSupplier, DataBufferFactory dataBufferFactory, int bufferSize) { return readAsynchronousFileChannel(channelSupplier, 0, dataBufferFactory, bufferSize); } /** * Obtain a {@code AsynchronousFileChannel} from the given supplier, and read it into a - * {@code Flux} of {@code DataBuffer}s, starting at the given position. Closes the channel when - * the flux is terminated. + * {@code Flux} of {@code DataBuffer}s, starting at the given position. Closes the + * channel when the flux is terminated. * @param channelSupplier the supplier for the channel to read from * @param position the position to start reading from * @param dataBufferFactory the factory to create data buffers with * @param bufferSize the maximum size of the data buffers * @return a flux of data buffers read from the given channel */ - public static Flux readAsynchronousFileChannel( - Callable channelSupplier, + public static Flux readAsynchronousFileChannel(Callable channelSupplier, long position, DataBufferFactory dataBufferFactory, int bufferSize) { Assert.notNull(channelSupplier, "'channelSupplier' must not be null"); @@ -242,8 +244,8 @@ public abstract class DataBufferUtils { * @param bufferSize the maximum size of the data buffers * @return a flux of data buffers read from the given channel */ - public static Flux read(Resource resource, - DataBufferFactory dataBufferFactory, int bufferSize) { + public static Flux read( + Resource resource, DataBufferFactory dataBufferFactory, int bufferSize) { return read(resource, 0, dataBufferFactory, bufferSize); } @@ -262,13 +264,12 @@ public abstract class DataBufferUtils { * @param bufferSize the maximum size of the data buffers * @return a flux of data buffers read from the given channel */ - public static Flux read(Resource resource, long position, - DataBufferFactory dataBufferFactory, int bufferSize) { + public static Flux read( + Resource resource, long position, DataBufferFactory dataBufferFactory, int bufferSize) { try { if (resource.isFile()) { File file = resource.getFile(); - return readAsynchronousFileChannel( () -> AsynchronousFileChannel.open(file.toPath(), StandardOpenOption.READ), position, dataBufferFactory, bufferSize); @@ -283,8 +284,6 @@ public abstract class DataBufferUtils { } - - //--------------------------------------------------------------------- // Writing //--------------------------------------------------------------------- @@ -295,16 +294,13 @@ public abstract class DataBufferUtils { * not {@linkplain #release(DataBuffer) release} the data buffers in the * source. If releasing is required, then subscribe to the returned {@code Flux} with a * {@link #releaseConsumer()}. - *

Note that the writing process does not start until the returned {@code Flux} is subscribed - * to. + *

Note that the writing process does not start until the returned {@code Flux} is subscribed to. * @param source the stream of data buffers to be written * @param outputStream the output stream 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 */ - public static Flux write(Publisher source, - OutputStream outputStream) { - + public static Flux write(Publisher source, OutputStream outputStream) { Assert.notNull(source, "'source' must not be null"); Assert.notNull(outputStream, "'outputStream' must not be null"); @@ -318,21 +314,17 @@ public abstract class DataBufferUtils { * not {@linkplain #release(DataBuffer) release} the data buffers in the * source. If releasing is required, then subscribe to the returned {@code Flux} with a * {@link #releaseConsumer()}. - *

Note that the writing process does not start until the returned {@code Flux} is subscribed - * to. + *

Note that the writing process does not start until the returned {@code Flux} is subscribed to. * @param source the stream of data buffers to be written * @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 */ - public static Flux write(Publisher source, - WritableByteChannel channel) { - + public static Flux write(Publisher source, WritableByteChannel channel) { Assert.notNull(source, "'source' must not be null"); Assert.notNull(channel, "'channel' must not be null"); Flux flux = Flux.from(source); - return Flux.create(sink -> flux.subscribe(dataBuffer -> { try { @@ -357,40 +349,36 @@ public abstract class DataBufferUtils { * not {@linkplain #release(DataBuffer) release} the data buffers in the * source. If releasing is required, then subscribe to the returned {@code Flux} with a * {@link #releaseConsumer()}. - *

Note that the writing process does not start until the returned {@code Flux} is subscribed - * to. + *

Note that the writing process does not start until the returned {@code Flux} is subscribed to. * @param source the stream of data buffers to be written * @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 */ - public static Flux write(Publisher source, AsynchronousFileChannel channel, - long position) { + public static Flux write( + Publisher source, AsynchronousFileChannel channel, long position) { Assert.notNull(source, "'source' must not be null"); Assert.notNull(channel, "'channel' must not be null"); Assert.isTrue(position >= 0, "'position' must be >= 0"); Flux flux = Flux.from(source); - return Flux.create(sink -> { - BaseSubscriber subscriber = - new AsynchronousFileChannelWriteCompletionHandler(sink, channel, position); - flux.subscribe(subscriber); + flux.subscribe(new AsynchronousFileChannelWriteCompletionHandler(sink, channel, position)); }); } - private static void closeChannel(@Nullable Channel channel) { - try { - if (channel != null && channel.isOpen()) { + if (channel != null && channel.isOpen()) { + try { channel.close(); } - } - catch (IOException ignored) { + catch (IOException ignored) { + } } } + //--------------------------------------------------------------------- // Various //--------------------------------------------------------------------- @@ -484,10 +472,7 @@ public abstract class DataBufferUtils { * @return {@code true} if the buffer was released; {@code false} otherwise. */ public static boolean release(@Nullable DataBuffer dataBuffer) { - if (dataBuffer instanceof PooledDataBuffer) { - return ((PooledDataBuffer) dataBuffer).release(); - } - return false; + return (dataBuffer instanceof PooledDataBuffer && ((PooledDataBuffer) dataBuffer).release()); } /** @@ -520,8 +505,7 @@ public abstract class DataBufferUtils { } - private static class ReadableByteChannelGenerator - implements Consumer> { + private static class ReadableByteChannelGenerator implements Consumer> { private final ReadableByteChannel channel; @@ -529,9 +513,8 @@ public abstract class DataBufferUtils { private final int bufferSize; - - public ReadableByteChannelGenerator(ReadableByteChannel channel, - DataBufferFactory dataBufferFactory, int bufferSize) { + public ReadableByteChannelGenerator( + ReadableByteChannel channel, DataBufferFactory dataBufferFactory, int bufferSize) { this.channel = channel; this.dataBufferFactory = dataBufferFactory; @@ -563,7 +546,6 @@ public abstract class DataBufferUtils { } } } - } @@ -582,10 +564,9 @@ public abstract class DataBufferUtils { private final AtomicBoolean disposed = new AtomicBoolean(); + public AsynchronousFileChannelReadCompletionHandler(AsynchronousFileChannel channel, + FluxSink sink, long position, DataBufferFactory dataBufferFactory, int bufferSize) { - private AsynchronousFileChannelReadCompletionHandler( - AsynchronousFileChannel channel, FluxSink sink, - long position, DataBufferFactory dataBufferFactory, int bufferSize) { this.channel = channel; this.sink = sink; this.position = new AtomicLong(position); @@ -599,10 +580,8 @@ public abstract class DataBufferUtils { long pos = this.position.addAndGet(read); dataBuffer.writePosition(read); this.sink.next(dataBuffer); - if (!this.disposed.get()) { - DataBuffer newDataBuffer = - this.dataBufferFactory.allocateBuffer(this.bufferSize); + DataBuffer newDataBuffer = this.dataBufferFactory.allocateBuffer(this.bufferSize); ByteBuffer newByteBuffer = newDataBuffer.asByteBuffer(0, this.bufferSize); this.channel.read(newByteBuffer, pos, newDataBuffer, this); } @@ -618,12 +597,10 @@ public abstract class DataBufferUtils { release(dataBuffer); this.sink.error(exc); } - } - private static class AsynchronousFileChannelWriteCompletionHandler - extends BaseSubscriber + private static class AsynchronousFileChannelWriteCompletionHandler extends BaseSubscriber implements CompletionHandler { private final FluxSink sink; @@ -639,6 +616,7 @@ public abstract class DataBufferUtils { public AsynchronousFileChannelWriteCompletionHandler( FluxSink sink, AsynchronousFileChannel channel, long position) { + this.sink = sink; this.channel = channel; this.position = new AtomicLong(position); @@ -653,7 +631,6 @@ public abstract class DataBufferUtils { protected void hookOnNext(DataBuffer value) { this.dataBuffer = value; ByteBuffer byteBuffer = value.asByteBuffer(); - this.channel.write(byteBuffer, this.position.get(), byteBuffer, this); } @@ -678,7 +655,6 @@ public abstract class DataBufferUtils { this.channel.write(byteBuffer, pos, byteBuffer, this); return; } - if (this.dataBuffer != null) { this.sink.next(this.dataBuffer); this.dataBuffer = null; @@ -696,4 +672,5 @@ public abstract class DataBufferUtils { this.sink.error(exc); } } + } diff --git a/spring-web/src/main/java/org/springframework/http/server/DefaultPathContainer.java b/spring-web/src/main/java/org/springframework/http/server/DefaultPathContainer.java index db54ca4cde0..6e945855237 100644 --- a/spring-web/src/main/java/org/springframework/http/server/DefaultPathContainer.java +++ b/spring-web/src/main/java/org/springframework/http/server/DefaultPathContainer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -200,8 +200,7 @@ class DefaultPathContainer implements PathContainer { private final MultiValueMap parameters; - - DefaultPathSegment(String value, String valueToMatch, MultiValueMap params) { + public DefaultPathSegment(String value, String valueToMatch, MultiValueMap params) { Assert.isTrue(!value.contains("/"), () -> "Invalid path segment value: " + value); this.value = value; this.valueToMatch = valueToMatch; @@ -209,7 +208,6 @@ class DefaultPathContainer implements PathContainer { this.parameters = CollectionUtils.unmodifiableMultiValueMap(params); } - @Override public String value() { return this.value; @@ -230,7 +228,6 @@ class DefaultPathContainer implements PathContainer { return this.parameters; } - @Override public boolean equals(@Nullable Object other) { if (this == other) { @@ -248,7 +245,8 @@ class DefaultPathContainer implements PathContainer { } public String toString() { - return "[value='" + this.value + "']"; } + return "[value='" + this.value + "']"; + } } } diff --git a/spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java b/spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java index 8d466a2a657..38b0e365d5f 100644 --- a/spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java +++ b/spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,8 +29,8 @@ import org.springframework.util.ObjectUtils; /** * Default implementation of {@link UriBuilderFactory} providing options to - * pre-configure all UriBuilder instances with common properties such as a base - * URI, encoding mode, and default URI variables. + * pre-configure all {@link UriBuilder} instances with common properties + * such as a base URI, encoding mode, and default URI variables. * *

Uses {@link UriComponentsBuilder} for URI building. * @@ -40,7 +40,7 @@ import org.springframework.util.ObjectUtils; */ public class DefaultUriBuilderFactory implements UriBuilderFactory { - public enum EncodingMode {URI_COMPONENT, VALUES_ONLY, NONE } + public enum EncodingMode {URI_COMPONENT, VALUES_ONLY, NONE} private final UriComponentsBuilder baseUri; @@ -78,7 +78,7 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory { * {@code UriComponentsBuilder}. */ public DefaultUriBuilderFactory(UriComponentsBuilder baseUri) { - Assert.notNull(baseUri, "'baseUri' is required."); + Assert.notNull(baseUri, "'baseUri' is required"); this.baseUri = baseUri; } @@ -182,13 +182,11 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory { private final UriComponentsBuilder uriComponentsBuilder; - public DefaultUriBuilder(String uriTemplate) { this.uriComponentsBuilder = initUriComponentsBuilder(uriTemplate); } private UriComponentsBuilder initUriComponentsBuilder(String uriTemplate) { - UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uriTemplate); UriComponents uriComponents = uriComponentsBuilder.build(); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/SimpleHandlerAdapter.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/SimpleHandlerAdapter.java index 2320bc5b00d..374cf1c6e75 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/SimpleHandlerAdapter.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/SimpleHandlerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,6 @@ package org.springframework.web.reactive.result; - import reactor.core.publisher.Mono; import org.springframework.web.reactive.DispatcherHandler; @@ -26,8 +25,8 @@ import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebHandler; /** - * HandlerAdapter that allows using the plain {@link WebHandler} contract with - * the generic {@link DispatcherHandler}. + * {@link HandlerAdapter} that allows using the plain {@link WebHandler} contract + * with the generic {@link DispatcherHandler}. * * @author Rossen Stoyanchev * @author Sebastien Deleuze