Skip to content

Commit

Permalink
fix fabric8io#4201: jetty's bytebuffers may be immediately re-used
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Oct 18, 2022
1 parent 387237b commit 9156cde
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,20 @@ public void onContent(Response response, LongConsumer demand, ByteBuffer content
this.demand.complete(demand);
}
try {
bodyConsumer.consume(process(response, content), this);
// we must clone as the buffer can be reused after the call to succeeded
bodyConsumer.consume(process(response, clone(content)), this);
callback.succeeded();
} catch (Exception e) {
callback.failed(e);
}
}

protected abstract T process(Response response, ByteBuffer content);

public static ByteBuffer clone(ByteBuffer original) {
ByteBuffer clone = ByteBuffer.allocate(original.remaining());
clone.put(original);
clone.flip();
return clone;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ default <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, Cl

/**
* Send a request and consume the bytes of the resulting response body
* <p>
* HtttpClient implementations will provide ByteBuffers that may be held directly.
*
* @param request the HttpRequest to send
* @param consumer the response body consumer
Expand Down

0 comments on commit 9156cde

Please sign in to comment.