-
Notifications
You must be signed in to change notification settings - Fork 181
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
Add Publisher.fromInputStream(InputStream, ByteArrayMapper)
#2989
Merged
Merged
Changes from 1 commit
Commits
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
79 changes: 79 additions & 0 deletions
79
servicetalk-concurrent-api/src/main/java/io/servicetalk/concurrent/api/ByteArrayMapper.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright © 2024 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.concurrent.api; | ||
|
||
import io.servicetalk.concurrent.api.FromInputStreamPublisher.ToByteArrayMapper; | ||
|
||
import static io.servicetalk.concurrent.api.FromInputStreamPublisher.DEFAULT_MAX_BUFFER_SIZE; | ||
import static io.servicetalk.concurrent.api.FromInputStreamPublisher.ToByteArrayMapper.DEFAULT_TO_BYTE_ARRAY_MAPPER; | ||
|
||
/** | ||
* A mapper to transform {@code byte[]} buffer regions into a desired type {@code T}. | ||
* | ||
* @param <T> Type of the result of this mapper | ||
*/ | ||
@FunctionalInterface | ||
public interface ByteArrayMapper<T> { | ||
|
||
/** | ||
* Maps a specified {@code byte[]} buffer region into a {@code T}. | ||
* <p> | ||
* The mapper can operate only with the specified region of the {@code buffer}. Access to other parts of the buffer | ||
* may lead to unexpected results. The specified region can be safely used by the mapper without a need to copy | ||
* data. | ||
bryce-anderson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @param buffer {@code byte[]} buffer with data | ||
* @param offset the offset of the region | ||
* @param length the length of the region | ||
* @return result of type {@code T} | ||
*/ | ||
T map(byte[] buffer, int offset, int length); | ||
|
||
/** | ||
* Returns the maximum allowed buffer size for the {@link #map(byte[], int, int)} operation. | ||
* <p> | ||
* Must be a positive number. | ||
* | ||
* @return the maximum allowed buffer size for the {@link #map(byte[], int, int)} operation | ||
*/ | ||
default int maxBufferSize() { | ||
return DEFAULT_MAX_BUFFER_SIZE; | ||
} | ||
|
||
/** | ||
* Mapper from the buffer region to an independent {@code byte[]} buffer. | ||
* <p> | ||
* Returns {@link #toByteArray(int)} with default {@link #maxBufferSize()}. | ||
* | ||
* @return a mapper from the buffer region to an independent {@code byte[]} buffer | ||
*/ | ||
static ByteArrayMapper<byte[]> toByteArray() { | ||
return DEFAULT_TO_BYTE_ARRAY_MAPPER; | ||
} | ||
|
||
/** | ||
* Mapper from the buffer region to an independent {@code byte[]} buffer. | ||
* <p> | ||
* Returns the original {@code byte[]} buffer as-is if it was completely full of data or allocates a new buffer for | ||
* the specified length and copies data. Returned {@code byte[]} buffer is always completely full. | ||
* | ||
* @param maxBufferSize the value for {@link #maxBufferSize()} | ||
* @return a mapper from the buffer region to an independent {@code byte[]} buffer | ||
*/ | ||
static ByteArrayMapper<byte[]> toByteArray(final int maxBufferSize) { | ||
return new ToByteArrayMapper(maxBufferSize); | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.
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.
We need this interface bcz concurrent-api doesn't have a dependency on buffer-api module.
Eventually, I think it's worth it. We can consolidate configuration parameters for how to read data from
InputStream
as default methods on this interface instead of adding more and more overloads forPublisher.fromInputStream
.Publisher
is already quite a lengthy class.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.
👍