mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8191516: OutputStream.write(byte[],int,int) could have fewer parameter bounds checks
Reduce parameter bounds checks from five to three as in InputStream::read Reviewed-by: psandoz
This commit is contained in:
parent
83719bc13b
commit
adb156a9b2
2 changed files with 12 additions and 17 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1994, 2017, 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
|
||||||
|
@ -164,11 +164,9 @@ public abstract class InputStream implements Closeable {
|
||||||
* @see java.io.InputStream#read()
|
* @see java.io.InputStream#read()
|
||||||
*/
|
*/
|
||||||
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) {
|
Objects.requireNonNull(b);
|
||||||
throw new NullPointerException();
|
Objects.checkFromIndexSize(off, len, b.length);
|
||||||
} else if (off < 0 || len < 0 || len > b.length - off) {
|
if (len == 0) {
|
||||||
throw new IndexOutOfBoundsException();
|
|
||||||
} else if (len == 0) {
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -302,8 +300,8 @@ public abstract class InputStream implements Closeable {
|
||||||
*/
|
*/
|
||||||
public int readNBytes(byte[] b, int off, int len) throws IOException {
|
public int readNBytes(byte[] b, int off, int len) throws IOException {
|
||||||
Objects.requireNonNull(b);
|
Objects.requireNonNull(b);
|
||||||
if (off < 0 || len < 0 || len > b.length - off)
|
Objects.checkFromIndexSize(off, len, b.length);
|
||||||
throw new IndexOutOfBoundsException();
|
|
||||||
int n = 0;
|
int n = 0;
|
||||||
while (n < len) {
|
while (n < len) {
|
||||||
int count = read(b, off + n, len - n);
|
int count = read(b, off + n, len - n);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1994, 2004, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1994, 2017, 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 abstract class is the superclass of all classes representing
|
* This abstract class is the superclass of all classes representing
|
||||||
* an output stream of bytes. An output stream accepts output bytes
|
* an output stream of bytes. An output stream accepts output bytes
|
||||||
|
@ -104,14 +106,9 @@ public abstract class OutputStream implements Closeable, Flushable {
|
||||||
* stream is closed.
|
* stream is closed.
|
||||||
*/
|
*/
|
||||||
public void write(byte b[], int off, int len) throws IOException {
|
public void write(byte b[], int off, int len) throws IOException {
|
||||||
if (b == null) {
|
Objects.requireNonNull(b);
|
||||||
throw new NullPointerException();
|
Objects.checkFromIndexSize(off, len, b.length);
|
||||||
} else if ((off < 0) || (off > b.length) || (len < 0) ||
|
// len == 0 condition implicitly handled by loop bounds
|
||||||
((off + len) > b.length) || ((off + len) < 0)) {
|
|
||||||
throw new IndexOutOfBoundsException();
|
|
||||||
} else if (len == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (int i = 0 ; i < len ; i++) {
|
for (int i = 0 ; i < len ; i++) {
|
||||||
write(b[off + i]);
|
write(b[off + i]);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue