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

(str spec) String(byte[], int, int, Charset) should be clearer when IndexOutOfBoundsException is thrown

XMLWordPrintable

    • b43
    • Verified

        As stated in javadocs this constructor must throw IndexOutOfBoundsException if the offset and length arguments index characters outside the bounds of the bytes array.

        http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#String-byte:A-int-int-java.nio.charset.Charset-

        In the following example the constructor is invoked with 3 elements in the array. Offset value is equal to 3, so it indexes the element which is out of bounds, as far as indexing starts with 0.
        The expected behavior in this case is throwing the IndexOutOfBoundsException exception, but the empty string is formed instead. Nevertheless, the exception is thrown if the length (third parameter) is nonzero.

        The following code must throw exception according to the spec but it does not:

             new String(new byte[] { 9, 9, 9 }, 3, 0, Charset.defaultCharset());


        JCK test which would be shipped with JCK8a (not present in JCK8) fails due to this issue:
        api/java_lang/String/index2.html#CtorBytesOffsetLenCharset








              sherman Xueming Shen
              kfertikov Kirill Fertikov (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              11 Start watching this issue

                Created:
                Updated:
                Resolved: