Skip to content

Commit

Permalink
Fix StreamEntityProducer not able to repeat the production of the con…
Browse files Browse the repository at this point in the history
…tent
  • Loading branch information
nck-mlcnv committed Oct 29, 2024
1 parent a4f755f commit 5556edc
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ public class StreamEntityProducer implements AsyncEntityProducer {
* @param streamSupplier the input stream supplier, should be repeatable
* @param chunked whether the entity data should be sent in chunks
*/
public StreamEntityProducer(Supplier<InputStream> streamSupplier, boolean chunked, String contentType) throws IOException {
public StreamEntityProducer(Supplier<InputStream> streamSupplier, boolean chunked, String contentType) {
this.streamSupplier = streamSupplier;
this.chunked = chunked;
this.contentType = contentType;
if (!chunked) {
content = (streamSupplier.get() instanceof ByteArrayListInputStream) ? (ByteArrayListInputStream) streamSupplier.get() : null;
}
if (!chunked) loadContent();
}

@Override
Expand Down Expand Up @@ -132,7 +130,8 @@ public int available() {
@Override
public void produce(DataStreamChannel channel) throws IOException {
// handling of non-chunked request
if (content != null) {
if (!chunked) {
if (content == null) loadContent(); // might be necessary if the producer is reused
ByteBuffer buffer = content.getCurrentBuffer();
while (channel.write(buffer) > 0) {
if (!buffer.hasRemaining()) {
Expand All @@ -148,7 +147,7 @@ public void produce(DataStreamChannel channel) throws IOException {
}

// handling of chunked request
if (chunked && currentStream == null) {
if (currentStream == null) {
currentStream = streamSupplier.get();
}

Expand All @@ -162,4 +161,8 @@ public void produce(DataStreamChannel channel) throws IOException {
channel.endStream();
}
}

private void loadContent() {
content = (ByteArrayListInputStream) streamSupplier.get();
}
}

0 comments on commit 5556edc

Please sign in to comment.