Stefán Freyr Stefánsson
01/19/2021, 2:07 PMclass ObservableInputStream(
private val wrapped: InputStream
) : InputStream() {
// override all methods of InputStream by calling the corresponding method in wrapped
}
Now, since Java is moving much faster than it was before 8 was released, we’re seeing methods being added to this fundamental class (readAllBytes()
, readNBytes(byte[], int, int)
and transferTo(OutputStream)
added in 9, readNBytes(int)
added in 11) my question is whether there is any best-practice to future proof such a delegator class. We’ll be running on JDK 11 for now but we’ll try to keep our runtime up to date… so if a future version of the JDK adds some methods to InputStream, this could possibly lead to mysterious bugs as those won’t get delegated unless we add those manually (which, of course, would render the new class backwards incompatible).
So, any pointers on how to handle this? Even something that would simply fail compilation if we update the JDK without overriding any new methods that would have been added. I don’t know of anything like that myself but maybe something like this exists.Dmitry
01/19/2021, 2:30 PMread(byte[] b, int off, int count)
method on observable input stream.
It delegates call to original input stream. Which then uses it’s own read()
, not one from observable input stream.
Is that how you want it to behave?
If yes - what is the point of wrapping input stream?Stefán Freyr Stefánsson
01/19/2021, 2:39 PMread(byte[], int, int)
I’m intercepting the return value of that and adding that to my accumulation of bytes read. Even though that internal implementation then calls its own read()
it’s okay because I’ve already collected the bytes read. Are you implying that using pure inheritance would be a better fit here?Dmitry
01/19/2021, 2:49 PMStefán Freyr Stefánsson
01/19/2021, 2:51 PMStefán Freyr Stefánsson
01/19/2021, 4:44 PM