com.sun.imageio.plugins.png.PNGImageWriter.deflate() is inefficient.

XMLWordPrintable

    • Type: Bug
    • Resolution: Duplicate
    • Priority: P4
    • 9
    • Affects Version/s: 1.4.0, 5.0
    • Component/s: client-libs
    • None

      I noticed while working on
      4986239: DeflaterOutputStream is memory inefficient
      that this code

           private byte[] deflate(String s) throws IOException {
               ByteArrayOutputStream baos = new ByteArrayOutputStream();
               DeflaterOutputStream dos = new DeflaterOutputStream(baos);

               int len = s.length();
               for (int i = 0; i < len; i++) {
                   dos.write((int)s.charAt(i));
               }
               dos.close();

               return baos.toByteArray();
           }

      might be quite inefficient (in analagous code, I measured a 20-fold speedup).
      It is likely that passing the data as a
      block would be an order of magnitude more efficient.

      In Java, it is the user's responsibility to buffer data when writing to
      an expensive stream like FileOutputStream or DeflaterOutputStream.

      Either a block could be writting at a time, or the DeflaterOutputStream
      could be wrapped with a BufferedOutputStream.

      I haven't tested the suggested fix; I don't know how to use these classes.

      ###@###.### 2004-02-01

            Assignee:
            Sergey Bylokhov
            Reporter:
            Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: