Shared static instance of DefaultDataBufferFactory

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -71,7 +71,7 @@ public class ServletHttpHandlerAdapter implements Servlet {
@Nullable
private String servletPath;
private DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory(false);
private DataBufferFactory dataBufferFactory = DefaultDataBufferFactory.sharedInstance;
public ServletHttpHandlerAdapter(HttpHandler httpHandler) {

View File

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

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -45,7 +45,7 @@ public class UndertowHttpHandlerAdapter implements io.undertow.server.HttpHandle
private final HttpHandler httpHandler;
private DataBufferFactory bufferFactory = new DefaultDataBufferFactory(false);
private DataBufferFactory bufferFactory = DefaultDataBufferFactory.sharedInstance;
public UndertowHttpHandlerAdapter(HttpHandler httpHandler) {