RFR: 8329829: HttpClient: Add a BodyPublishers.ofFileChannel method [v8]
Jaikiran Pai
jpai at openjdk.org
Tue Aug 19 06:42:43 UTC 2025
On Mon, 11 Aug 2025 07:47:03 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 style for declaring multiple consecutive fields of the same type
The implementation and test changes look good to me.
I had some suggestions for the javadoc text. Here's what I had in mind, if Daniel and Michael suggest that the current form in this PR is OK, then it's OK to not change it:
/**
* {@return a request body publisher whose body is the {@code length}
* content bytes read from the provided file {@code channel} starting
* from the specified {@code offset}}
* <p>
* This method does not modify the {@code channel}'s position.
* <p>
* This method does not close the {@code channel}. The caller is
* expected to close the {@code channel} when no longer needed.
*
* @apiNote
* This method can be used to either publish just a portion of a
* file's content as the request body or to publish different portions
* of the file's content concurrently.
* The typical approach to concurrently publish different portions of a
* file's content is to create an instance of {@link FileChannel}
* and then create multiple {@code HttpRequest}s each of which use
* a {@code ofFileChannel BodyPublisher} with a different non-overlapping
* offset and length.
*
* @param channel a file channel
* @param offset the offset of the first byte
* @param length the number of bytes to read from the file channel
*
* @throws IndexOutOfBoundsException if the specified byte range is
* found to be {@linkplain Objects#checkFromIndexSize(long, long, long)
* out of bounds} compared with the size of the file referred by the
* channel
*
* @throws IOException if the size of the {@code channel} cannot be
* determined or the {@code channel} is closed
*
* @throws NullPointerException if {@code channel} is null
*
* @since 26
*/
(Apart from the text changes, do note that the `@throws IOException` specification in that text is intentionally changed to note that it will be thrown if the given channel is closed, in addition to if the size() cannot be determined).
-------------
PR Comment: https://git.openjdk.org/jdk/pull/26155#issuecomment-3199431572
More information about the net-dev
mailing list