Harmony
  1. Harmony
  2. HARMONY-1175

[classlib][io] BufferedWriter.write() exception behavior differ from RI

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Cannot Reproduce
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Classlib
    • Labels:
      None

      Description

      1. Harmony implementation of BufferedWriter.write(String, int, int) throws an exception if len is negative while spec says (and RI follows it)
      public void write(String s, int off, int len) throws IOExceptionWrite a portion of a String.
      If the value of the len parameter is negative then no characters are written. This is contrary to the specification of this method in the superclass, which requires that an IndexOutOfBoundsException be thrown.

      2. Harmony implementation of BufferedWriter.write(char[], int, int) throws ArrayIndexOutOfBoundsException while RI throws IndexOutOfBoundsException.

      =========================== Test ===============================
      import java.io.*;
      public class bug9588 {

      static void test(String name, Object buf, int offset, int count) {
      try {
      System.err.println(name);
      ByteArrayOutputStream baos = new ByteArrayOutputStream(200);
      OutputStreamWriter osw = new OutputStreamWriter(baos);
      BufferedWriter bw = new BufferedWriter((Writer)osw);
      if (buf instanceof String)

      { bw.write((String)buf, offset, count); }

      else

      { bw.write((char[])buf, offset, count); }

      bw.flush();
      System.err.println("Number of written chars: " +(baos.toByteArray()).length);
      } catch (Exception e)

      { e.printStackTrace(); }


      }

      public static void main (String [] args) throws Exception {
      String str = "abcde";
      char[] ch = new char[]

      {'a', 'b', 'c', 'd', 'e'}

      ;
      test("1", str, 1, -1);
      test("2", str, -1, 1);
      test("3", str, -1, -1);
      test("4", str, 7, 1);
      test("5", str, 3, 4);
      test("6", ch, 1, -1);
      test("7", ch, -1, 1);
      test("8", ch, -1, -1);
      test("9", ch, 7, 1);
      test("10", ch, 3, 4);
      }
      }

      =================== Output ==========================

      RI

      1
      Number of written chars: 0
      2
      java.lang.StringIndexOutOfBoundsException: String index out of range: -1
      at java.lang.String.getChars(II[CI)V(Unknown Source)
      at java.io.BufferedWriter.write(BufferedWriter.java:208)
      at bug9588.test(bug9588.java:12)
      at bug9588.main(bug9588.java:27)
      3
      Number of written chars: 0
      4
      java.lang.StringIndexOutOfBoundsException: String index out of range: 8
      at java.lang.String.getChars(II[CI)V(Unknown Source)
      at java.io.BufferedWriter.write(BufferedWriter.java:208)
      at bug9588.test(bug9588.java:12)
      at bug9588.main(bug9588.java:29)
      5
      java.lang.StringIndexOutOfBoundsException: String index out of range: 7
      at java.lang.String.getChars(II[CI)V(Unknown Source)
      at java.io.BufferedWriter.write(BufferedWriter.java:208)
      at bug9588.test(bug9588.java:12)
      at bug9588.main(bug9588.java:30)
      6
      java.lang.IndexOutOfBoundsException
      at java.io.BufferedWriter.write(BufferedWriter.java:160)
      at bug9588.test(bug9588.java:14)
      at bug9588.main(bug9588.java:31)
      7
      java.lang.IndexOutOfBoundsException
      at java.io.BufferedWriter.write(BufferedWriter.java:160)
      at bug9588.test(bug9588.java:14)
      at bug9588.main(bug9588.java:32)
      8
      java.lang.IndexOutOfBoundsException
      at java.io.BufferedWriter.write(BufferedWriter.java:160)
      at bug9588.test(bug9588.java:14)
      at bug9588.main(bug9588.java:33)
      9
      java.lang.IndexOutOfBoundsException
      at java.io.BufferedWriter.write(BufferedWriter.java:160)
      at bug9588.test(bug9588.java:14)
      at bug9588.main(bug9588.java:34)
      10
      java.lang.IndexOutOfBoundsException
      at java.io.BufferedWriter.write(BufferedWriter.java:160)
      at bug9588.test(bug9588.java:14)
      at bug9588.main(bug9588.java:35)

      Harmony

      1
      java.lang.StringIndexOutOfBoundsException
      at java.io.BufferedWriter.write(BufferedWriter.java:291)
      at bug9588.test(bug9588.java:12)
      at bug9588.main(bug9588.java:26)
      2
      java.lang.StringIndexOutOfBoundsException
      at java.io.BufferedWriter.write(BufferedWriter.java:291)
      at bug9588.test(bug9588.java:12)
      at bug9588.main(bug9588.java:27)
      3
      java.lang.StringIndexOutOfBoundsException
      at java.io.BufferedWriter.write(BufferedWriter.java:291)
      at bug9588.test(bug9588.java:12)
      at bug9588.main(bug9588.java:28)
      4
      java.lang.StringIndexOutOfBoundsException
      at java.io.BufferedWriter.write(BufferedWriter.java:291)
      at bug9588.test(bug9588.java:12)
      at bug9588.main(bug9588.java:29)
      5
      java.lang.StringIndexOutOfBoundsException
      at java.io.BufferedWriter.write(BufferedWriter.java:291)
      at bug9588.test(bug9588.java:12)
      at bug9588.main(bug9588.java:30)
      6
      java.lang.ArrayIndexOutOfBoundsException
      at java.io.BufferedWriter.write(BufferedWriter.java:198)
      at bug9588.test(bug9588.java:14)
      at bug9588.main(bug9588.java:31)
      7
      java.lang.ArrayIndexOutOfBoundsException
      at java.io.BufferedWriter.write(BufferedWriter.java:198)
      at bug9588.test(bug9588.java:14)
      at bug9588.main(bug9588.java:32)
      8
      java.lang.ArrayIndexOutOfBoundsException
      at java.io.BufferedWriter.write(BufferedWriter.java:198)
      at bug9588.test(bug9588.java:14)
      at bug9588.main(bug9588.java:33)
      9
      java.lang.ArrayIndexOutOfBoundsException
      at java.io.BufferedWriter.write(BufferedWriter.java:198)
      at bug9588.test(bug9588.java:14)
      at bug9588.main(bug9588.java:34)
      10
      java.lang.ArrayIndexOutOfBoundsException
      at java.io.BufferedWriter.write(BufferedWriter.java:198)
      at bug9588.test(bug9588.java:14)
      at bug9588.main(bug9588.java:35)

      1. 1175-BufferedWriterTest.patch
        2 kB
        Denis Kishenko
      2. 1175-BufferedWriter.patch
        6 kB
        Denis Kishenko

        Activity

        Denis Kishenko created issue -
        Denis Kishenko made changes -
        Field Original Value New Value
        Attachment 1175-BufferedWriter.patch [ 12338809 ]
        Denis Kishenko made changes -
        Attachment 1175-BufferedWriterTest.patch [ 12338810 ]
        Paulex Yang made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Cannot Reproduce [ 5 ]
        Paulex Yang made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Assignee Paulex Yang [ paulex ]

          People

          • Assignee:
            Paulex Yang
            Reporter:
            Denis Kishenko
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development