Encapsulate Socket.InputStream on Session
valenpo opened this issue · comments
Incapsulate Session.getRawInput(), so returned stream couldn't be closed outside.
This solve few problems:
- Possible to use InputStream in try block in DataInputStream etc...
- All Decorators (BdatInputStream, DotTerminatedInputStream etc...) of InputStream also could be closed, prevent stream leaks inside decorators
Example of code that will be fixed
private static byte[] readAndClose(InputStream is, int maxMessageSize)
throws IOException, TooMuchDataException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int n;
try {
while ((n = is.read(buffer)) != -1) {
bytes.write(buffer, 0, n);
if (maxMessageSize > 0 && bytes.size() > maxMessageSize) {
throw new TooMuchDataException("message size exceeded maximum of " + maxMessageSize + "bytes");
}
}
} finally {
// TODO creator of stream should close it, not this method
is.close();
}
return bytes.toByteArray();
}
@davidmoten Hello, any update?
I'll answer on ytour related PR #113.