Make the StreamGenerator an AutoCloseable - #2428
Conversation
This would allow for Stream generator types to be used with `try-with-resources`
Make the `StreamGenerator` an `AutoCloseable`
|
Thanks for the contribution, but I have to knock this one back, the file is package private so it would be of limited use, but also PGP is often used as a real protocol, so auto-closing the underlying stream could cause havoc and close operations are better handled by hand as suits the user of the API. |
@dghgit in practice, that PGPCompressedDataGenerator compressedDataGenerator = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator();
try {
// our stuff
} finally {
literalDataGenerator.close();
compressedDataGenerator.close();
]In other words, the home-made |
|
That's not a bad point, but perhaps the trick here is to introduce a new class/interface which can act as a wrapper? Suggestions most welcome. |
|
@dghgit - to be fair, let's just look at what a wrapper might look like. public class AutoCloseableWrapper<T extends StreamGenerator> implements Closeable {
private final T wrapped;
public AutoCloseableWrapper(T wrapped) { this.wrapped = wrapped };
@Override
public void close() throws IOException {
wrapped.close();
}
public T get() {
return wrapped;
}
}So in a try with resources you could go: try (AutoCloseableWrapper<SomeThing> wrapped = new AutoCloseableWrapper<>(new Something()) {
Something something = wrapped.get();
// do the thing
}The problem with this approach is the hassle of wrapping and unwrapping the thing, all for the trivial bridge to the close method. |
|
I'm not sure how this deals with the issue that StreamGenerator is not meant to close the underlying stream though? |


This would allow
StreamGeneratorto be used with try-with-resources