RFR: 8329829: HttpClient: Add a BodyPublishers.ofFileChannel method [v3]
Jaikiran Pai
jpai at openjdk.org
Thu Jul 17 13:58:49 UTC 2025
On Mon, 7 Jul 2025 10:41:58 GMT, Volkan Yazici <vyazici at openjdk.org> wrote:
>> Adds a new `ofFileChannel(FileChannel channel, long offset, long length)` method to `java.net.HttpRequest.BodyPublishers` to provide an `HttpClient` publisher to upload a certain region of a file. The new publisher does not modify the state of the passed `FileChannel`, streams the file channel bytes as it publishes (i.e., avoids reading the entire file into the memory), and can be leveraged to implement sliced uploads. As noted in the Javadoc:
>>
>>> The file channel will not be closed upon completion. The caller is
>>> expected to manage the life cycle of the channel, and close it
>>> appropriately when not needed anymore.
>>
>> ### Implementation notes
>>
>> - `FileChannel` is preferred over `{Readable,Seekable}ByteChannel`, since the latter does not provide a positional read without modifying the state of the `FileChannel`, which is necessary to use a single `FileChannel` instance to implement sliced uploads.
>> - `ofFileChannel(FileChannel,long,long)` is preferred over `ofPath(Path,long,long)` to avoid overloading the maximum file descriptor limit of the platform.
>
> Volkan Yazici has updated the pull request incrementally with one additional commit since the last revision:
>
> Improve docs on `IndexOutOfBoundsException` thrown
>
> Co-authored-by: Daniel Fuchs <67001856+dfuch at users.noreply.github.com>
src/java.net.http/share/classes/java/net/http/HttpRequest.java line 723:
> 721:
> 722: /**
> 723: * {@return a request body publisher whose body is the {@code length}
I think some of the text in this javadoc would need changes/clarifications. I haven't added any review comments for it yet and will come to it later once we have settled on the rest of the review.
src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java line 372:
> 370:
> 371: /**
> 372: * {@return a new {@link ByteBuffer} instance of configured capacity for the HTTP Client}
I think instead of saying "configured capacity" we should say "instance of {@link BUFSIZE} ...". This is an internal class so it should be OK to link to this constant even if the constant itself isn't documented. Linking to `BUFSIZE` makes it clear and easier to find what the actual capacity is.
src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java line 384:
> 382: *
> 383: * @param maxCapacity a buffer capacity, in bytes
> 384: * @throws IllegalArgumentException if {@code capacity < 0}
Typo - should have been `if {@code maxCapacity < 0}`
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/26155#discussion_r2213422566
PR Review Comment: https://git.openjdk.org/jdk/pull/26155#discussion_r2213413211
PR Review Comment: https://git.openjdk.org/jdk/pull/26155#discussion_r2213416215
More information about the net-dev
mailing list