RFR: 8329829: HttpClient: Add a BodyPublishers.ofFileChannel method [v7]

Shaojin Wen swen at openjdk.org
Sat Aug 9 13:10:15 UTC 2025


On Tue, 5 Aug 2025 11:27:16 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:
> 
>   Revert the last commit: faa5d24d831
>   
>   The `send()`-vs-`sendAsync()` discrepancy will be addressed separately.

test/jdk/java/net/httpclient/FileChannelPublisherTest.java line 105:

> 103:     private static final ServerRequestPair HTTP2 = ServerRequestPair.of(Version.HTTP_2, false);
> 104: 
> 105:     private static final ServerRequestPair HTTPS2 = ServerRequestPair.of(Version.HTTP_2, true);

Suggestion:

    private static final ServerRequestPair 
            HTTP1  = ServerRequestPair.of(Version.HTTP_1_1, false), 
            HTTPS1 = ServerRequestPair.of(Version.HTTP_1_1, true),
            HTTP2  = ServerRequestPair.of(Version.HTTP_2, false),
            HTTPS2 = ServerRequestPair.of(Version.HTTP_2, true);

This coding style can be considered for declaring multiple consecutive fields of the same type.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/26155#discussion_r2264701902


More information about the net-dev mailing list