< prev index next >

src/java.base/share/classes/java/io/InputStream.java

Print this page
rev 47874 : 8191516: OutputStream.write(byte[],int,int) could have fewer parameter bounds checks
Summary: Reduce parameter bounds checks from five to three as in InputStream::read
Reviewed-by: XXX
rev 47216 : 8187443: Forest Consolidation: Move files to unified layout
Reviewed-by: darcy, ihse

@@ -1,7 +1,7 @@
 /*
- * 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.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -162,15 +162,13 @@
      * <code>len</code> is negative, or <code>len</code> is greater than
      * <code>b.length - off</code>
      * @see        java.io.InputStream#read()
      */
     public 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.requireNonNull(b);
+        Objects.checkFromIndexSize(off, len, b.length);
+        if (len == 0) {
             return 0;
         }
 
         int c = read();
         if (c == -1) {

@@ -300,12 +298,12 @@
      *
      * @since 9
      */
     public int readNBytes(byte[] b, int off, int len) throws IOException {
         Objects.requireNonNull(b);
-        if (off < 0 || len < 0 || len > b.length - off)
-            throw new IndexOutOfBoundsException();
+        Objects.checkFromIndexSize(off, len, b.length);
+
         int n = 0;
         while (n < len) {
             int count = read(b, off + n, len - n);
             if (count < 0)
                 break;
< prev index next >