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.http.client;
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.Flow;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Bridges between {@link OutputStream} and {@link Flow.Publisher Flow.Publisher<T>}.
*
*
When there is demand on the Reactive Streams subscription, any write to
* the OutputStream is mapped to a buffer and published to the subscriber.
* If there is no demand, writes block until demand materializes.
* If the subscription is cancelled, further writes raise {@code IOException}.
*
*
Note that this class has a near duplicate in
* {@link org.springframework.core.io.buffer.OutputStreamPublisher}.
*
* @author Oleh Dokuka
* @author Arjen Poutsma
* @since 6.1
* @param the published byte buffer type
*/
final class OutputStreamPublisher implements Flow.Publisher {
private static final int DEFAULT_CHUNK_SIZE = 1024;
private final OutputStreamHandler outputStreamHandler;
private final ByteMapper byteMapper;
private final Executor executor;
private final int chunkSize;
/**
* Create an instance.
* @param outputStreamHandler invoked when the first buffer is requested
* @param byteMapper maps written bytes to {@code T}
* @param executor used to invoke the {@code outputStreamHandler}
* @param chunkSize the chunk sizes to be produced by the publisher
*/
OutputStreamPublisher(
OutputStreamHandler outputStreamHandler, ByteMapper byteMapper,
Executor executor, @Nullable Integer chunkSize) {
Assert.notNull(outputStreamHandler, "OutputStreamHandler must not be null");
Assert.notNull(byteMapper, "ByteMapper must not be null");
Assert.notNull(executor, "Executor must not be null");
Assert.isTrue(chunkSize == null || chunkSize > 0, "ChunkSize must be larger than 0");
this.outputStreamHandler = outputStreamHandler;
this.byteMapper = byteMapper;
this.executor = executor;
this.chunkSize = (chunkSize != null ? chunkSize : DEFAULT_CHUNK_SIZE);
}
@Override
public void subscribe(Flow.Subscriber super T> subscriber) {
// We don't use Assert.notNull(), because a NullPointerException is required
// for Reactive Streams compliance.
Objects.requireNonNull(subscriber, "Subscriber must not be null");
OutputStreamSubscription subscription = new OutputStreamSubscription<>(
subscriber, this.outputStreamHandler, this.byteMapper, this.chunkSize);
subscriber.onSubscribe(subscription);
this.executor.execute(subscription::invokeHandler);
}
/**
* 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 the type of byte buffer to map to
*/
public interface ByteMapper {
T map(int b);
T map(byte[] b, int off, int len);
}
private static final class OutputStreamSubscription extends OutputStream implements Flow.Subscription {
private static final Object READY = new Object();
private final Flow.Subscriber super T> actual;
private final OutputStreamHandler outputStreamHandler;
private final ByteMapper byteMapper;
private final int chunkSize;
private final AtomicLong requested = new AtomicLong();
private final AtomicReference