Skip to content

Commit 8f01084

Browse files
jyaneejona86
authored andcommitted
core: add a close to InputBufferStream
Before: `InputBufferStream.close()` does not close their buffer so the buffer will leak. After: Resolves grpc#4198. Override the `close` for closing their buffer.
1 parent bace06f commit 8f01084

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

core/src/main/java/io/grpc/internal/ReadableBuffers.java

+5
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ public int read(byte[] dest, int destOffset, int length) throws IOException {
327327
buffer.readBytes(dest, destOffset, length);
328328
return length;
329329
}
330+
331+
@Override
332+
public void close() throws IOException {
333+
buffer.close();
334+
}
330335
}
331336

332337
private ReadableBuffers() {}

core/src/test/java/io/grpc/internal/ReadableBuffersTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.Assert.assertEquals;
2222
import static org.mockito.Mockito.mock;
2323
import static org.mockito.Mockito.never;
24+
import static org.mockito.Mockito.times;
2425
import static org.mockito.Mockito.verify;
2526

2627
import java.io.InputStream;
@@ -119,4 +120,12 @@ public void bufferInputStream_read_writesPartially() throws Exception {
119120
assertEquals(2, inputStream.read(dest, /*destOffset*/ 1, /*length*/ 2));
120121
assertArrayEquals(new byte[]{0x00, 'h', 'e'}, dest);
121122
}
123+
124+
@Test
125+
public void bufferInputStream_close_closesBuffer() throws Exception {
126+
ReadableBuffer buffer = mock(ReadableBuffer.class);
127+
InputStream inputStream = ReadableBuffers.openStream(buffer, true);
128+
inputStream.close();
129+
verify(buffer, times(1)).close();
130+
}
122131
}

0 commit comments

Comments
 (0)