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

Clarify the behaviour of ZipInputStream#readAllBytes() and readNBytes()

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: tbd
    • Component/s: core-libs
    • Labels:

      Description

      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)

        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated: