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

Deflater/Inflater documentation incomplete/misleading

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 24
    • 8
    • core-libs

      The Javadoc of Deflater and Inflater contains a code fragment intended to demonstrate how to use the class. The code fragment includes a fixed size buffer - byte[100] - which does not handle most real world use cases, and leaves the example incomplete and confusing.

      The following is an attempt at a better example section:

      Deflater deflater = new Deflater();
      deflater.setInput(data);
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
      deflater.finish();
      byte[] buffer = new byte[1024];
      while (deflater.finished() == false) {
       int size = deflater.deflate(buffer);
       outputStream.write(buffer, 0, size);
      }
      outputStream.close();
      byte[] output = outputStream.toByteArray();

      A similar looping strategy is needed for inflation.

      (Note that an expert should determine the best example code. The above is my guess at good code, given the absence of advice on looping in the Javadoc)

      The same example needs to be placed in both Deflater and Inflater. Right now, the examples are subtly different.

      The example should be updated to use StandardCharsets.UTF_8, removing the need to catch UnsupportedEncodingException.

      More broadly, the class feels like you need to have used ZLIB somewhere else (like C) in order to understand how to use it.

            jpai Jaikiran Pai
            scolebourne Stephen Colebourne
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: