Top Description Inners Fields Constructors Methods
org.springframework.http.client

pack-priv final Class OutputStreamPublisher<T>

extends Object
implements Flow.Publisher<T>
Class Inheritance
All Implemented Interfaces
java.util.concurrent.Flow.Publisher
Type Parameters
<T>
the published item type
Imports
java.io.BufferedOutputStream, .IOException, .OutputStream, java.util.Objects, java.util.concurrent.Executor, .Flow, java.util.concurrent.atomic.AtomicLong, .AtomicReference, java.util.concurrent.locks.LockSupport, org.springframework.lang.Nullable, org.springframework.util.Assert

Bridges between OutputStream and Flow.Publisher<T>.

Note that this class has a near duplicate in org.springframework.core.io.buffer.OutputStreamPublisher.

Authors
Oleh Dokuka, Arjen Poutsma
Since
6.1
See Also
create(OutputStreamHandler, ByteMapper, Executor)

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
OutputStreamPublisher.ByteMapper<
the type to map to
T
>

Maps bytes written to in OutputStreamHandler#handle(OutputStream) to published items.

public static interface
OutputStreamPublisher.OutputStreamHandler

Defines the contract for handling the OutputStream provided by the OutputStreamPublisher.

private static class

Field Summary

Modifier and TypeField and Description
private final OutputStreamPublisher.ByteMapper<T>
private final int
private static final int
private final Executor
private final OutputStreamPublisher.OutputStreamHandler

Constructor Summary

AccessConstructor and Description
private
OutputStreamPublisher(OutputStreamPublisher.OutputStreamHandler outputStreamHandler, OutputStreamPublisher.ByteMapper<T> byteMapper, Executor executor, int chunkSize)

Method Summary

Modifier and TypeMethod and Description
public static <
the publisher type
T
>
Flow.Publisher<T>

Returns:

a Publisher<T> based on bytes written by outputStreamHandler mapped by byteMapper
create
(OutputStreamPublisher.OutputStreamHandler
invoked when the first buffer is requested
outputStreamHandler
,
OutputStreamPublisher.ByteMapper<T>
maps written bytes to T
byteMapper
,
Executor
used to invoke the outputStreamHandler
executor
)

Creates a new Publisher<T> based on bytes written to a OutputStream.

public static <
the publisher type
T
>
Flow.Publisher<T>

Returns:

a Publisher<T> based on bytes written by outputStreamHandler mapped by byteMapper
create
(OutputStreamPublisher.OutputStreamHandler
invoked when the first buffer is requested
outputStreamHandler
,
OutputStreamPublisher.ByteMapper<T>
maps written bytes to T
byteMapper
,
Executor
used to invoke the outputStreamHandler
executor
,
int chunkSize)

Creates a new Publisher<T> based on bytes written to a OutputStream.

public void
subscribe(Flow.Subscriber<? super T>
the subscriber
subscriber
)

Implements java.util.concurrent.Flow.Publisher.subscribe.

Adds the given Subscriber if possible.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

byteMapperback to summary
private final OutputStreamPublisher.ByteMapper<T> byteMapper
chunkSizeback to summary
private final int chunkSize
DEFAULT_CHUNK_SIZEback to summary
private static final int DEFAULT_CHUNK_SIZE
executorback to summary
private final Executor executor
outputStreamHandlerback to summary
private final OutputStreamPublisher.OutputStreamHandler outputStreamHandler

Constructor Detail

OutputStreamPublisherback to summary
private OutputStreamPublisher(OutputStreamPublisher.OutputStreamHandler outputStreamHandler, OutputStreamPublisher.ByteMapper<T> byteMapper, Executor executor, int chunkSize)

Method Detail

createback to summary
public static <T> Flow.Publisher<T> create(OutputStreamPublisher.OutputStreamHandler outputStreamHandler, OutputStreamPublisher.ByteMapper<T> byteMapper, Executor executor)

Creates a new Publisher<T> based on bytes written to a OutputStream. The parameter byteMapper is used to map from written bytes to the published type.

  • The parameter outputStreamHandler is invoked once per subscription of the returned Publisher, when the first item is requested.
  • OutputStream.write() invocations made by outputStreamHandler are buffered until they exceed the default chunk size of 1024, and then result in a published item if there is demand.
  • If there is no demand, OutputStream.write() will block until there is.
  • If the subscription is cancelled, OutputStream.write() will throw a IOException.
  • The subscription is completed when outputStreamHandler completes.
  • Any IOExceptions thrown from outputStreamHandler will be dispatched to the Subscriber.
Parameters
<T>
the publisher type
outputStreamHandler:OutputStreamPublisher.OutputStreamHandler

invoked when the first buffer is requested

byteMapper:OutputStreamPublisher.ByteMapper<T>

maps written bytes to T

executor:Executor

used to invoke the outputStreamHandler

Returns:Flow.Publisher<T>

a Publisher<T> based on bytes written by outputStreamHandler mapped by byteMapper

createback to summary
public static <T> Flow.Publisher<T> create(OutputStreamPublisher.OutputStreamHandler outputStreamHandler, OutputStreamPublisher.ByteMapper<T> byteMapper, Executor executor, int chunkSize)

Creates a new Publisher<T> based on bytes written to a OutputStream. The parameter byteMapper is used to map from written bytes to the published type.

  • The parameter outputStreamHandler is invoked once per subscription of the returned Publisher, when the first item is requested.
  • OutputStream.write() invocations made by outputStreamHandler are buffered until they exceed chunkSize, and then result in a published item if there is demand.
  • If there is no demand, OutputStream.write() will block until there is.
  • If the subscription is cancelled, OutputStream.write() will throw a IOException.
  • The subscription is completed when outputStreamHandler completes.
  • Any IOExceptions thrown from outputStreamHandler will be dispatched to the Subscriber.
Parameters
<T>
the publisher type
outputStreamHandler:OutputStreamPublisher.OutputStreamHandler

invoked when the first buffer is requested

byteMapper:OutputStreamPublisher.ByteMapper<T>

maps written bytes to T

executor:Executor

used to invoke the outputStreamHandler

Returns:Flow.Publisher<T>

a Publisher<T> based on bytes written by outputStreamHandler mapped by byteMapper

subscribeback to summary
public void subscribe(Flow.Subscriber<? super T> subscriber)

Implements java.util.concurrent.Flow.Publisher.subscribe.

Doc from java.util.concurrent.Flow.Publisher.subscribe.

Adds the given Subscriber if possible. If already subscribed, or the attempt to subscribe fails due to policy violations or errors, the Subscriber's onError method is invoked with an IllegalStateException. Otherwise, the Subscriber's onSubscribe method is invoked with a new Subscription. Subscribers may enable receiving items by invoking the request method of this Subscription, and may unsubscribe by invoking its cancel method.

Parameters
subscriber:Flow.Subscriber<? super T>

the subscriber

Annotations
@Override
org.springframework.http.client back to summary

public Interface OutputStreamPublisher.ByteMapper<T>

Known Direct Implementers
org.springframework.http.client.ReactorNettyClientRequest.ByteBufMapper, org.springframework.http.client.JdkClientHttpRequest.ByteBufferMapper
Type Parameters
<T>
the type to map to

Maps bytes written to in OutputStreamHandler#handle(OutputStream) to published items.

Method Summary

Modifier and TypeMethod and Description
public T
map(int b)

Maps a single byte to T.

public T
map(byte[] b, int off, int len)

Maps a byte array to T.

Method Detail

mapback to summary
public T map(int b)

Maps a single byte to T.

mapback to summary
public T map(byte[] b, int off, int len)

Maps a byte array to T.

org.springframework.http.client back to summary

public Interface OutputStreamPublisher.OutputStreamHandler

Annotations
@FunctionalInterface

Defines the contract for handling the OutputStream provided by the OutputStreamPublisher.

Method Summary

Modifier and TypeMethod and Description
public void
handle(OutputStream
the stream to write to
outputStream
)

Use the given stream for writing.

Method Detail

handleback to summary
public void handle(OutputStream outputStream) throws IOException

Use the given stream for writing.

Parameters
outputStream:OutputStream

the stream to write to

Exceptions
IOException:
any thrown I/O errors will be dispatched to the Subscriber
org.springframework.http.client back to summary

private final Class OutputStreamPublisher.OutputStreamSubscription<T>

extends OutputStream
implements Subscription
Class Inheritance
All Implemented Interfaces
java.util.concurrent.Flow.Subscription

Field Summary

Modifier and TypeField and Description
private final Flow.Subscriber<? super T>
private final OutputStreamPublisher.ByteMapper<T>
private final int
private volatile Throwable
private final OutputStreamPublisher.OutputStreamHandler
private final AtomicReference<Object>
private long
pack-priv static final Object
private final AtomicLong

Constructor Summary

AccessConstructor and Description
public
OutputStreamSubscription(Flow.Subscriber<? super T> actual, OutputStreamPublisher.OutputStreamHandler outputStreamHandler, OutputStreamPublisher.ByteMapper<T> byteMapper, int chunkSize)

Method Summary

Modifier and TypeMethod and Description
private long
addCap(long n)

private static long
addCap(long a, long b)

private void
public void
cancel()

Implements java.util.concurrent.Flow.Subscription.cancel.

Causes the Subscriber to (eventually) stop receiving messages.
private void
private void
private static boolean
isCancelled(long state)

private static boolean
isTerminated(long state)

public void
request(long
the increment of demand; a value of Long.MAX_VALUE may be considered as effectively unbounded
n
)

Implements java.util.concurrent.Flow.Subscription.request.

Adds the given number n of items to the current unfulfilled demand for this subscription.
private void
private long
private long
tryProduce(long n)

private long
public void
write(int
the byte.
b
)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this output stream.
public void
write(byte[]
the data.
b
)

Overrides java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream.
public void
write(byte[]
the data.
b
,
int
the start offset in the data.
off
,
int
the number of bytes to write.
len
)

Overrides java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.
Inherited from java.io.OutputStream:
closeflushnullOutputStream

Field Detail

actualback to summary
private final Flow.Subscriber<? super T> actual
byteMapperback to summary
private final OutputStreamPublisher.ByteMapper<T> byteMapper
chunkSizeback to summary
private final int chunkSize
errorback to summary
private volatile Throwable error
Annotations
@Nullable
outputStreamHandlerback to summary
private final OutputStreamPublisher.OutputStreamHandler outputStreamHandler
parkedThreadback to summary
private final AtomicReference<Object> parkedThread
producedback to summary
private long produced
READYback to summary
pack-priv static final Object READY
requestedback to summary
private final AtomicLong requested

Constructor Detail

OutputStreamSubscriptionback to summary
public OutputStreamSubscription(Flow.Subscriber<? super T> actual, OutputStreamPublisher.OutputStreamHandler outputStreamHandler, OutputStreamPublisher.ByteMapper<T> byteMapper, int chunkSize)

Method Detail

addCapback to summary
private long addCap(long n)
addCapback to summary
private static long addCap(long a, long b)
awaitback to summary
private void await()
cancelback to summary
public void cancel()

Implements java.util.concurrent.Flow.Subscription.cancel.

Doc from java.util.concurrent.Flow.Subscription.cancel.

Causes the Subscriber to (eventually) stop receiving messages. Implementation is best-effort -- additional messages may be received after invoking this method. A cancelled subscription need not ever receive an onComplete or onError signal.

Annotations
@Override
checkDemandAndAwaitIfNeededback to summary
private void checkDemandAndAwaitIfNeeded() throws IOException
invokeHandlerback to summary
private void invokeHandler()
isCancelledback to summary
private static boolean isCancelled(long state)
isTerminatedback to summary
private static boolean isTerminated(long state)
requestback to summary
public void request(long n)

Implements java.util.concurrent.Flow.Subscription.request.

Doc from java.util.concurrent.Flow.Subscription.request.

Adds the given number n of items to the current unfulfilled demand for this subscription. If n is less than or equal to zero, the Subscriber will receive an onError signal with an IllegalArgumentException argument. Otherwise, the Subscriber will receive up to n additional onNext invocations (or fewer if terminated).

Parameters
n:long

the increment of demand; a value of Long.MAX_VALUE may be considered as effectively unbounded

Annotations
@Override
resumeback to summary
private void resume()
tryCancelback to summary
private long tryCancel()
tryProduceback to summary
private long tryProduce(long n)
tryTerminateback to summary
private long tryTerminate()
writeback to summary
public void write(int b) throws IOException

Implements abstract java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Parameters
b:int

the byte.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
writeback to summary
public void write(byte[] b) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).

Parameters
b:byte[]

the data.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(byte[] b, int off, int len) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

Parameters
b:byte[]

the data.

off:int

the start offset in the data.

len:int

the number of bytes to write.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.