Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8262435

Clarify the behavior of a few inherited ZipInputStream methods

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 20
    • None
    • core-libs
    • b24

      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
          ...
      }

      Adding javadoc to the readAllBytes and readNBytes methods in ZipInputStream should help avoid any confusion in behaviour of these methods.


      [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)


      (Discussed in core-libs-dev mailing list https://mail.openjdk.java.net/pipermail/core-libs-dev/2021-February/074651.html)

            lancea Lance Andersen
            jpai Jaikiran Pai
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: