Skip to content

Commit 9543eb2

Browse files
authored
feat: add limit support to ReadChannel (#688)
Add new methods to ReadChannel to allow limiting of the channel independent of any chunk sizes or buffers. ###### Motivation GCS supports range reads of objects, ReadChannel currently has `seek` to allow settings the begin offset, but doesn't provide limit to allow setting the end offset. The only alternative is to provide manually sized ByteBuffer(s) to read into that would have to track size externally. This new feature allows the Channel itself to absorb the burden and prevent reading more bytes than necessary from GCS.
1 parent b91ee86 commit 9543eb2

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- see https://www.mojohaus.org/clirr-maven-plugin/examples/ignored-differences.html -->
3+
<differences>
4+
<!-- Clear doesn't know about default interface methods -->
5+
<difference>
6+
<differenceType>7012</differenceType>
7+
<className>com/google/cloud/ReadChannel</className>
8+
<method>* limit(long)</method>
9+
</difference>
10+
<difference>
11+
<differenceType>7012</differenceType>
12+
<className>com/google/cloud/ReadChannel</className>
13+
<method>long limit()</method>
14+
</difference>
15+
</differences>

java-core/google-cloud-core/src/main/java/com/google/cloud/ReadChannel.java

+30
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,34 @@ public interface ReadChannel extends ReadableByteChannel, Closeable, Restorable<
5353
*/
5454
@Override
5555
RestorableState<ReadChannel> capture();
56+
57+
/**
58+
* Limit the maximum number of bytes available to be read from this channel. If the limit is
59+
* larger than the actual size of the content this will have no material impact.
60+
*
61+
* <p><i>NOTE:</i>Implementers are not required to return a new instance from this method, however
62+
* they are allowed to. Users of this method should always use the instance returned from this
63+
* method.
64+
*
65+
* <p><i>Default Implementation:</i>By default, this method will simply return {@code this}.
66+
*
67+
* @param limit the maximum number of bytes to limit this channel to
68+
* @return The instance of channel which will respect the limit.
69+
* @throws UnsupportedOperationException If the {@code this} instances does not support limiting
70+
* @since 2.4.0
71+
*/
72+
default ReadChannel limit(long limit) {
73+
return this;
74+
}
75+
76+
/**
77+
* The currently defined limit for this channel. Initial value is {@link Long#MAX_VALUE}
78+
*
79+
* @return the current limit for this channel
80+
* @throws UnsupportedOperationException If the {@code this} instances does not support limiting
81+
* @since 2.4.0
82+
*/
83+
default long limit() {
84+
return Long.MAX_VALUE;
85+
}
5686
}

0 commit comments

Comments
 (0)