Skip to content

Commit f679c63

Browse files
mtdowlingrhernandez35
authored andcommitted
Add close to DataStream to allow closing resources
1 parent 80f7fd4 commit f679c63

4 files changed

Lines changed: 42 additions & 1 deletion

File tree

io/src/main/java/software/amazon/smithy/java/io/ByteBufferOutputStream.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ public int size() {
6868
return count;
6969
}
7070

71+
/**
72+
* Resets the stream to empty, allowing buffer reuse.
73+
* The internal buffer is retained, avoiding reallocation.
74+
*/
75+
public void reset() {
76+
count = 0;
77+
}
78+
7179
private void ensureCapacity(int minCapacity) {
7280
// overflow-conscious code
7381
int oldCapacity = buf.length;

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* Abstraction for reading streams of data.
2121
*/
22-
public interface DataStream extends Flow.Publisher<ByteBuffer> {
22+
public interface DataStream extends Flow.Publisher<ByteBuffer>, AutoCloseable {
2323
/**
2424
* Length of the data stream, if known.
2525
*
@@ -105,6 +105,19 @@ default void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
105105
HttpRequest.BodyPublishers.ofInputStream(this::asInputStream).subscribe(subscriber);
106106
}
107107

108+
/**
109+
* Closes any underlying resources associated with this data stream.
110+
*
111+
* <p>The default implementation does nothing. Implementations that hold closeable resources (e.g., input streams)
112+
* should override this method to release them.
113+
*
114+
* <p>It is safe to call this method multiple times.
115+
*/
116+
@Override
117+
default void close() {
118+
// Default no-op. Implementations holding closeable resources should override.
119+
}
120+
108121
/**
109122
* Create an empty DataStream.
110123
*

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55

66
package software.amazon.smithy.java.io.datastream;
77

8+
import java.io.IOException;
89
import java.io.InputStream;
10+
import java.io.UncheckedIOException;
911

1012
final class InputStreamDataStream implements DataStream {
1113

1214
private final InputStream inputStream;
1315
private final String contentType;
1416
private final long contentLength;
1517
private boolean consumed;
18+
private boolean closed;
1619

1720
InputStreamDataStream(InputStream inputStream, String contentType, long contentLength) {
1821
this.inputStream = inputStream;
@@ -48,4 +51,16 @@ public long contentLength() {
4851
public String contentType() {
4952
return contentType;
5053
}
54+
55+
@Override
56+
public void close() {
57+
if (!closed) {
58+
closed = true;
59+
try {
60+
inputStream.close();
61+
} catch (IOException e) {
62+
throw new UncheckedIOException("Failed to close data stream", e);
63+
}
64+
}
65+
}
5166
}

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
@@ -57,4 +57,9 @@ public boolean isAvailable() {
5757
public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
5858
delegate.subscribe(subscriber);
5959
}
60+
61+
@Override
62+
public void close() {
63+
delegate.close();
64+
}
6065
}

0 commit comments

Comments
 (0)