Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;
import java.util.function.Consumer;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import org.springframework.lang.Nullable;
/**
* Bridges between {@link OutputStream} and
* {@link Publisher Publisher<DataBuffer>}.
*
*
Note that this class has a near duplicate in
* {@link org.springframework.http.client.OutputStreamPublisher}.
*
* @author Oleh Dokuka
* @author Arjen Poutsma
* @since 6.1
*/
final class OutputStreamPublisher implements Publisher {
private final Consumer outputStreamConsumer;
private final DataBufferFactory bufferFactory;
private final Executor executor;
private final int chunkSize;
OutputStreamPublisher(Consumer outputStreamConsumer, DataBufferFactory bufferFactory,
Executor executor, int chunkSize) {
this.outputStreamConsumer = outputStreamConsumer;
this.bufferFactory = bufferFactory;
this.executor = executor;
this.chunkSize = chunkSize;
}
@Override
public void subscribe(Subscriber subscriber) {
Objects.requireNonNull(subscriber, "Subscriber must not be null");
OutputStreamSubscription subscription = new OutputStreamSubscription(
subscriber, this.outputStreamConsumer, this.bufferFactory, this.chunkSize);
subscriber.onSubscribe(subscription);
this.executor.execute(subscription::invokeHandler);
}
private static final class OutputStreamSubscription extends OutputStream implements Subscription {
private static final Object READY = new Object();
private final Subscriber actual;
private final Consumer outputStreamHandler;
private final DataBufferFactory bufferFactory;
private final int chunkSize;
private final AtomicLong requested = new AtomicLong();
private final AtomicReference