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.
*
* 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) {

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.
*
* 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]);

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.
*
* 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;
}

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.
*
* 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);

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.
*
* 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);
}

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.
*
* 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);