Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataBufferUtils and BodyInserters: allow consumers of OutputStream to throw IOException #33830

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -439,22 +439,22 @@ static void closeChannel(@Nullable Channel channel) {
* @since 6.1
*/
public static Publisher<DataBuffer> outputStreamPublisher(
Consumer<OutputStream> consumer, DataBufferFactory bufferFactory, Executor executor) {
OutputStreamHandler consumer, DataBufferFactory bufferFactory, Executor executor) {

return new OutputStreamPublisher<>(
consumer::accept, new DataBufferMapper(bufferFactory), executor, null);
consumer, new DataBufferMapper(bufferFactory), executor, null);
}

/**
* Variant of {@link #outputStreamPublisher(Consumer, DataBufferFactory, Executor)}
* Variant of {@link #outputStreamPublisher(OutputStreamHandler, DataBufferFactory, Executor)}
* providing control over the chunk sizes to be produced by the publisher.
* @since 6.1
*/
public static Publisher<DataBuffer> outputStreamPublisher(
Consumer<OutputStream> consumer, DataBufferFactory bufferFactory, Executor executor, int chunkSize) {
OutputStreamHandler consumer, DataBufferFactory bufferFactory, Executor executor, int chunkSize) {

return new OutputStreamPublisher<>(
consumer::accept, new DataBufferMapper(bufferFactory), executor, chunkSize);
consumer, new DataBufferMapper(bufferFactory), executor, chunkSize);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2002-2024 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.core.io.buffer;

import java.io.IOException;
import java.io.OutputStream;

/**
* Contract to provide callback access to the {@link OutputStream}.
* @author Rossen Stoyanchev
* @author Federico Pettinari
*/
@FunctionalInterface
public interface OutputStreamHandler {

void handle(OutputStream outputStream) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,6 @@ public void subscribe(Subscriber<? super T> subscriber) {
}


/**
* Contract to provide callback access to the {@link OutputStream}.
*/
@FunctionalInterface
public interface OutputStreamHandler {

void handle(OutputStream outputStream) throws Exception;

}


/**
* Maps from bytes to byte buffers.
* @param <T> the type of byte buffer to map to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.OutputStream;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.function.Consumer;

import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
Expand All @@ -32,6 +31,7 @@
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.OutputStreamHandler;
import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpOutputMessage;
Expand Down Expand Up @@ -397,10 +397,10 @@ public static <T extends Publisher<DataBuffer>> BodyInserter<T, ReactiveHttpOutp
* separate thread
* @return an inserter that writes what is written to the output stream
* @since 6.1
* @see DataBufferUtils#outputStreamPublisher(Consumer, DataBufferFactory, Executor)
* @see DataBufferUtils#outputStreamPublisher(OutputStreamHandler, DataBufferFactory, Executor)
*/
public static <T extends Publisher<DataBuffer>> BodyInserter<T, ReactiveHttpOutputMessage> fromOutputStream(
Consumer<OutputStream> outputStreamConsumer, Executor executor) {
OutputStreamHandler outputStreamConsumer, Executor executor) {

Assert.notNull(outputStreamConsumer, "OutputStreamConsumer must not be null");
Assert.notNull(executor, "Executor must not be null");
Expand All @@ -418,10 +418,10 @@ public static <T extends Publisher<DataBuffer>> BodyInserter<T, ReactiveHttpOutp
* @param chunkSize minimum size of the buffer produced by the publisher
* @return an inserter that writes what is written to the output stream
* @since 6.1
* @see DataBufferUtils#outputStreamPublisher(Consumer, DataBufferFactory, Executor, int)
* @see DataBufferUtils#outputStreamPublisher(OutputStreamHandler, DataBufferFactory, Executor, int)
*/
public static <T extends Publisher<DataBuffer>> BodyInserter<T, ReactiveHttpOutputMessage> fromOutputStream(
Consumer<OutputStream> outputStreamConsumer, Executor executor, int chunkSize) {
OutputStreamHandler outputStreamConsumer, Executor executor, int chunkSize) {

Assert.notNull(outputStreamConsumer, "OutputStreamConsumer must not be null");
Assert.notNull(executor, "Executor must not be null");
Expand Down