changeset 9859:95e958b6fc86

8236984: Add compatibility wrapper for IOUtils.readFully Summary: Protect third party use following readFully removal in JDK-8231139 Reviewed-by: mbalao
author andrew
date Mon, 13 Jan 2020 02:42:14 +0000
parents 54df1e566aa5
children 0545ec814419
files src/share/classes/sun/misc/IOUtils.java
diffstat 1 files changed, 29 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/misc/IOUtils.java	Fri Jan 03 18:09:11 2020 +0000
+++ b/src/share/classes/sun/misc/IOUtils.java	Mon Jan 13 02:42:14 2020 +0000
@@ -281,4 +281,33 @@
         return n;
     }
 
+    /**
+     * Compatibility wrapper for third party users of
+     * {@code sun.misc.IOUtils.readFully} following its
+     * removal in JDK-8231139.
+     *
+     * Read up to {@code length} of bytes from {@code in}
+     * until EOF is detected.
+     *
+     * @param is input stream, must not be null
+     * @param length number of bytes to read
+     * @param readAll if true, an EOFException will be thrown if not enough
+     *        bytes are read.
+     * @return bytes read
+     * @throws EOFException if there are not enough bytes in the stream
+     * @throws IOException if an I/O error occurs or {@code length} is negative
+     * @throws OutOfMemoryError if an array of the required size cannot be
+     *         allocated.
+     */
+    public static byte[] readFully(InputStream is, int length, boolean readAll)
+             throws IOException {
+        if (length < 0) {
+            throw new IOException("length cannot be negative: " + length);
+        }
+        if (readAll) {
+            return readExactlyNBytes(is, length);
+        } else {
+            return readNBytes(is, length);
+        }
+    }
 }