8299600: Use Objects.check*() where appropriate in java.io

Reviewed-by: alanb, bpb
This commit is contained in:
Sergey Tsypanov 2023-01-06 21:01:21 +00:00 committed by Brian Burkhalter
parent 4a95c74b76
commit d086e82b3c
6 changed files with 28 additions and 27 deletions

View file

@ -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. * 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
@ -26,6 +26,7 @@
package java.io; package java.io;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects;
/** /**
* This class implements a character buffer that can be used as a Writer. * 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 * 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) || Objects.checkFromIndexSize(off, len, c.length);
((off + len) > c.length) || ((off + len) < 0)) { if (len == 0) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return; return;
} }
synchronized (lock) { synchronized (lock) {

View file

@ -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. * 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
@ -25,6 +25,8 @@
package java.io; package java.io;
import java.util.Objects;
/** /**
* This class is the superclass of all classes that filter output * This class is the superclass of all classes that filter output
* streams. These streams sit on top of an already existing output * streams. These streams sit on top of an already existing output
@ -131,8 +133,7 @@ public class FilterOutputStream extends OutputStream {
*/ */
@Override @Override
public void write(byte[] b, int off, int len) throws IOException { public void write(byte[] b, int off, int len) throws IOException {
if ((off | len | (b.length - (len + off)) | (off + len)) < 0) Objects.checkFromIndexSize(off, len, b.length);
throw new IndexOutOfBoundsException();
for (int i = 0 ; i < len ; i++) { for (int i = 0 ; i < len ; i++) {
write(b[off + i]); write(b[off + i]);

View file

@ -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. * 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
@ -25,6 +25,8 @@
package java.io; package java.io;
import java.util.Objects;
/** /**
* This class is an input stream filter that provides the added * This class is an input stream filter that provides the added
* functionality of keeping track of the current line number. * 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 { public int read(byte[] b, int off, int len) throws IOException {
if (b == null) { if (b == null) {
throw new NullPointerException(); throw new NullPointerException();
} else if ((off < 0) || (off > b.length) || (len < 0) || }
((off + len) > b.length) || ((off + len) < 0)) { Objects.checkFromIndexSize(off, len, b.length);
throw new IndexOutOfBoundsException(); if (len == 0) {
} else if (len == 0) {
return 0; return 0;
} }

View file

@ -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. * 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
@ -25,7 +25,7 @@
package java.io; package java.io;
import java.io.*; import java.util.Objects;
/** /**
* A piped output stream can be connected to a piped input stream * 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"); throw new IOException("Pipe not connected");
} else if (b == null) { } else if (b == null) {
throw new NullPointerException(); throw new NullPointerException();
} else if ((off < 0) || (off > b.length) || (len < 0) || }
((off + len) > b.length) || ((off + len) < 0)) { Objects.checkFromIndexSize(off, len, b.length);
throw new IndexOutOfBoundsException(); if (len == 0) {
} else if (len == 0) {
return; return;
} }
sink.receive(b, off, len); sink.receive(b, off, len);

View file

@ -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. * 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
@ -26,6 +26,8 @@
package java.io; package java.io;
import java.util.Objects;
/** /**
* Piped character-output streams. * Piped character-output streams.
* *
@ -150,9 +152,8 @@ public class PipedWriter extends Writer {
public void write(char[] cbuf, int off, int len) throws IOException { public void write(char[] cbuf, int off, int len) throws IOException {
if (sink == null) { if (sink == null) {
throw new IOException("Pipe not connected"); 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); sink.receive(cbuf, off, len);
} }

View file

@ -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. * 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
@ -26,6 +26,8 @@
package java.io; package java.io;
import java.util.Objects;
/** /**
* A character stream that collects its output in a string buffer, which can * A character stream that collects its output in a string buffer, which can
* then be used to construct a string. * then be used to construct a string.
@ -90,10 +92,8 @@ public class StringWriter extends Writer {
* of the given array * 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) || Objects.checkFromIndexSize(off, len, cbuf.length);
((off + len) > cbuf.length) || ((off + len) < 0)) { if (len == 0) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return; return;
} }
buf.append(cbuf, off, len); buf.append(cbuf, off, len);