Skip to content

Commit 0bb249f

Browse files
committed
Add isAvailable to check if has not been consumed
1 parent 2a66356 commit 0bb249f

File tree

11 files changed

+92
-0
lines changed

11 files changed

+92
-0
lines changed

io/src/main/java/software/amazon/smithy/java/io/datastream/ByteBufferDataStream.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public boolean isReplayable() {
3131
return true;
3232
}
3333

34+
@Override
35+
public boolean isAvailable() {
36+
return true;
37+
}
38+
3439
@Override
3540
public ByteBuffer asByteBuffer() {
3641
return buffer.duplicate();

io/src/main/java/software/amazon/smithy/java/io/datastream/DataStream.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ default boolean hasKnownLength() {
5757
*/
5858
boolean isReplayable();
5959

60+
/**
61+
* Check if the DataStream is available for consumption.
62+
*
63+
* <p>A stream is available if it either hasn't been consumed yet, or if it is replayable. This is useful for
64+
* interceptors that need to make decisions based on whether the stream data can be accessed without causing
65+
* an error. In other worse, if this method returns {@code true}, then calling {@link #asInputStream()} will
66+
* succeed.
67+
*
68+
* <p>Note: This method returning {@code true} does not guarantee that reading will succeed (e.g., I/O errors
69+
* can still occur), only that the stream has not been previously consumed in a non-replayable manner.
70+
*
71+
* @return true if the stream can be consumed.
72+
*/
73+
boolean isAvailable();
74+
6075
/**
6176
* Convert the stream into a blocking {@link InputStream}.
6277
*

io/src/main/java/software/amazon/smithy/java/io/datastream/EmptyDataStream.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public boolean isReplayable() {
3131
return true;
3232
}
3333

34+
@Override
35+
public boolean isAvailable() {
36+
return true;
37+
}
38+
3439
@Override
3540
public long contentLength() {
3641
return 0;

io/src/main/java/software/amazon/smithy/java/io/datastream/FileDataStream.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public boolean isReplayable() {
4646
return true;
4747
}
4848

49+
@Override
50+
public boolean isAvailable() {
51+
return true;
52+
}
53+
4954
@Override
5055
public InputStream asInputStream() {
5156
try {

io/src/main/java/software/amazon/smithy/java/io/datastream/InputStreamDataStream.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public boolean isReplayable() {
3434
return false;
3535
}
3636

37+
@Override
38+
public boolean isAvailable() {
39+
return !consumed;
40+
}
41+
3742
@Override
3843
public long contentLength() {
3944
return contentLength;

io/src/main/java/software/amazon/smithy/java/io/datastream/PublisherDataStream.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public boolean isReplayable() {
4040
return isReplayable;
4141
}
4242

43+
@Override
44+
public boolean isAvailable() {
45+
return isReplayable || !consumed;
46+
}
47+
4348
// Override to skip needing an intermediate InputStream for this.
4449
@Override
4550
public ByteBuffer asByteBuffer() {

io/src/main/java/software/amazon/smithy/java/io/datastream/WrappedDataStream.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public boolean isReplayable() {
4848
return isReplayable;
4949
}
5050

51+
@Override
52+
public boolean isAvailable() {
53+
return delegate.isAvailable();
54+
}
55+
5156
@Override
5257
public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
5358
delegate.subscribe(subscriber);

io/src/test/java/software/amazon/smithy/java/io/datastream/ByteBufferDataStreamTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,13 @@ public void returnsByteBuffer() {
2222
assertThat(ds.asByteBuffer(), equalTo(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8))));
2323
assertThat(ds.isReplayable(), is(true));
2424
}
25+
26+
@Test
27+
public void isAlwaysAvailable() {
28+
var ds = DataStream.ofBytes("foo".getBytes(StandardCharsets.UTF_8));
29+
30+
assertThat(ds.isAvailable(), is(true));
31+
ds.asByteBuffer();
32+
assertThat(ds.isAvailable(), is(true));
33+
}
2534
}

io/src/test/java/software/amazon/smithy/java/io/datastream/FileDataStreamTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,13 @@ public void readsDataToByteBuffer() throws Exception {
4747

4848
assertThat(ds.asByteBuffer(), equalTo(ByteBuffer.wrap("Hello!".getBytes(StandardCharsets.UTF_8))));
4949
}
50+
51+
@Test
52+
public void isAlwaysAvailable() throws Exception {
53+
var ds = DataStream.ofFile(Paths.get(getClass().getResource("test.txt").toURI()));
54+
55+
assertThat(ds.isAvailable(), is(true));
56+
ds.asByteBuffer();
57+
assertThat(ds.isAvailable(), is(true));
58+
}
5059
}

io/src/test/java/software/amazon/smithy/java/io/datastream/InputStreamDataStreamTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,21 @@ public void cannotReadDataStreamTwice() throws Exception {
7070
Assertions.assertDoesNotThrow(ds::asInputStream);
7171
Assertions.assertThrows(IllegalStateException.class, ds::asInputStream);
7272
}
73+
74+
@Test
75+
public void isAvailableBeforeConsumption() throws Exception {
76+
var ds = DataStream.ofInputStream(
77+
Files.newInputStream(Paths.get(getClass().getResource("test.txt").toURI())));
78+
79+
assertThat(ds.isAvailable(), is(true));
80+
}
81+
82+
@Test
83+
public void isNotAvailableAfterConsumption() throws Exception {
84+
var ds = DataStream.ofInputStream(
85+
Files.newInputStream(Paths.get(getClass().getResource("test.txt").toURI())));
86+
87+
ds.asInputStream();
88+
assertThat(ds.isAvailable(), is(false));
89+
}
7390
}

0 commit comments

Comments
 (0)