Skip to content

Commit 229ffd4

Browse files
Merge pull request #13939 from jetty/fix/jetty-12.1.x/ContentSourceAsByteArray
Add an asByteArrayAsync method taking promise for Content.Source
2 parents 63ddea9 + 218081e commit 229ffd4

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/Content.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ static ByteBuffer asByteBuffer(Source source) throws IOException
369369
* @param maxSize The maximum size to read, or -1 for no limit
370370
* @return A {@link CompletableFuture} that will be completed when the complete content is read or
371371
* failed if the max size is exceeded or there is a read error.
372-
* @deprecated no replacement
372+
* @deprecated use {@link #asByteArrayAsync(Source, int, Promise.Invocable)}
373373
*/
374374
@Deprecated(forRemoval = true, since = "12.0.15")
375375
static CompletableFuture<byte[]> asByteArrayAsync(Source source, int maxSize)
@@ -383,6 +383,18 @@ static CompletableFuture<byte[]> asByteArrayAsync(Source source, int maxSize)
383383
});
384384
}
385385

386+
/**
387+
* <p>Reads, non-blocking, the whole content source into a {@code byte} array.</p>
388+
*
389+
* @param source the source to read
390+
* @param maxSize The maximum size to read, or -1 for no limit
391+
* @param promise the {@link Promise.Invocable} to notify when the whole content has been read into a byte array.
392+
*/
393+
static void asByteArrayAsync(Source source, int maxSize, Promise.Invocable<byte[]> promise)
394+
{
395+
asRetainableByteBuffer(source, null, false, maxSize, Promise.Invocable.toPromise(promise, RetainableByteBuffer::takeByteArray));
396+
}
397+
386398
/**
387399
* <p>Reads, non-blocking, the whole content source into a {@link ByteBuffer}.</p>
388400
*

jetty-core/jetty-io/src/test/java/org/eclipse/jetty/io/ContentSourceTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.eclipse.jetty.util.FutureCallback;
5151
import org.eclipse.jetty.util.FuturePromise;
5252
import org.eclipse.jetty.util.IO;
53+
import org.eclipse.jetty.util.Promise;
5354
import org.junit.jupiter.api.AfterEach;
5455
import org.junit.jupiter.api.Assumptions;
5556
import org.junit.jupiter.api.BeforeEach;
@@ -1036,7 +1037,8 @@ public void testAsByteArrayAsync() throws Exception
10361037
{
10371038
TestContentSource source = new TestContentSource();
10381039

1039-
CompletableFuture<byte[]> completableFuture = Content.Source.asByteArrayAsync(source, -1);
1040+
CompletableFuture<byte[]> future = new CompletableFuture<>();
1041+
Content.Source.asByteArrayAsync(source, -1, Promise.Invocable.toPromise(future));
10401042

10411043
Retainable.ReferenceCounter counter = new Retainable.ReferenceCounter();
10421044
counter.retain();
@@ -1046,7 +1048,7 @@ public void testAsByteArrayAsync() throws Exception
10461048
assertNotNull(todo);
10471049
source.add(Content.Chunk.asChunk(BufferUtil.toBuffer("hello"), false, counter));
10481050
todo.run();
1049-
assertFalse(completableFuture.isDone());
1051+
assertFalse(future.isDone());
10501052

10511053
todo = source.takeDemand();
10521054
assertNotNull(todo);
@@ -1056,9 +1058,9 @@ public void testAsByteArrayAsync() throws Exception
10561058

10571059
todo = source.takeDemand();
10581060
assertNull(todo);
1059-
assertTrue(completableFuture.isDone());
1061+
assertTrue(future.isDone());
10601062

1061-
byte[] buffer = completableFuture.get();
1063+
byte[] buffer = future.get();
10621064
assertNotNull(buffer);
10631065

10641066
assertThat(new String(buffer, UTF_8), equalTo("hello cruel world"));

0 commit comments

Comments
 (0)