org.springframework.core.io.buffer.PooledDataBuffer Java Examples

The following examples show how to use org.springframework.core.io.buffer.PooledDataBuffer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RSocketBufferLeakTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
void checkForLeaks(Duration duration) throws InterruptedException {
	Instant start = Instant.now();
	while (true) {
		try {
			this.created.forEach(info -> {
				if (((PooledDataBuffer) info.getDataBuffer()).isAllocated()) {
					throw info.getError();
				}
			});
			break;
		}
		catch (AssertionError ex) {
			if (Instant.now().isAfter(start.plus(duration))) {
				throw ex;
			}
		}
		Thread.sleep(50);
	}
}
 
Example #2
Source File: MultipartHttpMessageWriter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private Mono<Void> writeMultipart(
		MultiValueMap<String, ?> map, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints) {

	byte[] boundary = generateMultipartBoundary();

	Map<String, String> params = new HashMap<>(2);
	params.put("boundary", new String(boundary, StandardCharsets.US_ASCII));
	params.put("charset", getCharset().name());

	outputMessage.getHeaders().setContentType(new MediaType(MediaType.MULTIPART_FORM_DATA, params));

	LogFormatUtils.traceDebug(logger, traceOn -> Hints.getLogPrefix(hints) + "Encoding " +
			(isEnableLoggingRequestDetails() ?
					LogFormatUtils.formatValue(map, !traceOn) :
					"parts " + map.keySet() + " (content masked)"));

	DataBufferFactory bufferFactory = outputMessage.bufferFactory();

	Flux<DataBuffer> body = Flux.fromIterable(map.entrySet())
			.concatMap(entry -> encodePartValues(boundary, entry.getKey(), entry.getValue(), bufferFactory))
			.concatWith(generateLastLine(boundary, bufferFactory))
			.doOnDiscard(PooledDataBuffer.class, PooledDataBuffer::release);

	return outputMessage.writeWith(body);
}
 
Example #3
Source File: DefaultMultipartMessageReader.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public Flux<Part> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
	byte[] boundary = boundary(message);
	if (boundary == null) {
		return Flux.error(new CodecException("No multipart boundary found in Content-Type: \"" +
				message.getHeaders().getContentType() + "\""));
	}
	if (logger.isTraceEnabled()) {
		logger.trace("Boundary: " + toString(boundary));
	}

	byte[] boundaryNeedle = concat(BOUNDARY_PREFIX, boundary);
	Flux<DataBuffer> body = skipUntilFirstBoundary(message.getBody(), boundary);

	return DataBufferUtils.split(body, boundaryNeedle)
			.takeWhile(DefaultMultipartMessageReader::notLastBoundary)
			.map(DefaultMultipartMessageReader::toPart)
			.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release)
			.doOnDiscard(DefaultPart.class, part -> DataBufferUtils.release(part.body));
}
 
Example #4
Source File: AbstractSingleValueEncoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public final Flux<DataBuffer> encode(Publisher<? extends T> inputStream, DataBufferFactory bufferFactory,
		ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

	return Flux.from(inputStream)
			.take(1)
			.concatMap(value -> encode(value, bufferFactory, elementType, mimeType, hints))
			.doOnDiscard(PooledDataBuffer.class, PooledDataBuffer::release);
}
 
Example #5
Source File: StringDecoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Flux<String> decode(Publisher<DataBuffer> input, ResolvableType elementType,
		@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

	List<byte[]> delimiterBytes = getDelimiterBytes(mimeType);

	Flux<DataBuffer> inputFlux = Flux.from(input)
			.flatMapIterable(buffer -> splitOnDelimiter(buffer, delimiterBytes))
			.bufferUntil(buffer -> buffer == END_FRAME)
			.map(StringDecoder::joinUntilEndFrame)
			.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);

	return super.decode(inputFlux, elementType, mimeType, hints);
}
 
Example #6
Source File: JettyClientHttpRequest.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
	Flux<ContentChunk> chunks = Flux.from(body)
			.flatMap(Function.identity())
			.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release)
			.map(this::toContentChunk);
	ReactiveRequest.Content content = ReactiveRequest.Content.fromPublisher(chunks, getContentType());
	this.reactiveRequest = ReactiveRequest.newBuilder(this.jettyRequest).content(content).build();
	return doCommit(this::completes);
}
 
Example #7
Source File: AbstractServerHttpResponse.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public final Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
	// Write as Mono if possible as an optimization hint to Reactor Netty
	// ChannelSendOperator not necessary for Mono
	if (body instanceof Mono) {
		return ((Mono<? extends DataBuffer>) body).flatMap(buffer ->
				doCommit(() -> writeWithInternal(Mono.just(buffer)))
						.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release));
	}
	return new ChannelSendOperator<>(body, inner -> doCommit(() -> writeWithInternal(inner)))
			.doOnError(t -> removeContentLength());
}
 
Example #8
Source File: EncoderHttpMessageWriter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType elementType,
		@Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {

	MediaType contentType = updateContentType(message, mediaType);

	Flux<DataBuffer> body = this.encoder.encode(
			inputStream, message.bufferFactory(), elementType, contentType, hints);

	if (inputStream instanceof Mono) {
		HttpHeaders headers = message.getHeaders();
		return Mono.from(body)
				.switchIfEmpty(Mono.defer(() -> {
					headers.setContentLength(0);
					return message.setComplete().then(Mono.empty());
				}))
				.flatMap(buffer -> {
					headers.setContentLength(buffer.readableByteCount());
					return message.writeWith(Mono.just(buffer)
							.doOnDiscard(PooledDataBuffer.class, PooledDataBuffer::release));
				});
	}

	if (isStreamingMediaType(contentType)) {
		return message.writeAndFlushWith(body.map(buffer ->
				Mono.just(buffer).doOnDiscard(PooledDataBuffer.class, PooledDataBuffer::release)));
	}

	return message.writeWith(body);
}
 
Example #9
Source File: StringDecoder.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Flux<String> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
		@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

	List<byte[]> delimiterBytes = getDelimiterBytes(mimeType);

	Flux<DataBuffer> inputFlux = Flux.from(inputStream)
			.flatMapIterable(dataBuffer -> splitOnDelimiter(dataBuffer, delimiterBytes))
			.bufferUntil(StringDecoder::isEndFrame)
			.map(StringDecoder::joinUntilEndFrame)
			.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);

	return super.decode(inputFlux, elementType, mimeType, hints);
}
 
Example #10
Source File: JettyClientHttpRequest.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
	Flux<ContentChunk> chunks = Flux.from(body)
			.flatMap(Function.identity())
			.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release)
			.map(this::toContentChunk);
	ReactiveRequest.Content content = ReactiveRequest.Content.fromPublisher(chunks, getContentType());
	this.reactiveRequest = ReactiveRequest.newBuilder(this.jettyRequest).content(content).build();
	return doCommit(this::completes);
}
 
Example #11
Source File: RemoveCachedBodyFilter.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
	return chain.filter(exchange).doFinally(s -> {
		PooledDataBuffer dataBuffer = (PooledDataBuffer) exchange.getAttributes()
				.remove(CACHED_REQUEST_BODY_ATTR);
		if (dataBuffer != null && dataBuffer.isAllocated()) {
			if (log.isTraceEnabled()) {
				log.trace("releasing cached body in exchange attribute");
			}
			dataBuffer.release();
		}
	});
}
 
Example #12
Source File: UndertowServerHttpRequest.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public PooledDataBuffer retain() {
	this.refCount.incrementAndGet();
	DataBufferUtils.retain(this.dataBuffer);
	return this;
}
 
Example #13
Source File: UndertowServerHttpRequest.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public PooledDataBuffer retain() {
	return this;
}
 
Example #14
Source File: DataBufferFactoryWrapper.java    From armeria with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a {@link PooledDataBuffer} which will be released after consuming by the consumer.
 * Currently, the {@link NettyDataBuffer} is only one implementation of the {@link PooledDataBuffer}
 * which is exposed to the public API.
 */
private PooledDataBuffer withNettyDataBufferFactory(PooledHttpData data) {
    return ((NettyDataBufferFactory) delegate).wrap(data.content());
}