diff --git a/src/java.base/share/classes/java/io/CharArrayWriter.java b/src/java.base/share/classes/java/io/CharArrayWriter.java index 20ce11d8afd..72320eecdfe 100644 --- a/src/java.base/share/classes/java/io/CharArrayWriter.java +++ b/src/java.base/share/classes/java/io/CharArrayWriter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -26,6 +26,7 @@ package java.io; import java.util.Arrays; +import java.util.Objects; /** * This class implements a character buffer that can be used as a Writer. @@ -97,10 +98,8 @@ public class CharArrayWriter extends Writer { * of the given array */ public void write(char[] c, int off, int len) { - if ((off < 0) || (off > c.length) || (len < 0) || - ((off + len) > c.length) || ((off + len) < 0)) { - throw new IndexOutOfBoundsException(); - } else if (len == 0) { + Objects.checkFromIndexSize(off, len, c.length); + if (len == 0) { return; } synchronized (lock) { diff --git a/src/java.base/share/classes/java/io/FilterOutputStream.java b/src/java.base/share/classes/java/io/FilterOutputStream.java index df2f4c273c7..0afe78b0f53 100644 --- a/src/java.base/share/classes/java/io/FilterOutputStream.java +++ b/src/java.base/share/classes/java/io/FilterOutputStream.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,6 +25,8 @@ package java.io; +import java.util.Objects; + /** * This class is the superclass of all classes that filter output * streams. These streams sit on top of an already existing output @@ -131,8 +133,7 @@ public class FilterOutputStream extends OutputStream { */ @Override public void write(byte[] b, int off, int len) throws IOException { - if ((off | len | (b.length - (len + off)) | (off + len)) < 0) - throw new IndexOutOfBoundsException(); + Objects.checkFromIndexSize(off, len, b.length); for (int i = 0 ; i < len ; i++) { write(b[off + i]); diff --git a/src/java.base/share/classes/java/io/LineNumberInputStream.java b/src/java.base/share/classes/java/io/LineNumberInputStream.java index 4557ef25795..1d07a0daad2 100644 --- a/src/java.base/share/classes/java/io/LineNumberInputStream.java +++ b/src/java.base/share/classes/java/io/LineNumberInputStream.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1995, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,6 +25,8 @@ package java.io; +import java.util.Objects; + /** * This class is an input stream filter that provides the added * functionality of keeping track of the current line number. @@ -129,10 +131,9 @@ public class LineNumberInputStream extends FilterInputStream { public int read(byte[] b, int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); - } else if ((off < 0) || (off > b.length) || (len < 0) || - ((off + len) > b.length) || ((off + len) < 0)) { - throw new IndexOutOfBoundsException(); - } else if (len == 0) { + } + Objects.checkFromIndexSize(off, len, b.length); + if (len == 0) { return 0; } diff --git a/src/java.base/share/classes/java/io/PipedOutputStream.java b/src/java.base/share/classes/java/io/PipedOutputStream.java index 425920d848e..6a8acd5803e 100644 --- a/src/java.base/share/classes/java/io/PipedOutputStream.java +++ b/src/java.base/share/classes/java/io/PipedOutputStream.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1995, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,7 +25,7 @@ package java.io; -import java.io.*; +import java.util.Objects; /** * A piped output stream can be connected to a piped input stream @@ -144,10 +144,9 @@ public class PipedOutputStream extends OutputStream { throw new IOException("Pipe not connected"); } else if (b == null) { throw new NullPointerException(); - } else if ((off < 0) || (off > b.length) || (len < 0) || - ((off + len) > b.length) || ((off + len) < 0)) { - throw new IndexOutOfBoundsException(); - } else if (len == 0) { + } + Objects.checkFromIndexSize(off, len, b.length); + if (len == 0) { return; } sink.receive(b, off, len); diff --git a/src/java.base/share/classes/java/io/PipedWriter.java b/src/java.base/share/classes/java/io/PipedWriter.java index a74a9c140b8..f7fed843386 100644 --- a/src/java.base/share/classes/java/io/PipedWriter.java +++ b/src/java.base/share/classes/java/io/PipedWriter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -26,6 +26,8 @@ package java.io; +import java.util.Objects; + /** * Piped character-output streams. * @@ -150,9 +152,8 @@ public class PipedWriter extends Writer { public void write(char[] cbuf, int off, int len) throws IOException { if (sink == null) { throw new IOException("Pipe not connected"); - } else if ((off | len | (off + len) | (cbuf.length - (off + len))) < 0) { - throw new IndexOutOfBoundsException(); } + Objects.checkFromIndexSize(off, len, cbuf.length); sink.receive(cbuf, off, len); } diff --git a/src/java.base/share/classes/java/io/StringWriter.java b/src/java.base/share/classes/java/io/StringWriter.java index 3491490cf99..afb5c41fdfc 100644 --- a/src/java.base/share/classes/java/io/StringWriter.java +++ b/src/java.base/share/classes/java/io/StringWriter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -26,6 +26,8 @@ package java.io; +import java.util.Objects; + /** * A character stream that collects its output in a string buffer, which can * then be used to construct a string. @@ -90,10 +92,8 @@ public class StringWriter extends Writer { * of the given array */ public void write(char[] cbuf, int off, int len) { - if ((off < 0) || (off > cbuf.length) || (len < 0) || - ((off + len) > cbuf.length) || ((off + len) < 0)) { - throw new IndexOutOfBoundsException(); - } else if (len == 0) { + Objects.checkFromIndexSize(off, len, cbuf.length); + if (len == 0) { return; } buf.append(cbuf, off, len);