Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,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 see {@link #asByteArrayAsync(Source, int, Promise)}
*/
@Deprecated(forRemoval = true, since = "12.0.15")
static CompletableFuture<byte[]> asByteArrayAsync(Source source, int maxSize)
Expand All @@ -340,6 +340,33 @@ static CompletableFuture<byte[]> asByteArrayAsync(Source source, int maxSize)
});
}

/**
* <p>Reads, non-blocking, the whole content source into a {@code byte} array.</p>
*
* @param source the source to read
* @param maxSize The maximum size to read, or -1 for no limit
* @param promise the promise to notify when the whole content has been read into a byte array.
*/
static void asByteArrayAsync(Source source, int maxSize, Promise<byte[]> promise)
Comment thread
lachlan-roberts marked this conversation as resolved.
Outdated
{
asRetainableByteBuffer(source, null, false, maxSize, new Promise.Completable<>()
Comment thread
lachlan-roberts marked this conversation as resolved.
Outdated
{
@Override
public void succeeded(RetainableByteBuffer result)
{
super.succeeded(result);
promise.succeeded(result.takeByteArray());
}

@Override
public void failed(Throwable x)
{
super.failed(x);
promise.failed(x);
}
});
}

/**
* <p>Reads, non-blocking, the whole content source into a {@link ByteBuffer}.</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,8 @@ public void testAsByteArrayAsync() throws Exception
{
TestContentSource source = new TestContentSource();

CompletableFuture<byte[]> completableFuture = Content.Source.asByteArrayAsync(source, -1);
FuturePromise<byte[]> promise = new FuturePromise<>();
Content.Source.asByteArrayAsync(source, -1, promise);

Retainable.ReferenceCounter counter = new Retainable.ReferenceCounter();
counter.retain();
Expand All @@ -1046,7 +1047,7 @@ public void testAsByteArrayAsync() throws Exception
assertNotNull(todo);
source.add(Content.Chunk.asChunk(BufferUtil.toBuffer("hello"), false, counter));
todo.run();
assertFalse(completableFuture.isDone());
assertFalse(promise.isDone());

todo = source.takeDemand();
assertNotNull(todo);
Expand All @@ -1056,9 +1057,9 @@ public void testAsByteArrayAsync() throws Exception

todo = source.takeDemand();
assertNull(todo);
assertTrue(completableFuture.isDone());
assertTrue(promise.isDone());

byte[] buffer = completableFuture.get();
byte[] buffer = promise.get();
assertNotNull(buffer);

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