-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8329829: HttpClient: Add a BodyPublishers.ofFileChannel method #26155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vy
wants to merge
13
commits into
openjdk:master
Choose a base branch
from
vy:ofFileChannel
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+849
−5
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bdeb999
Add `HttpRequest.BodyPublishers::ofFileChannel` along with tests
vy 0eda56f
Implement review feedback
vy 3af61c5
Improve docs on `IndexOutOfBoundsException` thrown
vy 4a9e303
Add `@since 26` to `ofFileChannel`
vy 8f66a32
Improve exception handling and documentation for `ofFileChannel`
vy b114fe9
Remove synchronization for `FileChannelIterator`
vy 183da9d
Improve wording for signaling request cancellation
vy eacd5da
Link to `BUFSIZE` in `Utils::getBuffer` Javadoc
vy da69840
Fix typo in `Utils::getBufferWithAtMost` Javadoc
vy 99503c0
Apply review feedback for `FileChannelPublisherTest`
vy 8699b83
Replace usage of `CompletableFuture` with `Future`
vy faa5d24
Verify exceptions using both `send()` and `sendAsync()`
vy c8a0257
Revert the last commit: faa5d24d831
vy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved. | ||
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
|
@@ -26,13 +26,13 @@ | |
package java.net.http; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.OpenOption; | ||
import java.nio.file.Path; | ||
import java.time.Duration; | ||
import java.util.Iterator; | ||
|
@@ -720,6 +720,42 @@ public static BodyPublisher ofFile(Path path) throws FileNotFoundException { | |
return RequestPublishers.FilePublisher.create(path); | ||
} | ||
|
||
/** | ||
* {@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> | ||
* The {@linkplain FileChannel file channel} will be read using | ||
* {@link FileChannel#read(ByteBuffer, long) FileChannel.read(ByteBuffer buffer, long position)}, | ||
* which does not modify the channel's position. Thus, the same file | ||
* channel may be shared between several publishers passed to | ||
* concurrent requests. | ||
* <p> | ||
* 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. | ||
* | ||
* @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 file referred by the provided | ||
* channel cannot be read while verifying the specified byte range | ||
* | ||
* @throws NullPointerException if {@code channel} is null | ||
* | ||
* @since 26 | ||
*/ | ||
vy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public static BodyPublisher ofFileChannel(FileChannel channel, long offset, long length) throws IOException { | ||
Objects.requireNonNull(channel, "channel"); | ||
vy marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. public FileChannelPublisher(FileChannel channel, long offset, long length) throws IOException {
this.channel = Objects.requireNonNull(channel, "channel");
// ... The FileChannelPublisher constructor already has a null value check for the parameter channel, which is redundant |
||
return new RequestPublishers.FileChannelPublisher(channel, offset, length); | ||
vy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* A request body publisher that takes data from an {@code Iterable} | ||
* of byte arrays. An {@link Iterable} is provided which supplies | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -367,10 +367,32 @@ public static String describeOps(int interestOps) { | |||||
public static IllegalArgumentException newIAE(String message, Object... args) { | ||||||
return new IllegalArgumentException(format(message, args)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
/** | ||||||
* {@return a new {@link ByteBuffer} instance of {@link #BUFSIZE} capacity} | ||||||
*/ | ||||||
public static ByteBuffer getBuffer() { | ||||||
return ByteBuffer.allocate(BUFSIZE); | ||||||
} | ||||||
|
||||||
/** | ||||||
* {@return a new {@link ByteBuffer} instance whose capacity is set to the | ||||||
* smaller of the specified {@code maxCapacity} and the default | ||||||
* ({@value BUFSIZE})} | ||||||
* | ||||||
* @param maxCapacity a buffer capacity, in bytes | ||||||
* @throws IllegalArgumentException if {@code maxCapacity < 0} | ||||||
*/ | ||||||
public static ByteBuffer getBufferWithAtMost(long maxCapacity) { | ||||||
if (maxCapacity < 0) { | ||||||
throw new IllegalArgumentException( | ||||||
// Match the message produced by `ByteBuffer::createCapacityException` | ||||||
"capacity < 0: (%s < 0)".formatted(maxCapacity)); | ||||||
} | ||||||
int effectiveCapacity = (int) Math.min(maxCapacity, BUFSIZE); | ||||||
return ByteBuffer.allocate(effectiveCapacity); | ||||||
} | ||||||
|
||||||
public static Throwable getCompletionCause(Throwable x) { | ||||||
Throwable cause = x; | ||||||
while ((cause instanceof CompletionException) | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.