Skip to content

Commit 2690a6a

Browse files
committed
HTTPRequestBuilder: Add body as InputStream functions
1 parent 7a84029 commit 2690a6a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

protocol/src/main/java/org/threadly/litesockets/protocols/http/request/HTTPRequestBuilder.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.threadly.litesockets.protocols.http.request;
22

3+
import java.io.InputStream;
34
import java.net.URL;
45
import java.nio.ByteBuffer;
56
import java.nio.charset.Charset;
@@ -13,6 +14,7 @@
1314
import java.util.concurrent.TimeUnit;
1415
import java.util.function.Supplier;
1516

17+
import org.threadly.concurrent.SubmitterExecutor;
1618
import org.threadly.concurrent.future.FutureUtils;
1719
import org.threadly.concurrent.future.ListenableFuture;
1820
import org.threadly.litesockets.protocols.http.shared.HTTPAddress;
@@ -270,6 +272,11 @@ public HTTPRequestBuilder setBody(final ByteBuffer bb) {
270272
return this;
271273
}
272274

275+
public HTTPRequestBuilder setStreamedBody(final SubmitterExecutor executor, final int bodySize,
276+
final InputStream bodyStream) {
277+
return setStreamedBody(bodySize, bodyProducer(executor, bodyStream));
278+
}
279+
273280
public HTTPRequestBuilder setStreamedBody(final int bodySize,
274281
final Supplier<ListenableFuture<ByteBuffer>> bodySupplier) {
275282
this.bodySupplier = bodySupplier;
@@ -278,6 +285,24 @@ public HTTPRequestBuilder setStreamedBody(final int bodySize,
278285
return this;
279286
}
280287

288+
public HTTPRequestBuilder setChunkedBody(final SubmitterExecutor executor,
289+
final InputStream bodyStream) {
290+
return setChunkedBody(bodyProducer(executor, bodyStream));
291+
}
292+
293+
private Supplier<ListenableFuture<ByteBuffer>> bodyProducer(final SubmitterExecutor executor,
294+
final InputStream bodyStream) {
295+
return () -> executor.submit(() -> {
296+
byte[] buffer = new byte[8192];
297+
int c = bodyStream.read(buffer);
298+
if (c > 0) {
299+
return ByteBuffer.wrap(buffer, 0, c);
300+
} else {
301+
return null;
302+
}
303+
});
304+
}
305+
281306
public HTTPRequestBuilder setChunkedBody(final Supplier<ListenableFuture<ByteBuffer>> bodySupplier) {
282307
this.bodySupplier = bodySupplier;
283308
this.removeHeader(HTTPConstants.HTTP_KEY_CONTENT_LENGTH);

0 commit comments

Comments
 (0)