Skip to content

Commit b16f6fa

Browse files
committed
Shared static instance of DefaultDataBufferFactory
1 parent 3a06622 commit b16f6fa

File tree

67 files changed

+184
-273
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+184
-273
lines changed

spring-core/src/main/java/org/springframework/core/codec/StringDecoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public static StringDecoder allMimeTypes(List<String> delimiters, boolean stripD
271271

272272
private static class EndFrameBuffer extends DataBufferWrapper {
273273

274-
private static final DataBuffer BUFFER = new DefaultDataBufferFactory().wrap(new byte[0]);
274+
private static final DataBuffer BUFFER = DefaultDataBufferFactory.sharedInstance.wrap(new byte[0]);
275275

276276
private byte[] delimiter;
277277

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -38,6 +38,12 @@ public class DefaultDataBufferFactory implements DataBufferFactory {
3838
*/
3939
public static final int DEFAULT_INITIAL_CAPACITY = 256;
4040

41+
/**
42+
* Shared instance based on the default constructor.
43+
* @since 5.3
44+
*/
45+
public static final DefaultDataBufferFactory sharedInstance = new DefaultDataBufferFactory();
46+
4147

4248
private final boolean preferDirect;
4349

@@ -46,6 +52,7 @@ public class DefaultDataBufferFactory implements DataBufferFactory {
4652

4753
/**
4854
* Creates a new {@code DefaultDataBufferFactory} with default settings.
55+
* @see #sharedInstance
4956
*/
5057
public DefaultDataBufferFactory() {
5158
this(false);

spring-core/src/test/java/org/springframework/core/io/buffer/LimitedDataBufferListTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -27,9 +27,6 @@
2727
*/
2828
public class LimitedDataBufferListTests {
2929

30-
private final static DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
31-
32-
3330
@Test
3431
void limitEnforced() {
3532
Assertions.assertThatThrownBy(() -> new LimitedDataBufferList(5).add(toDataBuffer("123456")))
@@ -51,7 +48,8 @@ void clearResetsCount() {
5148

5249

5350
private static DataBuffer toDataBuffer(String value) {
54-
return bufferFactory.wrap(value.getBytes(StandardCharsets.UTF_8));
51+
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
52+
return DefaultDataBufferFactory.sharedInstance.wrap(bytes);
5553
}
5654

5755
}

spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/AbstractEncoderMethodReturnValueHandler.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -73,8 +73,6 @@ public abstract class AbstractEncoderMethodReturnValueHandler implements Handler
7373

7474
private final ReactiveAdapterRegistry adapterRegistry;
7575

76-
private DataBufferFactory defaultBufferFactory = new DefaultDataBufferFactory();
77-
7876

7977
protected AbstractEncoderMethodReturnValueHandler(List<Encoder<?>> encoders, ReactiveAdapterRegistry registry) {
8078
Assert.notEmpty(encoders, "At least one Encoder is required");
@@ -114,7 +112,8 @@ public Mono<Void> handleReturnValue(
114112
}
115113

116114
DataBufferFactory bufferFactory = (DataBufferFactory) message.getHeaders()
117-
.getOrDefault(HandlerMethodReturnValueHandler.DATA_BUFFER_FACTORY_HEADER, this.defaultBufferFactory);
115+
.getOrDefault(HandlerMethodReturnValueHandler.DATA_BUFFER_FACTORY_HEADER,
116+
DefaultDataBufferFactory.sharedInstance);
118117

119118
MimeType mimeType = (MimeType) message.getHeaders().get(MessageHeaders.CONTENT_TYPE);
120119
Flux<DataBuffer> encodedContent = encodeContent(

spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/reactive/MessageMappingMessageHandlerTests.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -35,7 +35,6 @@
3535
import org.springframework.core.env.MapPropertySource;
3636
import org.springframework.core.env.PropertySource;
3737
import org.springframework.core.io.buffer.DataBuffer;
38-
import org.springframework.core.io.buffer.DataBufferFactory;
3938
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
4039
import org.springframework.messaging.Message;
4140
import org.springframework.messaging.handler.DestinationPatternsMessageCondition;
@@ -60,9 +59,6 @@
6059
@SuppressWarnings("ALL")
6160
public class MessageMappingMessageHandlerTests {
6261

63-
private static final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
64-
65-
6662
private TestEncoderMethodReturnValueHandler returnValueHandler;
6763

6864

@@ -163,7 +159,7 @@ private Message<?> message(String destination, String... content) {
163159
}
164160

165161
private DataBuffer toDataBuffer(String payload) {
166-
return bufferFactory.wrap(payload.getBytes(UTF_8));
162+
return DefaultDataBufferFactory.sharedInstance.wrap(payload.getBytes(UTF_8));
167163
}
168164

169165
private void verifyOutputContent(List<String> expected) {

spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/reactive/PayloadMethodArgumentResolverTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -154,7 +154,7 @@ public void validateStringFlux() {
154154

155155

156156
private DataBuffer toDataBuffer(String value) {
157-
return new DefaultDataBufferFactory().wrap(value.getBytes(StandardCharsets.UTF_8));
157+
return DefaultDataBufferFactory.sharedInstance.wrap(value.getBytes(StandardCharsets.UTF_8));
158158
}
159159

160160

spring-messaging/src/test/java/org/springframework/messaging/rsocket/DefaultRSocketRequesterBuilderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public void setupWithAsyncValues() {
232232
@Test
233233
public void frameDecoderMatchesDataBufferFactory() throws Exception {
234234
testPayloadDecoder(new NettyDataBufferFactory(ByteBufAllocator.DEFAULT), PayloadDecoder.ZERO_COPY);
235-
testPayloadDecoder(new DefaultDataBufferFactory(), PayloadDecoder.DEFAULT);
235+
testPayloadDecoder(DefaultDataBufferFactory.sharedInstance, PayloadDecoder.DEFAULT);
236236
}
237237

238238
private void testPayloadDecoder(DataBufferFactory bufferFactory, PayloadDecoder payloadDecoder)

spring-messaging/src/test/java/org/springframework/messaging/rsocket/DefaultRSocketRequesterTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ public class DefaultRSocketRequesterTests {
6565

6666
private final RSocketStrategies strategies = RSocketStrategies.create();
6767

68-
private final DefaultDataBufferFactory bufferFactory = new DefaultDataBufferFactory();
69-
7068

7169
@BeforeEach
7270
public void setUp() {
@@ -244,7 +242,8 @@ public void fluxToMonoIsRejected() {
244242
}
245243

246244
private Payload toPayload(String value) {
247-
return PayloadUtils.createPayload(bufferFactory.wrap(value.getBytes(StandardCharsets.UTF_8)));
245+
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
246+
return PayloadUtils.createPayload(DefaultDataBufferFactory.sharedInstance.wrap(bytes));
248247
}
249248

250249

spring-messaging/src/test/java/org/springframework/messaging/rsocket/MetadataEncoderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public void mimeTypeDoesNotMatchConnectionMetadataMimeType() {
203203

204204
@Test
205205
public void defaultDataBufferFactory() {
206-
DefaultDataBufferFactory bufferFactory = new DefaultDataBufferFactory();
206+
DefaultDataBufferFactory bufferFactory = DefaultDataBufferFactory.sharedInstance;
207207
RSocketStrategies strategies = RSocketStrategies.builder().dataBufferFactory(bufferFactory).build();
208208

209209
DataBuffer buffer = new MetadataEncoder(COMPOSITE_METADATA, strategies)

spring-messaging/src/test/java/org/springframework/messaging/rsocket/PayloadUtilsTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ public class PayloadUtilsTests {
4444
private LeakAwareNettyDataBufferFactory nettyBufferFactory =
4545
new LeakAwareNettyDataBufferFactory(PooledByteBufAllocator.DEFAULT);
4646

47-
private DefaultDataBufferFactory defaultBufferFactory = new DefaultDataBufferFactory();
48-
4947

5048
@AfterEach
5149
public void tearDown() throws Exception {
@@ -70,7 +68,7 @@ public void retainAndReleaseWithNettyFactory() {
7068
@Test
7169
public void retainAndReleaseWithDefaultFactory() {
7270
Payload payload = ByteBufPayload.create("sample data");
73-
DataBuffer buffer = PayloadUtils.retainDataAndReleasePayload(payload, this.defaultBufferFactory);
71+
DataBuffer buffer = PayloadUtils.retainDataAndReleasePayload(payload, DefaultDataBufferFactory.sharedInstance);
7472

7573
assertThat(buffer).isInstanceOf(DefaultDataBuffer.class);
7674
assertThat(payload.refCnt()).isEqualTo(0);
@@ -163,7 +161,7 @@ private NettyDataBuffer createNettyDataBuffer(String content) {
163161
}
164162

165163
private DefaultDataBuffer createDefaultDataBuffer(String content) {
166-
DefaultDataBuffer buffer = this.defaultBufferFactory.allocateBuffer();
164+
DefaultDataBuffer buffer = DefaultDataBufferFactory.sharedInstance.allocateBuffer();
167165
buffer.write(content, StandardCharsets.UTF_8);
168166
return buffer;
169167
}

spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ResourceDatabasePopulator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
7474

7575
private boolean ignoreFailedDrops = false;
7676

77-
private DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
77+
private DataBufferFactory dataBufferFactory = DefaultDataBufferFactory.sharedInstance;
7878

7979

8080
/**

spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,9 @@ public static Mono<Void> executeSqlScript(Connection connection, Resource resour
512512
* @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection
513513
*/
514514
public static Mono<Void> executeSqlScript(Connection connection, EncodedResource resource) throws ScriptException {
515-
return executeSqlScript(connection, resource, new DefaultDataBufferFactory(), false, false, DEFAULT_COMMENT_PREFIX,
516-
DEFAULT_STATEMENT_SEPARATOR, DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER);
515+
return executeSqlScript(connection, resource, DefaultDataBufferFactory.sharedInstance, false, false,
516+
DEFAULT_COMMENT_PREFIX, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
517+
DEFAULT_BLOCK_COMMENT_END_DELIMITER);
517518
}
518519

519520
/**

spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/ScriptUtilsUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public void containsDelimiters() {
213213
private String readScript(String path) {
214214
EncodedResource resource = new EncodedResource(
215215
new ClassPathResource(path, getClass()));
216-
return ScriptUtils.readScript(resource, new DefaultDataBufferFactory()).block();
216+
return ScriptUtils.readScript(resource, DefaultDataBufferFactory.sharedInstance).block();
217217
}
218218

219219
}

spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
5252

5353
private final URI url;
5454

55-
private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
56-
5755
private Flux<DataBuffer> body = Flux.error(
5856
new IllegalStateException("The body is not set. " +
5957
"Did handling complete with success? Is a custom \"writeHandler\" configured?"));
@@ -103,7 +101,7 @@ public URI getURI() {
103101

104102
@Override
105103
public DataBufferFactory bufferFactory() {
106-
return this.bufferFactory;
104+
return DefaultDataBufferFactory.sharedInstance;
107105
}
108106

109107
@Override

spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpResponse.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import reactor.core.publisher.Mono;
2727

2828
import org.springframework.core.io.buffer.DataBuffer;
29-
import org.springframework.core.io.buffer.DataBufferFactory;
3029
import org.springframework.core.io.buffer.DataBufferUtils;
3130
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
3231
import org.springframework.http.HttpHeaders;
@@ -55,8 +54,6 @@ public class MockClientHttpResponse implements ClientHttpResponse {
5554

5655
private Flux<DataBuffer> body = Flux.empty();
5756

58-
private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
59-
6057

6158
public MockClientHttpResponse(HttpStatus status) {
6259
Assert.notNull(status, "HttpStatus is required");
@@ -109,7 +106,7 @@ public void setBody(String body, Charset charset) {
109106
private DataBuffer toDataBuffer(String body, Charset charset) {
110107
byte[] bytes = body.getBytes(charset);
111108
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
112-
return this.bufferFactory.wrap(byteBuffer);
109+
return DefaultDataBufferFactory.sharedInstance.wrap(byteBuffer);
113110
}
114111

115112
@Override

spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import reactor.core.publisher.Flux;
3131

3232
import org.springframework.core.io.buffer.DataBuffer;
33-
import org.springframework.core.io.buffer.DataBufferFactory;
3433
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
3534
import org.springframework.http.HttpCookie;
3635
import org.springframework.http.HttpHeaders;
@@ -428,8 +427,6 @@ public interface BodyBuilder extends BaseBuilder<BodyBuilder> {
428427

429428
private static class DefaultBodyBuilder implements BodyBuilder {
430429

431-
private static final DataBufferFactory BUFFER_FACTORY = new DefaultDataBufferFactory();
432-
433430
private final String methodValue;
434431

435432
private final URI url;
@@ -579,7 +576,9 @@ public MockServerHttpRequest build() {
579576

580577
@Override
581578
public MockServerHttpRequest body(String body) {
582-
return body(Flux.just(BUFFER_FACTORY.wrap(body.getBytes(getCharset()))));
579+
byte[] bytes = body.getBytes(getCharset());
580+
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
581+
return body(Flux.just(buffer));
583582
}
584583

585584
private Charset getCharset() {

spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class MockServerHttpResponse extends AbstractServerHttpResponse {
5757

5858

5959
public MockServerHttpResponse() {
60-
this(new DefaultDataBufferFactory());
60+
this(DefaultDataBufferFactory.sharedInstance);
6161
}
6262

6363
public MockServerHttpResponse(DataBufferFactory dataBufferFactory) {

spring-test/src/main/java/org/springframework/test/web/reactive/server/WiretapConnector.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import reactor.core.publisher.MonoProcessor;
3030

3131
import org.springframework.core.io.buffer.DataBuffer;
32-
import org.springframework.core.io.buffer.DataBufferFactory;
3332
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
3433
import org.springframework.http.HttpMethod;
3534
import org.springframework.http.client.reactive.ClientHttpConnector;
@@ -125,16 +124,13 @@ public ExchangeResult createExchangeResult(Duration timeout, @Nullable String ur
125124
*/
126125
final static class WiretapRecorder {
127126

128-
private static final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
129-
130-
131127
@Nullable
132128
private final Flux<? extends DataBuffer> publisher;
133129

134130
@Nullable
135131
private final Flux<? extends Publisher<? extends DataBuffer>> publisherNested;
136132

137-
private final DataBuffer buffer = bufferFactory.allocateBuffer();
133+
private final DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.allocateBuffer();
138134

139135
private final MonoProcessor<byte[]> content = MonoProcessor.create();
140136

spring-test/src/test/java/org/springframework/test/web/reactive/server/HttpHandlerConnectorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void handlerOnNonBlockingThread() {
121121
}
122122

123123
private DataBuffer toDataBuffer(String body) {
124-
return new DefaultDataBufferFactory().wrap(body.getBytes(UTF_8));
124+
return DefaultDataBufferFactory.sharedInstance.wrap(body.getBytes(UTF_8));
125125
}
126126

127127

spring-test/src/test/java/org/springframework/test/web/reactive/server/MockServerSpecTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -88,7 +88,7 @@ private static class TestMockServerSpec extends AbstractMockServerSpec<TestMockS
8888
@Override
8989
protected WebHttpHandlerBuilder initHttpHandlerBuilder() {
9090
return WebHttpHandlerBuilder.webHandler(exchange -> {
91-
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
91+
DefaultDataBufferFactory factory = DefaultDataBufferFactory.sharedInstance;
9292
String text = exchange.getAttributes().toString();
9393
DataBuffer buffer = factory.wrap(text.getBytes(StandardCharsets.UTF_8));
9494
return exchange.getResponse().writeWith(Mono.just(buffer));

spring-test/src/test/java/org/springframework/test/web/reactive/server/MockServerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -161,7 +161,7 @@ public void responseBodyContentWithFluxExchangeResult() {
161161

162162
private DataBuffer toDataBuffer(String value) {
163163
byte[] bytes = value.getBytes(UTF_8);
164-
return new DefaultDataBufferFactory().wrap(bytes);
164+
return DefaultDataBufferFactory.sharedInstance.wrap(bytes);
165165
}
166166

167167
}

spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/WebFilterTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -36,7 +36,7 @@ public class WebFilterTests {
3636
public void testWebFilter() throws Exception {
3737

3838
WebFilter filter = (exchange, chain) -> {
39-
DataBuffer buffer = new DefaultDataBufferFactory().allocateBuffer();
39+
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.allocateBuffer();
4040
buffer.write("It works!".getBytes(StandardCharsets.UTF_8));
4141
return exchange.getResponse().writeWith(Mono.just(buffer));
4242
};

0 commit comments

Comments
 (0)