Index: BufferedWriter.java =================================================================== --- BufferedWriter.java (revision 430789) +++ BufferedWriter.java (working copy) @@ -157,45 +157,43 @@ */ public void write(char[] cbuf, int offset, int count) throws IOException { - // avoid int overflow - if (0 <= offset && offset <= cbuf.length && 0 <= count - && count <= cbuf.length - offset) { - synchronized (lock) { - if (isOpen()) { - if (pos == 0 && count >= this.buf.length) { - out.write(cbuf, offset, count); - return; - } - int available = this.buf.length - pos; - if (count < available) - available = count; - if (available > 0) { - System - .arraycopy(cbuf, offset, this.buf, pos, - available); - pos += available; - } - if (pos == this.buf.length) { - out.write(this.buf, 0, this.buf.length); - pos = 0; - if (count > available) { - offset += available; - available = count - available; - if (available >= this.buf.length) { - out.write(cbuf, offset, available); - return; - } - System.arraycopy(cbuf, offset, this.buf, pos, - available); - pos += available; - } - } - } else - throw new IOException(org.apache.harmony.luni.util.Msg - .getString("K005d")); //$NON-NLS-1$ - } - } else - throw new ArrayIndexOutOfBoundsException(); + if (offset < 0 || count < 0 || offset > cbuf.length || count > cbuf.length - offset) { + throw new IndexOutOfBoundsException(); + } + synchronized (lock) { + if (isOpen()) { + if (pos == 0 && count >= this.buf.length) { + out.write(cbuf, offset, count); + return; + } + int available = this.buf.length - pos; + if (count < available) + available = count; + if (available > 0) { + System + .arraycopy(cbuf, offset, this.buf, pos, + available); + pos += available; + } + if (pos == this.buf.length) { + out.write(this.buf, 0, this.buf.length); + pos = 0; + if (count > available) { + offset += available; + available = count - available; + if (available >= this.buf.length) { + out.write(cbuf, offset, available); + return; + } + System.arraycopy(cbuf, offset, this.buf, pos, + available); + pos += available; + } + } + } else + throw new IOException(org.apache.harmony.luni.util.Msg + .getString("K005d")); //$NON-NLS-1$ + } } /** @@ -241,46 +239,47 @@ */ public void write(String str, int offset, int count) throws IOException { - // avoid int overflow - if (0 <= offset && offset <= str.length() && 0 <= count - && count <= str.length() - offset) { - synchronized (lock) { - if (isOpen()) { - if (pos == 0 && count >= buf.length) { - char[] chars = new char[count]; - str.getChars(offset, offset + count, chars, 0); - out.write(chars, 0, count); - return; - } - int available = buf.length - pos; - if (count < available) - available = count; - if (available > 0) { - str.getChars(offset, offset + available, buf, pos); - pos += available; - } - if (pos == buf.length) { - out.write(this.buf, 0, this.buf.length); - pos = 0; - if (count > available) { - offset += available; - available = count - available; - if (available >= buf.length) { - char[] chars = new char[count]; - str.getChars(offset, offset + available, chars, - 0); - out.write(chars, 0, available); - return; - } - str.getChars(offset, offset + available, buf, pos); - pos += available; - } - } - } else - throw new IOException(org.apache.harmony.luni.util.Msg - .getString("K005d")); //$NON-NLS-1$ - } - } else - throw new StringIndexOutOfBoundsException(); + if (count < 0) { + return; + } + if (offset < 0 || offset > str.length() || count > str.length() - offset) { + throw new StringIndexOutOfBoundsException(); + } + synchronized (lock) { + if (isOpen()) { + if (pos == 0 && count >= buf.length) { + char[] chars = new char[count]; + str.getChars(offset, offset + count, chars, 0); + out.write(chars, 0, count); + return; + } + int available = buf.length - pos; + if (count < available) + available = count; + if (available > 0) { + str.getChars(offset, offset + available, buf, pos); + pos += available; + } + if (pos == buf.length) { + out.write(this.buf, 0, this.buf.length); + pos = 0; + if (count > available) { + offset += available; + available = count - available; + if (available >= buf.length) { + char[] chars = new char[count]; + str.getChars(offset, offset + available, chars, + 0); + out.write(chars, 0, available); + return; + } + str.getChars(offset, offset + available, buf, pos); + pos += available; + } + } + } else + throw new IOException(org.apache.harmony.luni.util.Msg + .getString("K005d")); //$NON-NLS-1$ + } } }