-
- All Superinterfaces:
Flow.Publisher<ByteBuffer>
- Enclosing class:
- HttpRequest
public static interface HttpRequest.BodyProcessor extends Flow.Publisher<ByteBuffer>
A processor which converts high level Java objects into flows ofByteBuffer
s suitable for sending as request bodies.
Incubating Feature. Will be removed in a future release.BodyProcessor
s implementFlow.Publisher
which means they act as a publisher of byte buffers.The HTTP client implementation subscribes to the processor in order to receive the flow of outgoing data buffers. The normal semantics of
Flow.Subscriber
andFlow.Publisher
are implemented by the library and expected from processor implementations. Each outgoing request results in oneSubscriber
subscribing to thePublisher
in order to provide the sequence ofByteBuffer
s containing the request body.ByteBuffer
s must be allocated by the processor, and must not be accessed after being handed over to the library. These subscriptions complete normally when the request is fully sent, and can be canceled or terminated early through error. If a request needs to be resent for any reason, then a new subscription is created which is expected to generate the same data as before.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description long
contentLength()
Returns the content length for this request body.static HttpRequest.BodyProcessor
fromByteArray(byte[] buf)
Returns a request body processor whose body is the given byte array.static HttpRequest.BodyProcessor
fromByteArray(byte[] buf, int offset, int length)
Returns a request body processor whose body is the content of the given byte array oflength
bytes starting from the specifiedoffset
.static HttpRequest.BodyProcessor
fromByteArrays(Iterable<byte[]> iter)
A request body processor that takes data from anIterable
of byte arrays.static HttpRequest.BodyProcessor
fromFile(Path path)
A request body processor that takes data from the contents of a File.static HttpRequest.BodyProcessor
fromInputStream(Supplier<? extends InputStream> streamSupplier)
A request body processor that reads its data from anInputStream
.static HttpRequest.BodyProcessor
fromString(String body)
Returns a request body processor whose body is the givenString
, converted using theUTF_8
character set.static HttpRequest.BodyProcessor
fromString(String s, Charset charset)
Returns a request body processor whose body is the givenString
, converted using the given character set.-
Methods inherited from interface java.util.concurrent.Flow.Publisher
subscribe
-
-
-
-
Method Detail
-
fromString
static HttpRequest.BodyProcessor fromString(String body)
Returns a request body processor whose body is the givenString
, converted using theUTF_8
character set.- Parameters:
body
- the String containing the body- Returns:
- a BodyProcessor
-
fromString
static HttpRequest.BodyProcessor fromString(String s, Charset charset)
Returns a request body processor whose body is the givenString
, converted using the given character set.- Parameters:
s
- the String containing the bodycharset
- the character set to convert the string to bytes- Returns:
- a BodyProcessor
-
fromInputStream
static HttpRequest.BodyProcessor fromInputStream(Supplier<? extends InputStream> streamSupplier)
A request body processor that reads its data from anInputStream
. ASupplier
ofInputStream
is used in case the request needs to be sent again as the content is not buffered. TheSupplier
may returnnull
on subsequent attempts in which case, the request fails.- Parameters:
streamSupplier
- a Supplier of open InputStreams- Returns:
- a BodyProcessor
-
fromByteArray
static HttpRequest.BodyProcessor fromByteArray(byte[] buf)
Returns a request body processor whose body is the given byte array.- Parameters:
buf
- the byte array containing the body- Returns:
- a BodyProcessor
-
fromByteArray
static HttpRequest.BodyProcessor fromByteArray(byte[] buf, int offset, int length)
Returns a request body processor whose body is the content of the given byte array oflength
bytes starting from the specifiedoffset
.- Parameters:
buf
- the byte array containing the bodyoffset
- the offset of the first bytelength
- the number of bytes to use- Returns:
- a BodyProcessor
-
fromFile
static HttpRequest.BodyProcessor fromFile(Path path) throws FileNotFoundException
A request body processor that takes data from the contents of a File.- Parameters:
path
- the path to the file containing the body- Returns:
- a BodyProcessor
- Throws:
FileNotFoundException
- if path not found
-
fromByteArrays
static HttpRequest.BodyProcessor fromByteArrays(Iterable<byte[]> iter)
A request body processor that takes data from anIterable
of byte arrays. AnIterable
is provided which suppliesIterator
instances. Each attempt to send the request results in one invocation of theIterable
- Parameters:
iter
- an Iterable of byte arrays- Returns:
- a BodyProcessor
-
contentLength
long contentLength()
Returns the content length for this request body. May be zero if no request content being sent, greater than zero for a fixed length content, and less than zero for an unknown content length.- Returns:
- the content length for this request body if known
-
-