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

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

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • 9
    • 1.4.0, 5.0
    • 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

            serb Sergey Bylokhov
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: