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
}

0 commit comments

Comments
 (0)