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

@@ -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));
}