diff --git a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/Content.java b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/Content.java index b46b6bca36a5..85f8b9627a2e 100644 --- a/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/Content.java +++ b/jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/Content.java @@ -369,7 +369,7 @@ static ByteBuffer asByteBuffer(Source source) throws IOException * @param maxSize The maximum size to read, or -1 for no limit * @return A {@link CompletableFuture} that will be completed when the complete content is read or * failed if the max size is exceeded or there is a read error. - * @deprecated no replacement + * @deprecated use {@link #asByteArrayAsync(Source, int, Promise.Invocable)} */ @Deprecated(forRemoval = true, since = "12.0.15") static CompletableFuture asByteArrayAsync(Source source, int maxSize) @@ -383,6 +383,18 @@ static CompletableFuture asByteArrayAsync(Source source, int maxSize) }); } + /** + *

Reads, non-blocking, the whole content source into a {@code byte} array.

+ * + * @param source the source to read + * @param maxSize The maximum size to read, or -1 for no limit + * @param promise the {@link Promise.Invocable} to notify when the whole content has been read into a byte array. + */ + static void asByteArrayAsync(Source source, int maxSize, Promise.Invocable promise) + { + asRetainableByteBuffer(source, null, false, maxSize, Promise.Invocable.toPromise(promise, RetainableByteBuffer::takeByteArray)); + } + /** *

Reads, non-blocking, the whole content source into a {@link ByteBuffer}.

* diff --git a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ContentSourceTest.java b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ContentSourceTest.java index 9b78c3fc4e02..a9ef220a5222 100644 --- a/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ContentSourceTest.java +++ b/jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ContentSourceTest.java @@ -50,6 +50,7 @@ import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.util.FuturePromise; import org.eclipse.jetty.util.IO; +import org.eclipse.jetty.util.Promise; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.BeforeEach; @@ -1036,7 +1037,8 @@ public void testAsByteArrayAsync() throws Exception { TestContentSource source = new TestContentSource(); - CompletableFuture completableFuture = Content.Source.asByteArrayAsync(source, -1); + CompletableFuture future = new CompletableFuture<>(); + Content.Source.asByteArrayAsync(source, -1, Promise.Invocable.toPromise(future)); Retainable.ReferenceCounter counter = new Retainable.ReferenceCounter(); counter.retain(); @@ -1046,7 +1048,7 @@ public void testAsByteArrayAsync() throws Exception assertNotNull(todo); source.add(Content.Chunk.asChunk(BufferUtil.toBuffer("hello"), false, counter)); todo.run(); - assertFalse(completableFuture.isDone()); + assertFalse(future.isDone()); todo = source.takeDemand(); assertNotNull(todo); @@ -1056,9 +1058,9 @@ public void testAsByteArrayAsync() throws Exception todo = source.takeDemand(); assertNull(todo); - assertTrue(completableFuture.isDone()); + assertTrue(future.isDone()); - byte[] buffer = completableFuture.get(); + byte[] buffer = future.get(); assertNotNull(buffer); assertThat(new String(buffer, UTF_8), equalTo("hello cruel world"));