8139706: JarFile.getBytes could use InputStream.readNBytes
Reviewed-by: sherman, chegar, alanb
--- a/jdk/src/java.base/share/classes/java/util/jar/JarFile.java Fri Oct 16 19:05:49 2015 +0300
+++ b/jdk/src/java.base/share/classes/java/util/jar/JarFile.java Sun Oct 18 01:43:06 2015 +0200
@@ -438,10 +438,19 @@
private byte[] getBytes(ZipEntry ze) throws IOException {
try (InputStream is = super.getInputStream(ze)) {
int len = (int)ze.getSize();
- byte[] b = is.readAllBytes();
- if (len != -1 && b.length != len)
- throw new EOFException("Expected:" + len + ", read:" + b.length);
-
+ int bytesRead;
+ byte[] b;
+ // trust specified entry sizes when reasonably small
+ if (len != -1 && len <= 65535) {
+ b = new byte[len];
+ bytesRead = is.readNBytes(b, 0, len);
+ } else {
+ b = is.readAllBytes();
+ bytesRead = b.length;
+ }
+ if (len != -1 && len != bytesRead) {
+ throw new EOFException("Expected:" + len + ", read:" + bytesRead);
+ }
return b;
}
}