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

@@ -271,7 +271,7 @@ public final class StringDecoder extends AbstractDataBufferDecoder<String> {
private static class EndFrameBuffer extends DataBufferWrapper {
private static final DataBuffer BUFFER = new DefaultDataBufferFactory().wrap(new byte[0]);
private static final DataBuffer BUFFER = DefaultDataBufferFactory.sharedInstance.wrap(new byte[0]);
private byte[] delimiter;

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.
@@ -38,6 +38,12 @@ public class DefaultDataBufferFactory implements DataBufferFactory {
*/
public static final int DEFAULT_INITIAL_CAPACITY = 256;
/**
* Shared instance based on the default constructor.
* @since 5.3
*/
public static final DefaultDataBufferFactory sharedInstance = new DefaultDataBufferFactory();
private final boolean preferDirect;
@@ -46,6 +52,7 @@ public class DefaultDataBufferFactory implements DataBufferFactory {
/**
* Creates a new {@code DefaultDataBufferFactory} with default settings.
* @see #sharedInstance
*/
public DefaultDataBufferFactory() {
this(false);

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.
@@ -27,9 +27,6 @@ import org.junit.jupiter.api.Test;
*/
public class LimitedDataBufferListTests {
private final static DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
@Test
void limitEnforced() {
Assertions.assertThatThrownBy(() -> new LimitedDataBufferList(5).add(toDataBuffer("123456")))
@@ -51,7 +48,8 @@ public class LimitedDataBufferListTests {
private static DataBuffer toDataBuffer(String value) {
return bufferFactory.wrap(value.getBytes(StandardCharsets.UTF_8));
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
return DefaultDataBufferFactory.sharedInstance.wrap(bytes);
}
}

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.
@@ -73,8 +73,6 @@ public abstract class AbstractEncoderMethodReturnValueHandler implements Handler
private final ReactiveAdapterRegistry adapterRegistry;
private DataBufferFactory defaultBufferFactory = new DefaultDataBufferFactory();
protected AbstractEncoderMethodReturnValueHandler(List<Encoder<?>> encoders, ReactiveAdapterRegistry registry) {
Assert.notEmpty(encoders, "At least one Encoder is required");
@@ -114,7 +112,8 @@ public abstract class AbstractEncoderMethodReturnValueHandler implements Handler
}
DataBufferFactory bufferFactory = (DataBufferFactory) message.getHeaders()
.getOrDefault(HandlerMethodReturnValueHandler.DATA_BUFFER_FACTORY_HEADER, this.defaultBufferFactory);
.getOrDefault(HandlerMethodReturnValueHandler.DATA_BUFFER_FACTORY_HEADER,
DefaultDataBufferFactory.sharedInstance);
MimeType mimeType = (MimeType) message.getHeaders().get(MessageHeaders.CONTENT_TYPE);
Flux<DataBuffer> encodedContent = encodeContent(

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.
@@ -35,7 +35,6 @@ import org.springframework.core.codec.StringDecoder;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.DestinationPatternsMessageCondition;
@@ -60,9 +59,6 @@ import static org.assertj.core.api.Assertions.assertThat;
@SuppressWarnings("ALL")
public class MessageMappingMessageHandlerTests {
private static final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
private TestEncoderMethodReturnValueHandler returnValueHandler;
@@ -163,7 +159,7 @@ public class MessageMappingMessageHandlerTests {
}
private DataBuffer toDataBuffer(String payload) {
return bufferFactory.wrap(payload.getBytes(UTF_8));
return DefaultDataBufferFactory.sharedInstance.wrap(payload.getBytes(UTF_8));
}
private void verifyOutputContent(List<String> expected) {

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.
@@ -154,7 +154,7 @@ public class PayloadMethodArgumentResolverTests {
private DataBuffer toDataBuffer(String value) {
return new DefaultDataBufferFactory().wrap(value.getBytes(StandardCharsets.UTF_8));
return DefaultDataBufferFactory.sharedInstance.wrap(value.getBytes(StandardCharsets.UTF_8));
}

View File

@@ -232,7 +232,7 @@ public class DefaultRSocketRequesterBuilderTests {
@Test
public void frameDecoderMatchesDataBufferFactory() throws Exception {
testPayloadDecoder(new NettyDataBufferFactory(ByteBufAllocator.DEFAULT), PayloadDecoder.ZERO_COPY);
testPayloadDecoder(new DefaultDataBufferFactory(), PayloadDecoder.DEFAULT);
testPayloadDecoder(DefaultDataBufferFactory.sharedInstance, PayloadDecoder.DEFAULT);
}
private void testPayloadDecoder(DataBufferFactory bufferFactory, PayloadDecoder payloadDecoder)

View File

@@ -65,8 +65,6 @@ public class DefaultRSocketRequesterTests {
private final RSocketStrategies strategies = RSocketStrategies.create();
private final DefaultDataBufferFactory bufferFactory = new DefaultDataBufferFactory();
@BeforeEach
public void setUp() {
@@ -244,7 +242,8 @@ public class DefaultRSocketRequesterTests {
}
private Payload toPayload(String value) {
return PayloadUtils.createPayload(bufferFactory.wrap(value.getBytes(StandardCharsets.UTF_8)));
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
return PayloadUtils.createPayload(DefaultDataBufferFactory.sharedInstance.wrap(bytes));
}

View File

@@ -203,7 +203,7 @@ public class MetadataEncoderTests {
@Test
public void defaultDataBufferFactory() {
DefaultDataBufferFactory bufferFactory = new DefaultDataBufferFactory();
DefaultDataBufferFactory bufferFactory = DefaultDataBufferFactory.sharedInstance;
RSocketStrategies strategies = RSocketStrategies.builder().dataBufferFactory(bufferFactory).build();
DataBuffer buffer = new MetadataEncoder(COMPOSITE_METADATA, strategies)

View File

@@ -44,8 +44,6 @@ public class PayloadUtilsTests {
private LeakAwareNettyDataBufferFactory nettyBufferFactory =
new LeakAwareNettyDataBufferFactory(PooledByteBufAllocator.DEFAULT);
private DefaultDataBufferFactory defaultBufferFactory = new DefaultDataBufferFactory();
@AfterEach
public void tearDown() throws Exception {
@@ -70,7 +68,7 @@ public class PayloadUtilsTests {
@Test
public void retainAndReleaseWithDefaultFactory() {
Payload payload = ByteBufPayload.create("sample data");
DataBuffer buffer = PayloadUtils.retainDataAndReleasePayload(payload, this.defaultBufferFactory);
DataBuffer buffer = PayloadUtils.retainDataAndReleasePayload(payload, DefaultDataBufferFactory.sharedInstance);
assertThat(buffer).isInstanceOf(DefaultDataBuffer.class);
assertThat(payload.refCnt()).isEqualTo(0);
@@ -163,7 +161,7 @@ public class PayloadUtilsTests {
}
private DefaultDataBuffer createDefaultDataBuffer(String content) {
DefaultDataBuffer buffer = this.defaultBufferFactory.allocateBuffer();
DefaultDataBuffer buffer = DefaultDataBufferFactory.sharedInstance.allocateBuffer();
buffer.write(content, StandardCharsets.UTF_8);
return buffer;
}

View File

@@ -74,7 +74,7 @@ public class ResourceDatabasePopulator implements DatabasePopulator {
private boolean ignoreFailedDrops = false;
private DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
private DataBufferFactory dataBufferFactory = DefaultDataBufferFactory.sharedInstance;
/**

View File

@@ -512,8 +512,9 @@ public abstract class ScriptUtils {
* @see org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection
*/
public static Mono<Void> executeSqlScript(Connection connection, EncodedResource resource) throws ScriptException {
return executeSqlScript(connection, resource, new DefaultDataBufferFactory(), false, false, DEFAULT_COMMENT_PREFIX,
DEFAULT_STATEMENT_SEPARATOR, DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER);
return executeSqlScript(connection, resource, DefaultDataBufferFactory.sharedInstance, false, false,
DEFAULT_COMMENT_PREFIX, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_BLOCK_COMMENT_START_DELIMITER,
DEFAULT_BLOCK_COMMENT_END_DELIMITER);
}
/**

View File

@@ -213,7 +213,7 @@ public class ScriptUtilsUnitTests {
private String readScript(String path) {
EncodedResource resource = new EncodedResource(
new ClassPathResource(path, getClass()));
return ScriptUtils.readScript(resource, new DefaultDataBufferFactory()).block();
return ScriptUtils.readScript(resource, DefaultDataBufferFactory.sharedInstance).block();
}
}

View File

@@ -52,8 +52,6 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
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?"));
@@ -103,7 +101,7 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
@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) {

View File

@@ -29,7 +29,6 @@ import reactor.core.publisher.Mono;
import reactor.core.publisher.MonoProcessor;
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.HttpMethod;
import org.springframework.http.client.reactive.ClientHttpConnector;
@@ -125,16 +124,13 @@ class WiretapConnector implements ClientHttpConnector {
*/
final static class WiretapRecorder {
private static final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
@Nullable
private final Flux<? extends DataBuffer> publisher;
@Nullable
private final Flux<? extends Publisher<? extends DataBuffer>> publisherNested;
private final DataBuffer buffer = bufferFactory.allocateBuffer();
private final DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.allocateBuffer();
private final MonoProcessor<byte[]> content = MonoProcessor.create();

View File

@@ -121,7 +121,7 @@ public class HttpHandlerConnectorTests {
}
private DataBuffer toDataBuffer(String body) {
return new DefaultDataBufferFactory().wrap(body.getBytes(UTF_8));
return DefaultDataBufferFactory.sharedInstance.wrap(body.getBytes(UTF_8));
}

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.
@@ -88,7 +88,7 @@ public class MockServerSpecTests {
@Override
protected WebHttpHandlerBuilder initHttpHandlerBuilder() {
return WebHttpHandlerBuilder.webHandler(exchange -> {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBufferFactory factory = DefaultDataBufferFactory.sharedInstance;
String text = exchange.getAttributes().toString();
DataBuffer buffer = factory.wrap(text.getBytes(StandardCharsets.UTF_8));
return exchange.getResponse().writeWith(Mono.just(buffer));

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.
@@ -161,7 +161,7 @@ public class MockServerTests {
private DataBuffer toDataBuffer(String value) {
byte[] bytes = value.getBytes(UTF_8);
return new DefaultDataBufferFactory().wrap(bytes);
return DefaultDataBufferFactory.sharedInstance.wrap(bytes);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 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.
@@ -36,7 +36,7 @@ public class WebFilterTests {
public void testWebFilter() throws Exception {
WebFilter filter = (exchange, chain) -> {
DataBuffer buffer = new DefaultDataBufferFactory().allocateBuffer();
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.allocateBuffer();
buffer.write("It works!".getBytes(StandardCharsets.UTF_8));
return exchange.getResponse().writeWith(Mono.just(buffer));
};

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) {

View File

@@ -186,7 +186,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
this.body = Flux.just(body).
map(s -> {
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
return new DefaultDataBufferFactory().wrap(bytes);
return DefaultDataBufferFactory.sharedInstance.wrap(bytes);
});
return this;
}

View File

@@ -36,7 +36,6 @@ import org.springframework.context.i18n.LocaleContext;
import org.springframework.core.ResolvableType;
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.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpCookie;
@@ -151,11 +150,10 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
public ServerRequest.Builder body(String body) {
Assert.notNull(body, "Body must not be null");
releaseBody();
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
this.body = Flux.just(body).
map(s -> {
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
return dataBufferFactory.wrap(bytes);
return DefaultDataBufferFactory.sharedInstance.wrap(bytes);
});
return this;
}

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.
@@ -21,7 +21,6 @@ import reactor.core.publisher.Mono;
import org.springframework.core.io.Resource;
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.util.DigestUtils;
@@ -39,13 +38,12 @@ import org.springframework.util.StreamUtils;
*/
public class ContentVersionStrategy extends AbstractFileNameVersionStrategy {
private static final DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
@Override
public Mono<String> getResourceVersion(Resource resource) {
Flux<DataBuffer> flux =
DataBufferUtils.read(resource, dataBufferFactory, StreamUtils.BUFFER_SIZE);
Flux<DataBuffer> flux = DataBufferUtils.read(
resource, DefaultDataBufferFactory.sharedInstance, StreamUtils.BUFFER_SIZE);
return DataBufferUtils.join(flux)
.map(buffer -> {
byte[] result = new byte[buffer.readableByteCount()];

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.
@@ -29,7 +29,6 @@ import reactor.core.publisher.Mono;
import reactor.core.publisher.MonoProcessor;
import org.springframework.context.Lifecycle;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.web.reactive.socket.HandshakeInfo;
@@ -59,8 +58,6 @@ public class JettyWebSocketClient implements WebSocketClient, Lifecycle {
private final boolean externallyManaged;
private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
/**
* Default constructor that creates and manages an instance of a Jetty
@@ -157,7 +154,8 @@ public class JettyWebSocketClient implements WebSocketClient, Lifecycle {
private Object createHandler(URI url, WebSocketHandler handler, MonoProcessor<Void> completion) {
return new JettyWebSocketHandlerAdapter(handler, session -> {
HandshakeInfo info = createHandshakeInfo(url, session);
return new JettyWebSocketSession(session, info, this.bufferFactory, completion);
return new JettyWebSocketSession(
session, info, DefaultDataBufferFactory.sharedInstance, completion);
});
}

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.
@@ -55,8 +55,6 @@ public class StandardWebSocketClient implements WebSocketClient {
private static final Log logger = LogFactory.getLog(StandardWebSocketClient.class);
private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
private final WebSocketContainer webSocketContainer;
@@ -129,7 +127,7 @@ public class StandardWebSocketClient implements WebSocketClient {
protected StandardWebSocketSession createWebSocketSession(Session session, HandshakeInfo info,
MonoProcessor<Void> completion) {
return new StandardWebSocketSession(session, info, this.bufferFactory, completion);
return new StandardWebSocketSession(session, info, DefaultDataBufferFactory.sharedInstance, completion);
}
private ClientEndpointConfig createEndpointConfig(Configurator configurator, List<String> subProtocols) {
@@ -140,7 +138,7 @@ public class StandardWebSocketClient implements WebSocketClient {
}
protected DataBufferFactory bufferFactory() {
return this.bufferFactory;
return DefaultDataBufferFactory.sharedInstance;
}

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.
@@ -65,8 +65,6 @@ public class UndertowWebSocketClient implements WebSocketClient {
private final Consumer<ConnectionBuilder> builderConsumer;
private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
/**
* Constructor with the {@link XnioWorker} to pass to
@@ -198,7 +196,8 @@ public class UndertowWebSocketClient implements WebSocketClient {
DefaultNegotiation negotiation, WebSocketChannel channel) {
HandshakeInfo info = createHandshakeInfo(url, negotiation);
UndertowWebSocketSession session = new UndertowWebSocketSession(channel, info, this.bufferFactory, completion);
DataBufferFactory bufferFactory = DefaultDataBufferFactory.sharedInstance;
UndertowWebSocketSession session = new UndertowWebSocketSession(channel, info, bufferFactory, completion);
UndertowWebSocketHandlerAdapter adapter = new UndertowWebSocketHandlerAdapter(session);
channel.getReceiveSetter().set(adapter);

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.
@@ -102,7 +102,7 @@ public class DispatcherHandlerTests {
@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
byte[] bytes = ((String) result.getReturnValue()).getBytes(StandardCharsets.UTF_8);
DataBuffer dataBuffer = new DefaultDataBufferFactory().wrap(bytes);
DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
return exchange.getResponse().writeWith(Mono.just(dataBuffer));
}
}

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.
@@ -119,7 +119,7 @@ public class BodyExtractorsTests {
public void toMono() {
BodyExtractor<Mono<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(String.class);
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBufferFactory factory = DefaultDataBufferFactory.sharedInstance;
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
Flux<DataBuffer> body = Flux.just(dataBuffer);
@@ -138,9 +138,8 @@ public class BodyExtractorsTests {
BodyExtractor<Mono<Map<String, String>>, ReactiveHttpInputMessage> extractor =
BodyExtractors.toMono(new ParameterizedTypeReference<Map<String, String>>() {});
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("{\"username\":\"foo\",\"password\":\"bar\"}".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "{\"username\":\"foo\",\"password\":\"bar\"}".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_JSON).body(body);
@@ -160,9 +159,8 @@ public class BodyExtractorsTests {
BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("{\"username\":\"foo\",\"password\":\"bar\"}".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "{\"username\":\"foo\",\"password\":\"bar\"}".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
MockServerHttpRequest request = MockServerHttpRequest.post("/")
@@ -193,9 +191,8 @@ public class BodyExtractorsTests {
@Test
public void toMonoVoidAsClientShouldConsumeAndCancel() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
TestPublisher<DataBuffer> body = TestPublisher.create();
BodyExtractor<Mono<Void>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(Void.class);
@@ -232,9 +229,8 @@ public class BodyExtractorsTests {
public void toFlux() {
BodyExtractor<Flux<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(String.class);
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
@@ -251,9 +247,9 @@ public class BodyExtractorsTests {
BodyExtractor<Flux<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(User.class);
this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
String text = "[{\"username\":\"foo\",\"password\":\"bar\"},{\"username\":\"bar\",\"password\":\"baz\"}]";
DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap(text.getBytes(StandardCharsets.UTF_8)));
byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
MockServerHttpRequest request = MockServerHttpRequest.post("/")
@@ -279,9 +275,8 @@ public class BodyExtractorsTests {
public void toFluxUnacceptable() {
BodyExtractor<Flux<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(String.class);
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
MockServerHttpRequest request = MockServerHttpRequest.post("/")
@@ -313,9 +308,8 @@ public class BodyExtractorsTests {
@Test
public void toFormData() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
String text = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3";
DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap(text.getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
MockServerHttpRequest request = MockServerHttpRequest.post("/")
@@ -360,9 +354,8 @@ public class BodyExtractorsTests {
"\r\n" +
"-----------------------------9051914041544843365972754266--\r\n";
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap(bodyContents.getBytes(StandardCharsets.UTF_8)));
byte[] bytes = bodyContents.getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
MockServerHttpRequest request = MockServerHttpRequest.post("/")
@@ -403,9 +396,8 @@ public class BodyExtractorsTests {
public void toDataBuffers() {
BodyExtractor<Flux<DataBuffer>, ReactiveHttpInputMessage> extractor = BodyExtractors.toDataBuffers();
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);

View File

@@ -421,9 +421,8 @@ public class BodyInsertersTests {
@Test
public void ofDataBuffers() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
BodyInserter<Flux<DataBuffer>, ReactiveHttpOutputMessage> inserter = BodyInserters.fromDataBuffers(body);

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.
@@ -118,7 +118,7 @@ public class DefaultClientRequestBuilderTests {
BodyInserter<String, ClientHttpRequest> inserter =
(response, strategies) -> {
byte[] bodyBytes = body.getBytes(UTF_8);
DataBuffer buffer = new DefaultDataBufferFactory().wrap(bodyBytes);
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bodyBytes);
return response.writeWith(Mono.just(buffer));
};

View File

@@ -23,7 +23,6 @@ import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
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.HttpHeaders;
import org.springframework.http.HttpMethod;
@@ -41,14 +40,11 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
*/
public class DefaultClientResponseBuilderTests {
private final DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
@Test
public void normal() {
Flux<DataBuffer> body = Flux.just("baz")
.map(s -> s.getBytes(StandardCharsets.UTF_8))
.map(dataBufferFactory::wrap);
.map(DefaultDataBufferFactory.sharedInstance::wrap);
ClientResponse response = ClientResponse.create(HttpStatus.BAD_GATEWAY, ExchangeStrategies.withDefaults())
.header("foo", "bar")
@@ -71,7 +67,7 @@ public class DefaultClientResponseBuilderTests {
public void mutate() {
Flux<DataBuffer> otherBody = Flux.just("foo", "bar")
.map(s -> s.getBytes(StandardCharsets.UTF_8))
.map(dataBufferFactory::wrap);
.map(DefaultDataBufferFactory.sharedInstance::wrap);
HttpRequest mockClientHttpRequest = new MockClientHttpRequest(HttpMethod.GET, "/path");

View File

@@ -125,9 +125,8 @@ public class DefaultClientResponseTests {
@Test
public void body() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
mockTextPlainResponse(body);
@@ -141,9 +140,8 @@ public class DefaultClientResponseTests {
@Test
public void bodyToMono() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
mockTextPlainResponse(body);
@@ -157,9 +155,8 @@ public class DefaultClientResponseTests {
@Test
public void bodyToMonoTypeReference() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
mockTextPlainResponse(body);
@@ -175,9 +172,8 @@ public class DefaultClientResponseTests {
@Test
public void bodyToFlux() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
mockTextPlainResponse(body);
@@ -192,9 +188,8 @@ public class DefaultClientResponseTests {
@Test
public void bodyToFluxTypeReference() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
mockTextPlainResponse(body);
@@ -211,9 +206,8 @@ public class DefaultClientResponseTests {
@Test
public void toEntity() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
mockTextPlainResponse(body);
@@ -230,9 +224,8 @@ public class DefaultClientResponseTests {
@Test
public void toEntityWithUnknownStatusCode() throws Exception {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer
= factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
httpHeaders.setContentType(MediaType.TEXT_PLAIN);
@@ -255,9 +248,8 @@ public class DefaultClientResponseTests {
@Test
public void toEntityTypeReference() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
mockTextPlainResponse(body);
@@ -276,9 +268,8 @@ public class DefaultClientResponseTests {
@Test
public void toEntityList() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
mockTextPlainResponse(body);
@@ -295,9 +286,8 @@ public class DefaultClientResponseTests {
@Test
public void toEntityListWithUnknownStatusCode() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
httpHeaders.setContentType(MediaType.TEXT_PLAIN);
@@ -320,8 +310,8 @@ public class DefaultClientResponseTests {
@Test
public void toEntityListTypeReference() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
mockTextPlainResponse(body);

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.
@@ -197,10 +197,9 @@ public class ExchangeFilterFunctionsTests {
@Test
public void limitResponseSize() {
DefaultDataBufferFactory bufferFactory = new DefaultDataBufferFactory();
DataBuffer b1 = dataBuffer("foo", bufferFactory);
DataBuffer b2 = dataBuffer("bar", bufferFactory);
DataBuffer b3 = dataBuffer("baz", bufferFactory);
DataBuffer b1 = dataBuffer("foo");
DataBuffer b2 = dataBuffer("bar");
DataBuffer b3 = dataBuffer("baz");
ClientRequest request = ClientRequest.create(HttpMethod.GET, DEFAULT_URL).build();
ClientResponse response = ClientResponse.create(HttpStatus.OK).body(Flux.just(b1, b2, b3)).build();
@@ -222,8 +221,8 @@ public class ExchangeFilterFunctionsTests {
return value;
}
private DataBuffer dataBuffer(String foo, DefaultDataBufferFactory bufferFactory) {
return bufferFactory.wrap(foo.getBytes(StandardCharsets.UTF_8));
private DataBuffer dataBuffer(String foo) {
return DefaultDataBufferFactory.sharedInstance.wrap(foo.getBytes(StandardCharsets.UTF_8));
}

View File

@@ -627,7 +627,7 @@ class WebClientIntegrationTests {
prepareResponse(response -> {});
Resource resource = new ClassPathResource("largeTextFile.txt", getClass());
Flux<DataBuffer> body = DataBufferUtils.read(resource, new DefaultDataBufferFactory(), 4096);
Flux<DataBuffer> body = DataBufferUtils.read(resource, DefaultDataBufferFactory.sharedInstance, 4096);
Mono<Void> result = this.webClient.post()
.uri("/")

View File

@@ -23,7 +23,6 @@ import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
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.HttpMethod;
import org.springframework.http.ResponseCookie;
@@ -42,9 +41,6 @@ import static org.assertj.core.api.Assertions.entry;
*/
public class DefaultServerRequestBuilderTests {
private final DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
@Test
public void from() {
MockServerHttpRequest request = MockServerHttpRequest.post("https://example.com")
@@ -58,7 +54,7 @@ public class DefaultServerRequestBuilderTests {
Flux<DataBuffer> body = Flux.just("baz")
.map(s -> s.getBytes(StandardCharsets.UTF_8))
.map(dataBufferFactory::wrap);
.map(DefaultDataBufferFactory.sharedInstance::wrap);
ServerRequest result = ServerRequest.from(other)
.method(HttpMethod.HEAD)

View File

@@ -239,9 +239,8 @@ public class DefaultServerRequestTests {
@Test
public void body() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();
@@ -259,9 +258,8 @@ public class DefaultServerRequestTests {
@Test
public void bodyToMono() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();
@@ -278,9 +276,8 @@ public class DefaultServerRequestTests {
@Test
public void bodyToMonoParameterizedTypeReference() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();
@@ -298,9 +295,8 @@ public class DefaultServerRequestTests {
@Test
public void bodyToMonoDecodingException() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("{\"invalid\":\"json\" ".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "{\"invalid\":\"json\" ".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();
@@ -320,9 +316,8 @@ public class DefaultServerRequestTests {
@Test
public void bodyToFlux() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();
@@ -339,9 +334,8 @@ public class DefaultServerRequestTests {
@Test
public void bodyToFluxParameterizedTypeReference() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();
@@ -359,9 +353,8 @@ public class DefaultServerRequestTests {
@Test
public void bodyUnacceptable() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();
@@ -380,9 +373,8 @@ public class DefaultServerRequestTests {
@Test
public void formData() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("foo=bar&baz=qux".getBytes(StandardCharsets.UTF_8)));
byte[] bytes = "foo=bar&baz=qux".getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();
@@ -414,9 +406,8 @@ public class DefaultServerRequestTests {
"\r\n" +
"qux\r\n" +
"--12345--\r\n";
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap(data.getBytes(StandardCharsets.UTF_8)));
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();

View File

@@ -35,7 +35,6 @@ import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
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.CacheControl;
@@ -71,12 +70,8 @@ public class ResourceWebHandlerTests {
private static final Duration TIMEOUT = Duration.ofSeconds(1);
private ResourceWebHandler handler;
private DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
@BeforeEach
public void setup() throws Exception {
List<Resource> locations = new ArrayList<>(2);
@@ -596,7 +591,7 @@ public class ResourceWebHandlerTests {
String boundary = "--" + exchange.getResponse().getHeaders().getContentType().toString().substring(30);
Mono<DataBuffer> reduced = Flux.from(exchange.getResponse().getBody())
.reduce(this.bufferFactory.allocateBuffer(), (previous, current) -> {
.reduce(DefaultDataBufferFactory.sharedInstance.allocateBuffer(), (previous, current) -> {
previous.write(current);
DataBufferUtils.release(current);
return previous;

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 @@ class SimpleUrlHandlerMappingIntegrationTests extends AbstractHttpHandlerIntegra
}
private static DataBuffer asDataBuffer(String text) {
DefaultDataBuffer buffer = new DefaultDataBufferFactory().allocateBuffer();
DefaultDataBuffer buffer = DefaultDataBufferFactory.sharedInstance.allocateBuffer();
return buffer.write(text.getBytes(StandardCharsets.UTF_8));
}

View File

@@ -292,7 +292,8 @@ public class InvocableHandlerMethodTests {
}
private Flux<DataBuffer> getBody(String body) {
return Flux.just(new DefaultDataBufferFactory().wrap(body.getBytes(StandardCharsets.UTF_8)));
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
return Flux.just(DefaultDataBufferFactory.sharedInstance.wrap(bytes));
}
}

View File

@@ -44,7 +44,6 @@ import org.springframework.core.ResolvableType;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
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.HttpHeaders;
import org.springframework.http.HttpStatus;
@@ -487,9 +486,8 @@ public class RequestMappingMessageConversionIntegrationTests extends AbstractReq
@GetMapping("/publisher")
public Publisher<ByteBuffer> getPublisher() {
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
return encoder.encode(Mono.just(new Person("Robert")), dataBufferFactory,
return encoder.encode(Mono.just(new Person("Robert")), DefaultDataBufferFactory.sharedInstance,
ResolvableType.forClass(Person.class), JSON, Collections.emptyMap()).map(DataBuffer::asByteBuffer);
}

View File

@@ -398,7 +398,7 @@ public class ViewResolutionResultHandlerTests {
model = new TreeMap<>(model);
String value = this.name + ": " + model.toString();
ByteBuffer byteBuffer = ByteBuffer.wrap(value.getBytes(UTF_8));
DataBuffer dataBuffer = new DefaultDataBufferFactory().wrap(byteBuffer);
DataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(byteBuffer);
return response.writeWith(Flux.just(dataBuffer));
}
}