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

read([B,I,I) works incorrectly if len<=0

    XMLWordPrintable

Details

    • beta3
    • generic
    • generic

    Description



      Name: mlR10151 Date: 06/08/2001



      The description of the MemoryCacheImageOutputStream.read(byte[] b, int off, int len)
      says:

      Reads up to len bytes from the stream, and stores them into b starting at index off. The number
      of bytes read is returned. If no bytes can be read because the end of the stream has been
      reached, -1 is returned.
      ...
      Throws:
                  IndexOutOfBoundsException - if off is negative, len is negative, or off + len is greater
                  than b.length.

      So, it must:
      1. throw IOOBE if len < 0
      2. return 0, if len == 0 and current position is not an EOF,

      but it just returns -1 if len<=0.

      To reproduce the bug run the following test on JDK-Merlin build b66:
      ===================== a.java =======================
      import javax.imageio.stream.*;
      import java.io.*;
      public class a {
          public static void main(String[] args) throws Exception {
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(baos);

              byte[] ba = new byte[100];
              mcios.write(ba);
              mcios.seek(0);

              int result = mcios.read(ba, 0, 0);
              System.out.println((result == 0 ? "Passed" : "Failed") + " for len = 0");

              try {
                  mcios.read(ba, 0, -1);
                  System.out.println("Failed for len = -1");
              } catch (IndexOutOfBoundsException e) {
                  System.out.println("Passed for len = -1");
              }
          }
      }
      ===================== log =======================
      % java -version
      java version "1.4.0-beta_refresh"
      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 a
      Failed for len = 0
      Failed for len = -1

      The bug causes failure of the following test:
      api/javax_imageio/stream/MemoryCacheImageOutputStream/index.html#read

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

      Attachments

        Activity

          People

            dricesunw Daniel Rice (Inactive)
            mlsunw Ml Ml (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: