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

Jaikiran Pai jpai at openjdk.org
Mon Jul 28 07:11:01 UTC 2025


On Fri, 18 Jul 2025 09:44:35 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 six additional commits since the last revision:
> 
>  - Fix typo in `Utils::getBufferWithAtMost` Javadoc
>  - Link to `BUFSIZE` in `Utils::getBuffer` Javadoc
>  - Improve wording for signaling request cancellation
>  - Remove synchronization for `FileChannelIterator`
>  - Improve exception handling and documentation for `ofFileChannel`
>  - Add `@since 26` to `ofFileChannel`

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

> 294:             "6,2,5"     // (offset + length) > fileSize
> 295:     })
> 296:     void testIllegalOffset(

Given what this test verifies, should we name this method `testIllegalOffsetOrLen(...)`?

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

> 298:             int fileChannelOffset,
> 299:             int fileChannelLength,
> 300:             @TempDir(cleanup = CleanupMode.ON_SUCCESS) Path tempDir) throws Exception {

It's not very common for us to use JUnit's `@TempDir` in our tests. I'm not completely sure where it creates those temporary directories. Instead, it might be better to use `Files.createTemporaryDirectory(Path.of("."), ...)` in these tests to allow for those temporary directories to be created under the jtreg's scratch directories.

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

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


More information about the net-dev mailing list