Skip to content
Permalink
Browse files
8276806: Use Objects.checkFromIndexSize where possible in java.base
Reviewed-by: rriggs, lancea
  • Loading branch information
stsypanov authored and Roger Riggs committed Dec 2, 2021
1 parent 30087cc commit 73a9654c2685e14454a355a16bfe3f6687966f14
@@ -1035,10 +1035,7 @@ public int read(byte[] buf, int off, int len) throws IOException {
if (buf == null) {
throw new NullPointerException();
}
int endoff = off + len;
if (off < 0 || len < 0 || endoff > buf.length || endoff < 0) {
throw new IndexOutOfBoundsException();
}
Objects.checkFromIndexSize(off, len, buf.length);
return bin.read(buf, off, len, false);
}

@@ -1207,10 +1204,7 @@ public void readFully(byte[] buf) throws IOException {
* @throws IOException If other I/O error has occurred.
*/
public void readFully(byte[] buf, int off, int len) throws IOException {
int endoff = off + len;
if (off < 0 || len < 0 || endoff > buf.length || endoff < 0) {
throw new IndexOutOfBoundsException();
}
Objects.checkFromToIndex(off, len, buf.length);
bin.readFully(buf, off, len, false);
}

@@ -32,6 +32,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@@ -718,10 +719,7 @@ public void write(byte[] buf, int off, int len) throws IOException {
if (buf == null) {
throw new NullPointerException();
}
int endoff = off + len;
if (off < 0 || len < 0 || endoff > buf.length || endoff < 0) {
throw new IndexOutOfBoundsException();
}
Objects.checkFromIndexSize(off, len, buf.length);
bout.write(buf, off, len, false);
}

@@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2021, 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;

/**
* A piped input stream should be connected
* to a piped output stream; the piped input
@@ -367,9 +369,9 @@ public synchronized int read() throws IOException {
public synchronized int read(byte[] b, int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
}
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return 0;
}

@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2021, 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;

/**
* A {@code PushbackInputStream} adds
* functionality to another input stream, namely
@@ -162,9 +164,9 @@ public int read(byte[] b, int off, int len) throws IOException {
ensureOpen();
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
}
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return 0;
}

@@ -25,8 +25,8 @@

package java.io;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.Objects;
import java.util.Vector;

/**
@@ -189,9 +189,9 @@ public int read(byte[] b, int off, int len) throws IOException {
return -1;
} else if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
}
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return 0;
}
do {
@@ -430,9 +430,7 @@ public int codePointBefore(int index) {
* {@code beginIndex} is larger than {@code endIndex}.
*/
public int codePointCount(int beginIndex, int endIndex) {
if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
throw new IndexOutOfBoundsException();
}
Preconditions.checkFromToIndex(beginIndex, endIndex, length(), null);
if (isLatin1()) {
return endIndex - beginIndex;
}
@@ -28,6 +28,7 @@
import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.Objects;

/**
* Implements an input stream filter for compressing data in the "deflate"
@@ -172,9 +173,9 @@ public int read(byte[] b, int off, int len) throws IOException {
ensureOpen();
if (b == null) {
throw new NullPointerException("Null buffer for read");
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
}
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return 0;
}

@@ -29,6 +29,7 @@
import java.io.InputStream;
import java.io.IOException;
import java.io.EOFException;
import java.util.Objects;

/**
* This class implements a stream filter for uncompressing data in the
@@ -142,9 +143,9 @@ public int read(byte[] b, int off, int len) throws IOException {
ensureOpen();
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
}
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return 0;
}
try {
@@ -28,6 +28,7 @@
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Objects;

/**
* Implements an output stream filter for uncompressing data stored in the
@@ -223,9 +224,9 @@ public void write(byte[] b, int off, int len) throws IOException {
ensureOpen();
if (b == null) {
throw new NullPointerException("Null buffer for read");
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
}
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return;
}

@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2021, 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
@@ -30,6 +30,7 @@
import java.io.EOFException;
import java.io.PushbackInputStream;
import java.nio.charset.Charset;
import java.util.Objects;

import sun.nio.cs.UTF_8;

@@ -182,9 +183,8 @@ public int available() throws IOException {
*/
public int read(byte[] b, int off, int len) throws IOException {
ensureOpen();
if (off < 0 || len < 0 || off > b.length - len) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return 0;
}

@@ -28,6 +28,7 @@
import java.io.OutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Objects;
import java.util.Vector;
import java.util.HashSet;
import static java.util.zip.ZipConstants64.*;
@@ -333,9 +334,8 @@ public synchronized void write(byte[] b, int off, int len)
throws IOException
{
ensureOpen();
if (off < 0 || len < 0 || off > b.length - len) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return;
}

1 comment on commit 73a9654

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 73a9654 Dec 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.