src/share/classes/java/io/ByteArrayInputStream.java

Print this page

        

@@ -177,14 +177,18 @@
         if (b == null) {
             throw new NullPointerException();
         } else if (off < 0 || len < 0 || len > b.length - off) {
             throw new IndexOutOfBoundsException();
         }
+
         if (pos >= count) {
             return -1;
         }
-        if (pos + len > count) {
+
+        int newpos = pos + len;
+        if (newpos - count > 0) {
+            // overflow-conscious code
             len = count - pos;
         }
         if (len <= 0) {
             return 0;
         }

@@ -204,19 +208,18 @@
      *
      * @param   n   the number of bytes to be skipped.
      * @return  the actual number of bytes skipped.
      */
     public synchronized long skip(long n) {
-        if (pos + n > count) {
-            n = count - pos;
+        long k = count - pos;
+        if (n < k) {
+            k = n < 0 ? 0 : n;
         }
-        if (n < 0) {
-            return 0;
+
+        pos += k;
+        return k;
         }
-        pos += n;
-        return n;
-    }
 
     /**
      * Returns the number of remaining bytes that can be read (or skipped over)
      * from this input stream.
      * <p>