Shared static instance of DefaultDataBufferFactory

This commit is contained in:
Rossen Stoyanchev
2020-06-24 16:12:34 +01:00
parent 3a06622270
commit b16f6fa456
67 changed files with 184 additions and 273 deletions

View File

@@ -54,7 +54,7 @@ public class HttpComponentsClientHttpConnector implements ClientHttpConnector {
private final BiFunction<HttpMethod, URI, ? extends HttpClientContext> contextProvider;
private DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
private DataBufferFactory dataBufferFactory = DefaultDataBufferFactory.sharedInstance;
/**

View File

@@ -43,7 +43,7 @@ public class JettyClientHttpConnector implements ClientHttpConnector {
private final HttpClient httpClient;
private DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
private DataBufferFactory bufferFactory = DefaultDataBufferFactory.sharedInstance;
/**

View File

@@ -30,7 +30,6 @@ import org.springframework.core.codec.CodecException;
import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.StringDecoder;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferLimitException;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.MediaType;
@@ -49,8 +48,6 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
private static final ResolvableType STRING_TYPE = ResolvableType.forClass(String.class);
private static final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
@Nullable
private final Decoder<?> decoder;
@@ -194,7 +191,7 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
throw new CodecException("No SSE decoder configured and the data is not String.");
}
byte[] bytes = data.toString().getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = bufferFactory.wrap(bytes); // wrapping only, no allocation
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes); // wrapping only, no allocation
return this.decoder.decode(buffer, dataType, MediaType.TEXT_EVENT_STREAM, hints);
}

View File

@@ -28,7 +28,6 @@ import reactor.core.publisher.Flux;
import org.springframework.core.ResolvableType;
import org.springframework.core.codec.StringDecoder;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.lang.Nullable;
@@ -79,8 +78,7 @@ public class Jackson2JsonDecoder extends AbstractJackson2Decoder {
MimeType textMimeType = new MimeType(MimeTypeUtils.TEXT_PLAIN, charset);
Flux<String> decoded = STRING_DECODER.decode(input, STRING_TYPE, textMimeType, null);
DataBufferFactory factory = new DefaultDataBufferFactory();
return decoded.map(s -> factory.wrap(s.getBytes(StandardCharsets.UTF_8)));
return decoded.map(s -> DefaultDataBufferFactory.sharedInstance.wrap(s.getBytes(StandardCharsets.UTF_8)));
}
}

View File

@@ -22,7 +22,6 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.ContentDisposition;
@@ -107,8 +106,6 @@ abstract class DefaultParts {
private final String value;
private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
public DefaultFormFieldPart(HttpHeaders headers, String value) {
super(headers);
this.value = value;
@@ -118,7 +115,7 @@ abstract class DefaultParts {
public Flux<DataBuffer> content() {
return Flux.defer(() -> {
byte[] bytes = this.value.getBytes(MultipartUtils.charset(headers()));
return Flux.just(this.bufferFactory.wrap(bytes));
return Flux.just(DefaultDataBufferFactory.sharedInstance.wrap(bytes));
});
}

View File

@@ -44,7 +44,6 @@ import reactor.core.scheduler.Scheduler;
import org.springframework.core.codec.DecodingException;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferLimitException;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
@@ -61,8 +60,6 @@ import org.springframework.util.FastByteArrayOutputStream;
*/
final class PartGenerator extends BaseSubscriber<MultipartParser.Token> {
private static final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
private static final Log logger = LogFactory.getLog(PartGenerator.class);
private final AtomicReference<State> state = new AtomicReference<>(new InitialState());
@@ -513,7 +510,7 @@ final class PartGenerator extends BaseSubscriber<MultipartParser.Token> {
DataBufferUtils.release(buffer);
}
this.content.clear();
Flux<DataBuffer> content = Flux.just(bufferFactory.wrap(bytes));
Flux<DataBuffer> content = Flux.just(DefaultDataBufferFactory.sharedInstance.wrap(bytes));
emitPart(DefaultParts.part(this.headers, content));
}
@@ -678,8 +675,10 @@ final class PartGenerator extends BaseSubscriber<MultipartParser.Token> {
}
private Flux<DataBuffer> partContent() {
return DataBufferUtils.readByteChannel(() -> Files.newByteChannel(this.file, StandardOpenOption.READ),
bufferFactory, 1024)
return DataBufferUtils
.readByteChannel(
() -> Files.newByteChannel(this.file, StandardOpenOption.READ),
DefaultDataBufferFactory.sharedInstance, 1024)
.subscribeOn(PartGenerator.this.blockingOperationScheduler);
}

View File

@@ -49,7 +49,6 @@ import org.springframework.core.ResolvableType;
import org.springframework.core.codec.DecodingException;
import org.springframework.core.codec.Hints;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferLimitException;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
@@ -79,10 +78,6 @@ import org.springframework.util.Assert;
*/
public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implements HttpMessageReader<Part> {
// Static DataBufferFactory to copy from FileInputStream or wrap bytes[].
private static final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
private int maxInMemorySize = 256 * 1024;
private long maxDiskUsagePerPart = -1;
@@ -441,7 +436,8 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem
@Override
public Flux<DataBuffer> content() {
return DataBufferUtils.readInputStream(getStorage()::getInputStream, bufferFactory, 4096);
return DataBufferUtils.readInputStream(
getStorage()::getInputStream, DefaultDataBufferFactory.sharedInstance, 4096);
}
protected StreamStorage getStorage() {
@@ -530,7 +526,7 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem
@Override
public Flux<DataBuffer> content() {
byte[] bytes = this.content.getBytes(getCharset());
return Flux.just(bufferFactory.wrap(bytes));
return Flux.just(DefaultDataBufferFactory.sharedInstance.wrap(bytes));
}
private Charset getCharset() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -71,7 +71,7 @@ public class ServletHttpHandlerAdapter implements Servlet {
@Nullable
private String servletPath;
private DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory(false);
private DataBufferFactory dataBufferFactory = DefaultDataBufferFactory.sharedInstance;
public ServletHttpHandlerAdapter(HttpHandler httpHandler) {

View File

@@ -60,7 +60,7 @@ import org.springframework.util.StringUtils;
*/
class ServletServerHttpRequest extends AbstractServerHttpRequest {
static final DataBuffer EOF_BUFFER = new DefaultDataBufferFactory().allocateBuffer(0);
static final DataBuffer EOF_BUFFER = DefaultDataBufferFactory.sharedInstance.allocateBuffer(0);
private final HttpServletRequest request;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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.
@@ -45,7 +45,7 @@ public class UndertowHttpHandlerAdapter implements io.undertow.server.HttpHandle
private final HttpHandler httpHandler;
private DataBufferFactory bufferFactory = new DefaultDataBufferFactory(false);
private DataBufferFactory bufferFactory = DefaultDataBufferFactory.sharedInstance;
public UndertowHttpHandlerAdapter(HttpHandler httpHandler) {

View File

@@ -45,7 +45,6 @@ import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpMethod;
@@ -68,8 +67,6 @@ public class ClientHttpConnectorTests {
private final MockWebServer server = new MockWebServer();
private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
@BeforeEach
void startServer() throws IOException {
server.start();
@@ -102,7 +99,7 @@ public class ClientHttpConnectorTests {
if (requestHasBody) {
Mono<DataBuffer> body = Mono.fromCallable(() -> {
byte[] bytes = requestBody.getBytes(StandardCharsets.UTF_8);
return this.bufferFactory.wrap(bytes);
return DefaultDataBufferFactory.sharedInstance.wrap(bytes);
});
return request.writeWith(body);
}
@@ -228,7 +225,7 @@ public class ClientHttpConnectorTests {
private Mono<DataBuffer> stringBuffer(String value) {
return Mono.fromCallable(() -> {
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.allocateBuffer(bytes.length);
buffer.write(bytes);
return buffer;
});

View File

@@ -155,7 +155,7 @@ class EncoderHttpMessageWriterTests {
@Test
void setContentLengthForMonoBody() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBufferFactory factory = DefaultDataBufferFactory.sharedInstance;
DataBuffer buffer = factory.wrap("body".getBytes(StandardCharsets.UTF_8));
configureEncoder(Flux.just(buffer), MimeTypeUtils.TEXT_PLAIN);
HttpMessageWriter<String> writer = new EncoderHttpMessageWriter<>(this.encoder);

View File

@@ -251,7 +251,7 @@ public class MultipartHttpMessageWriterTests extends AbstractLeakCheckingTests {
@Test // SPR-16376
public void customContentDisposition() throws IOException {
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
Flux<DataBuffer> buffers = DataBufferUtils.read(logo, new DefaultDataBufferFactory(), 1024);
Flux<DataBuffer> buffers = DataBufferUtils.read(logo, DefaultDataBufferFactory.sharedInstance, 1024);
long contentLength = logo.contentLength();
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();

View File

@@ -242,8 +242,9 @@ public class ClientCodecConfigurerTests {
Object expected = !textOnly;
assertThat(decoder.canDecode(forClass(String.class), MediaType.TEXT_EVENT_STREAM)).isEqualTo(expected);
byte[] bytes = "line1\nline2".getBytes(StandardCharsets.UTF_8);
Flux<String> decoded = (Flux<String>) decoder.decode(
Flux.just(new DefaultDataBufferFactory().wrap("line1\nline2".getBytes(StandardCharsets.UTF_8))),
Flux.just(DefaultDataBufferFactory.sharedInstance.wrap(bytes)),
ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap());
assertThat(decoded.collectList().block(Duration.ZERO)).isEqualTo(Arrays.asList("line1", "line2"));

View File

@@ -266,8 +266,9 @@ public class ServerCodecConfigurerTests {
Object expected = !textOnly;
assertThat(decoder.canDecode(forClass(String.class), MediaType.TEXT_EVENT_STREAM)).isEqualTo(expected);
byte[] bytes = "line1\nline2".getBytes(StandardCharsets.UTF_8);
Flux<String> flux = (Flux<String>) decoder.decode(
Flux.just(new DefaultDataBufferFactory().wrap("line1\nline2".getBytes(StandardCharsets.UTF_8))),
Flux.just(DefaultDataBufferFactory.sharedInstance.wrap(bytes)),
ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap());
assertThat(flux.collectList().block(Duration.ZERO)).isEqualTo(Arrays.asList("line1", "line2"));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -24,7 +24,6 @@ import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
@@ -42,8 +41,6 @@ class AsyncIntegrationTests extends AbstractHttpHandlerIntegrationTests {
private final Scheduler asyncGroup = Schedulers.parallel();
private final DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
@Override
protected AsyncHandler createHttpHandler() {
@@ -68,7 +65,8 @@ class AsyncIntegrationTests extends AbstractHttpHandlerIntegrationTests {
return response.writeWith(Flux.just("h", "e", "l", "l", "o")
.delayElements(Duration.ofMillis(100))
.publishOn(asyncGroup)
.collect(dataBufferFactory::allocateBuffer, (buffer, str) -> buffer.write(str.getBytes())));
.collect(DefaultDataBufferFactory.sharedInstance::allocateBuffer,
(buffer, str) -> buffer.write(str.getBytes())));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-20 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.
@@ -24,7 +24,6 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
@@ -47,8 +46,6 @@ class RandomHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTests
private final RandomHandler handler = new RandomHandler();
private final DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
@Override
protected RandomHandler createHttpHandler() {
@@ -105,7 +102,7 @@ class RandomHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTests
private DataBuffer randomBuffer(int size) {
byte[] bytes = new byte[size];
rnd.nextBytes(bytes);
DataBuffer buffer = dataBufferFactory.allocateBuffer(size);
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.allocateBuffer(size);
buffer.write(bytes);
return buffer;
}

View File

@@ -198,7 +198,7 @@ public class ServerHttpRequestTests {
MockHttpServletRequest request = new TestHttpServletRequest(uri);
request.setContextPath(contextPath);
AsyncContext asyncContext = new MockAsyncContext(request, new MockHttpServletResponse());
return new ServletServerHttpRequest(request, asyncContext, "", new DefaultDataBufferFactory(), 1024);
return new ServletServerHttpRequest(request, asyncContext, "", DefaultDataBufferFactory.sharedInstance, 1024);
}
private static class TestHttpServletRequest extends MockHttpServletRequest {

View File

@@ -178,7 +178,7 @@ public class ServerHttpResponseTests {
private DefaultDataBuffer wrap(String a) {
return new DefaultDataBufferFactory().wrap(ByteBuffer.wrap(a.getBytes(StandardCharsets.UTF_8)));
return DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(a.getBytes(StandardCharsets.UTF_8)));
}
@@ -193,7 +193,7 @@ public class ServerHttpResponseTests {
private final List<DataBuffer> body = new ArrayList<>();
public TestServerHttpResponse() {
super(new DefaultDataBufferFactory());
super(DefaultDataBufferFactory.sharedInstance);
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -117,7 +117,7 @@ public class WebHttpHandlerBuilderTests {
private static Mono<Void> writeToResponse(ServerWebExchange exchange, String value) {
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = new DefaultDataBufferFactory().wrap(bytes);
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
return exchange.getResponse().writeWith(Flux.just(buffer));
}

View File

@@ -53,8 +53,6 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest implements
private final URI url;
private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
private Flux<DataBuffer> body = Flux.error(
new IllegalStateException("The body is not set. " +
"Did handling complete with success? Is a custom \"writeHandler\" configured?"));
@@ -109,7 +107,7 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest implements
@Override
public DataBufferFactory bufferFactory() {
return this.bufferFactory;
return DefaultDataBufferFactory.sharedInstance;
}
@Override

View File

@@ -26,7 +26,6 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpHeaders;
@@ -55,8 +54,6 @@ public class MockClientHttpResponse implements ClientHttpResponse {
private Flux<DataBuffer> body = Flux.empty();
private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
public MockClientHttpResponse(HttpStatus status) {
Assert.notNull(status, "HttpStatus is required");
@@ -109,7 +106,7 @@ public class MockClientHttpResponse implements ClientHttpResponse {
private DataBuffer toDataBuffer(String body, Charset charset) {
byte[] bytes = body.getBytes(charset);
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
return this.bufferFactory.wrap(byteBuffer);
return DefaultDataBufferFactory.sharedInstance.wrap(byteBuffer);
}
@Override

View File

@@ -30,7 +30,6 @@ import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
@@ -428,8 +427,6 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
private static class DefaultBodyBuilder implements BodyBuilder {
private static final DataBufferFactory BUFFER_FACTORY = new DefaultDataBufferFactory();
private final String methodValue;
private final URI url;
@@ -579,7 +576,9 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
@Override
public MockServerHttpRequest body(String body) {
return body(Flux.just(BUFFER_FACTORY.wrap(body.getBytes(getCharset()))));
byte[] bytes = body.getBytes(getCharset());
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
return body(Flux.just(buffer));
}
private Charset getCharset() {

View File

@@ -57,7 +57,7 @@ public class MockServerHttpResponse extends AbstractServerHttpResponse {
public MockServerHttpResponse() {
this(new DefaultDataBufferFactory());
this(DefaultDataBufferFactory.sharedInstance);
}
public MockServerHttpResponse(DataBufferFactory dataBufferFactory) {