RFR: 8329829: HttpClient: Add a BodyPublishers.ofFileChannel method [v3]
Jaikiran Pai
jpai at openjdk.org
Thu Jul 17 13:17:51 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 747:
> 745: public static BodyPublisher ofFileChannel(FileChannel channel, long offset, long length) {
> 746: Objects.requireNonNull(channel, "channel");
> 747: return new RequestPublishers.FileChannelPublisher(channel, offset, length);
Hello Volkan, I see that the constructor of `FileChannelPublisher` attempts to determine the file size of the channel and if an `IOException` is thrown then it gets propagated as `UncheckedIOException`. I think an alternate approach and perhaps a better one would be to specify in this new method's documentation that the `FileChannel`'s size will be first determined by this method to verify that the given `offset` and the `length` are within bounds of that size. That then allows us to add a throws clause to this method which we merely propagate from the call of `FileChannel.size()`.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/26155#discussion_r2213322140
More information about the net-dev
mailing list