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

method flushBits() works incorrectly after setBitOffset(int)

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      beta3
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Not verified

      Description



      Name: vpR10011 Date: 06/04/2001



      Specification of the class javax.imageio.stream.ImageOutputStreamImpl says:

      "protected final void flushBits() throws IOException

      If the bit offset is non-zero, forces the remaining bits in the current
      byte to 0 and advances the stream position by one.
      This method should be called by subclasses at the beginning of
      the write(int) and write(byte[], int, int) methods.

      Throws:
          IOException - if an I/O error occurs."


      This method works incorrectly when the bit offset is non-zero and
      the current stream position is equal to the stream length.
      It happens when we use setBitOffset(int) before write(int).
      In this case flushBits() reads byte from the stream and ignores
      that read() returns -1 because the end of the stream is reached.

      When the stream is empty, streamPos is equal to zero and bit offset isn't
      zero flushBits() throws IndexOutOfBoundsException.

      To reproduce this bug run the following test:
      ---------------------test.java--------------
      import java.io.*;
      import javax.imageio.stream.*;

      public class test {
          public static void main(String[] argv) {
              OutputStream os = (OutputStream) new ByteArrayOutputStream();
              MemoryCacheImageOutputStream iosI =
                      new MemoryCacheImageOutputStream(os);
              try {
                  System.out.println("setBitOffset(7) when stream is empty:");
                  iosI.seek(0);
                  iosI.setBitOffset(7);
                  iosI.write(1);
                  System.out.println("Passed");

              } catch (Exception e) {
                   System.out.println("Failed: unxepected xception: " + e);
              }
              try {
                  System.out.println("setBitOffset(7) after write(1):");
                  iosI.seek(0);
                  iosI.write(1);
                  iosI.setBitOffset(7);
                  iosI.write(1);
                  iosI.seek(0);
                  int i = iosI.read();
                  if (i == 1) {
                      System.out.println("Passed: read() = " + i);
                  } else {
                      System.out.println("Failed: read() = " + i + " but expected 1");
                  }
              } catch (Exception e) {
                   System.out.println("Failed: unxepected xception: " + e);
              }
          }
      }
      -----------------------------------------
      % javac test.java
      % java -version
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta_refresh-b66)
      Java HotSpot(TM) Client VM (build 1.4.0-beta_refresh-b66, mixed mode)
      % java test
      setBitOffset(7) when stream is empty:
      Failed: unxepected xception: java.lang.IndexOutOfBoundsException: pos < flushedPos!
      setBitOffset(7) after write(1):
      Failed: read() = 254 but expected 1
      ----------------------------------------


      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              dricesunw Daniel Rice (Inactive)
              Reporter:
              verapsunw Verap Verap (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: