8130679: Writer/StringWriter.write methods do not specify index out bounds

Add throws tag indicating IndexOutOfBoundsExcepion to write() methods which have an index parameter.

Reviewed-by: chegar, prappo, rriggs
This commit is contained in:
Brian Burkhalter 2016-05-17 16:04:53 -07:00
parent 33bb9220fc
commit 9cdeece6b6
8 changed files with 108 additions and 29 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -153,13 +153,18 @@ public class BufferedWriter extends Writer {
* needed. If the requested length is at least as large as the buffer, * needed. If the requested length is at least as large as the buffer,
* however, then this method will flush the buffer and write the characters * however, then this method will flush the buffer and write the characters
* directly to the underlying stream. Thus redundant * directly to the underlying stream. Thus redundant
* <code>BufferedWriter</code>s will not copy data unnecessarily. * {@code BufferedWriter}s will not copy data unnecessarily.
* *
* @param cbuf A character array * @param cbuf A character array
* @param off Offset from which to start reading characters * @param off Offset from which to start reading characters
* @param len Number of characters to write * @param len Number of characters to write
* *
* @exception IOException If an I/O error occurs * @throws IndexOutOfBoundsException
* If {@code off} is negative, or {@code len} is negative,
* or {@code off + len} is negative or greater than the length
* of the given array
*
* @throws IOException If an I/O error occurs
*/ */
public void write(char cbuf[], int off, int len) throws IOException { public void write(char cbuf[], int off, int len) throws IOException {
synchronized (lock) { synchronized (lock) {
@ -195,17 +200,24 @@ public class BufferedWriter extends Writer {
/** /**
* Writes a portion of a String. * Writes a portion of a String.
* *
* <p> If the value of the {@code len} parameter is negative then no * @implSpec
* characters are written. This is contrary to the specification of this * While the specification of this method in the
* method in the {@linkplain java.io.Writer#write(java.lang.String,int,int) * {@linkplain java.io.Writer#write(java.lang.String,int,int) superclass}
* superclass}, which requires that an {@link IndexOutOfBoundsException} be * recommends that an {@link IndexOutOfBoundsException} be thrown
* thrown. * if {@code len} is negative or {@code off + len} is negative,
* the implementation in this class does not throw such an exception in
* these cases but instead simply writes no characters.
* *
* @param s String to be written * @param s String to be written
* @param off Offset from which to start reading characters * @param off Offset from which to start reading characters
* @param len Number of characters to be written * @param len Number of characters to be written
* *
* @exception IOException If an I/O error occurs * @throws IndexOutOfBoundsException
* If {@code off} is negative,
* or {@code off + len} is greater than the length
* of the given string
*
* @throws IOException If an I/O error occurs
*/ */
public void write(String s, int off, int len) throws IOException { public void write(String s, int off, int len) throws IOException {
synchronized (lock) { synchronized (lock) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -91,6 +91,11 @@ class CharArrayWriter extends Writer {
* @param c the data to be written * @param c the data to be written
* @param off the start offset in the data * @param off the start offset in the data
* @param len the number of chars that are written * @param len the number of chars that are written
*
* @throws IndexOutOfBoundsException
* If {@code off} is negative, or {@code len} is negative,
* or {@code off + len} is negative or greater than the length
* of the given array
*/ */
public void write(char c[], int off, int len) { public void write(char c[], int off, int len) {
if ((off < 0) || (off > c.length) || (len < 0) || if ((off < 0) || (off > c.length) || (len < 0) ||
@ -114,6 +119,11 @@ class CharArrayWriter extends Writer {
* @param str String to be written from * @param str String to be written from
* @param off Offset from which to start reading characters * @param off Offset from which to start reading characters
* @param len Number of characters to be written * @param len Number of characters to be written
*
* @throws IndexOutOfBoundsException
* If {@code off} is negative, or {@code len} is negative,
* or {@code off + len} is negative or greater than the length
* of the given string
*/ */
public void write(String str, int off, int len) { public void write(String str, int off, int len) {
synchronized (lock) { synchronized (lock) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -72,7 +72,12 @@ public abstract class FilterWriter extends Writer {
* @param off Offset from which to start reading characters * @param off Offset from which to start reading characters
* @param len Number of characters to be written * @param len Number of characters to be written
* *
* @exception IOException If an I/O error occurs * @throws IndexOutOfBoundsException
* If the values of the {@code off} and {@code len} parameters
* cause the corresponding method of the underlying {@code Writer}
* to throw an {@code IndexOutOfBoundsException}
*
* @throws IOException If an I/O error occurs
*/ */
public void write(char cbuf[], int off, int len) throws IOException { public void write(char cbuf[], int off, int len) throws IOException {
out.write(cbuf, off, len); out.write(cbuf, off, len);
@ -85,7 +90,12 @@ public abstract class FilterWriter extends Writer {
* @param off Offset from which to start reading characters * @param off Offset from which to start reading characters
* @param len Number of characters to be written * @param len Number of characters to be written
* *
* @exception IOException If an I/O error occurs * @throws IndexOutOfBoundsException
* If the values of the {@code off} and {@code len} parameters
* cause the corresponding method of the underlying {@code Writer}
* to throw an {@code IndexOutOfBoundsException}
*
* @throws IOException If an I/O error occurs
*/ */
public void write(String str, int off, int len) throws IOException { public void write(String str, int off, int len) throws IOException {
out.write(str, off, len); out.write(str, off, len);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -202,7 +202,12 @@ public class OutputStreamWriter extends Writer {
* @param off Offset from which to start writing characters * @param off Offset from which to start writing characters
* @param len Number of characters to write * @param len Number of characters to write
* *
* @exception IOException If an I/O error occurs * @throws IndexOutOfBoundsException
* If {@code off} is negative, or {@code len} is negative,
* or {@code off + len} is negative or greater than the length
* of the given array
*
* @throws IOException If an I/O error occurs
*/ */
public void write(char cbuf[], int off, int len) throws IOException { public void write(char cbuf[], int off, int len) throws IOException {
se.write(cbuf, off, len); se.write(cbuf, off, len);
@ -215,7 +220,12 @@ public class OutputStreamWriter extends Writer {
* @param off Offset from which to start writing characters * @param off Offset from which to start writing characters
* @param len Number of characters to write * @param len Number of characters to write
* *
* @exception IOException If an I/O error occurs * @throws IndexOutOfBoundsException
* If {@code off} is negative, or {@code len} is negative,
* or {@code off + len} is negative or greater than the length
* of the given string
*
* @throws IOException If an I/O error occurs
*/ */
public void write(String str, int off, int len) throws IOException { public void write(String str, int off, int len) throws IOException {
se.write(str, off, len); se.write(str, off, len);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -125,19 +125,25 @@ public class PipedWriter extends Writer {
} }
/** /**
* Writes <code>len</code> characters from the specified character array * Writes {@code len} characters from the specified character array
* starting at offset <code>off</code> to this piped output stream. * starting at offset {@code off} to this piped output stream.
* This method blocks until all the characters are written to the output * This method blocks until all the characters are written to the output
* stream. * stream.
* If a thread was reading data characters from the connected piped input * If a thread was reading data characters from the connected piped input
* stream, but the thread is no longer alive, then an * stream, but the thread is no longer alive, then an
* <code>IOException</code> is thrown. * {@code IOException} is thrown.
* *
* @param cbuf the data. * @param cbuf the data.
* @param off the start offset in the data. * @param off the start offset in the data.
* @param len the number of characters to write. * @param len the number of characters to write.
* @exception IOException if the pipe is *
* <a href=PipedOutputStream.html#BROKEN> <code>broken</code></a>, * @throws IndexOutOfBoundsException
* If {@code off} is negative, or {@code len} is negative,
* or {@code off + len} is negative or greater than the length
* of the given array
*
* @throws IOException if the pipe is
* <a href=PipedOutputStream.html#BROKEN><code>broken</code></a>,
* {@link #connect(java.io.PipedReader) unconnected}, closed * {@link #connect(java.io.PipedReader) unconnected}, closed
* or an I/O error occurs. * or an I/O error occurs.
*/ */

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -410,6 +410,11 @@ public class PrintWriter extends Writer {
* @param buf Array of characters * @param buf Array of characters
* @param off Offset from which to start writing characters * @param off Offset from which to start writing characters
* @param len Number of characters to write * @param len Number of characters to write
*
* @throws IndexOutOfBoundsException
* If the values of the {@code off} and {@code len} parameters
* cause the corresponding method of the underlying {@code Writer}
* to throw an {@code IndexOutOfBoundsException}
*/ */
public void write(char buf[], int off, int len) { public void write(char buf[], int off, int len) {
try { try {
@ -440,6 +445,11 @@ public class PrintWriter extends Writer {
* @param s A String * @param s A String
* @param off Offset from which to start writing characters * @param off Offset from which to start writing characters
* @param len Number of characters to write * @param len Number of characters to write
*
* @throws IndexOutOfBoundsException
* If the values of the {@code off} and {@code len} parameters
* cause the corresponding method of the underlying {@code Writer}
* to throw an {@code IndexOutOfBoundsException}
*/ */
public void write(String s, int off, int len) { public void write(String s, int off, int len) {
try { try {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2004, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -83,6 +83,11 @@ public class StringWriter extends Writer {
* @param cbuf Array of characters * @param cbuf Array of characters
* @param off Offset from which to start writing characters * @param off Offset from which to start writing characters
* @param len Number of characters to write * @param len Number of characters to write
*
* @throws IndexOutOfBoundsException
* If {@code off} is negative, or {@code len} is negative,
* or {@code off + len} is negative or greater than the length
* of the given array
*/ */
public void write(char cbuf[], int off, int len) { public void write(char cbuf[], int off, int len) {
if ((off < 0) || (off > cbuf.length) || (len < 0) || if ((off < 0) || (off > cbuf.length) || (len < 0) ||
@ -107,6 +112,11 @@ public class StringWriter extends Writer {
* @param str String to be written * @param str String to be written
* @param off Offset from which to start writing characters * @param off Offset from which to start writing characters
* @param len Number of characters to write * @param len Number of characters to write
*
* @throws IndexOutOfBoundsException
* If {@code off} is negative, or {@code len} is negative,
* or {@code off + len} is negative or greater than the length
* of the given string
*/ */
public void write(String str, int off, int len) { public void write(String str, int off, int len) {
buf.append(str, off, off + len); buf.append(str, off, off + len);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -32,12 +32,11 @@ package java.io;
* Most subclasses, however, will override some of the methods defined here in * Most subclasses, however, will override some of the methods defined here in
* order to provide higher efficiency, additional functionality, or both. * order to provide higher efficiency, additional functionality, or both.
* *
* @see Writer
* @see BufferedWriter * @see BufferedWriter
* @see CharArrayWriter * @see CharArrayWriter
* @see FilterWriter * @see FilterWriter
* @see OutputStreamWriter * @see OutputStreamWriter
* @see FileWriter * @see FileWriter
* @see PipedWriter * @see PipedWriter
* @see PrintWriter * @see PrintWriter
* @see StringWriter * @see StringWriter
@ -139,6 +138,12 @@ public abstract class Writer implements Appendable, Closeable, Flushable {
* @param len * @param len
* Number of characters to write * Number of characters to write
* *
* @throws IndexOutOfBoundsException
* Implementations should throw this exception
* if {@code off} is negative, or {@code len} is negative,
* or {@code off + len} is negative or greater than the length
* of the given array
*
* @throws IOException * @throws IOException
* If an I/O error occurs * If an I/O error occurs
*/ */
@ -160,6 +165,11 @@ public abstract class Writer implements Appendable, Closeable, Flushable {
/** /**
* Writes a portion of a string. * Writes a portion of a string.
* *
* @implSpec
* The implementation in this class throws an
* {@code IndexOutOfBoundsException} for the indicated conditions;
* overriding methods may choose to do otherwise.
*
* @param str * @param str
* A String * A String
* *
@ -170,8 +180,9 @@ public abstract class Writer implements Appendable, Closeable, Flushable {
* Number of characters to write * Number of characters to write
* *
* @throws IndexOutOfBoundsException * @throws IndexOutOfBoundsException
* If {@code off} is negative, or {@code len} is negative, * Implementations should throw this exception
* or {@code off+len} is negative or greater than the length * if {@code off} is negative, or {@code len} is negative,
* or {@code off + len} is negative or greater than the length
* of the given string * of the given string
* *
* @throws IOException * @throws IOException