RFR JDK-8029804: (spec) BufferedWriter.write(String, int, int) IndexOutOfBoundsException

Pavel Rappo pavel.rappo at oracle.com
Mon Apr 27 16:48:31 UTC 2015


Hi everyone,

Could you please review my change for JDK-8029804

http://cr.openjdk.java.net/~prappo/8029804/webrev.00/

-------------------------------------------------------------------------------
There is an issue where java.io.BufferedWriter.write(String s, int off, int len)
doesn't check required preconditions for `off` and `len` and goes straight to
the algorithm procedures. As a result an acceptable region for (off, len) is
wider than the one declared by parent. For example, following calls should throw
IndexOutOfBoundsException, but they don't:

    bw.write("abc", 10,   0)
    bw.write("abc",  5,  -1)
    bw.write("abc", -7,   0)
    bw.write("abc", -2,  -3)
    bw.write("abc",  3, Integer.MAX_VALUE)

The problem though is not in the bug itself (it's been there for a long time),
but rather than the amendment to the javadoc which supposed to describe this
divergence is not comprehensive:

     * <p> If the value of the <tt>len</tt> parameter is negative then no
     * characters are written.  This is contrary to the specification of this
     * method in the {@linkplain java.io.Writer#write(java.lang.String,int,int)
     * superclass}, which requires that an {@link IndexOutOfBoundsException} be
     * thrown.

Suggested solution is to update the javadoc and provide a test that gives a
certain amount of confidence that the javadoc captures the actual contract.

 -Pavel




More information about the core-libs-dev mailing list