ZipInputStream#readAllBytes should clarify it doesn't read the whole stream?
Jaikiran Pai
jai.forums2013 at gmail.com
Thu Feb 25 03:56:31 UTC 2021
The javadoc of InputStream#readAllBytes() states[1] that it reads all
the remaining bytes of the stream. The java.util.zip.ZipInputStream
doesn't override this method and thus "inherits" this javadoc. The
implementation of InputStream#readAllBytes() ultimately ends up calling
ZipInputStream#read()[2], so the implementation correctly reads only
till the end of the current ZipEntry and not the entire ZipInputStream.
However, because the javadoc gets inherited, reading any code like the
following doesn't make it clear that it's only reading till the end of
the current entry:
zis = ... // ZipInputStream
while ((e = zis.getNextEntry()) != null) {
String name = e.getName();
zis.readAllBytes(); // gives an impression that all bytes of the
stream are read
...
}
Should the ZipInputStream override the readAllBytes(), just so as to add
a very specific javadoc to this method which explains that it reads only
till the end of current entry and other related semantics? Perhaps the
same should be done for ZipInputStream#readNBytes(...)?
[1]
https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/io/InputStream.html#readAllBytes()
[2]
https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/zip/ZipInputStream.html#read(byte%5B%5D,int,int)
-Jaikiran
More information about the core-libs-dev
mailing list