< prev index next >

src/java.base/share/classes/java/util/jar/JarFile.java

Print this page
rev 12879 : 8139706: JarFile.getBytes could use InputStream.readNBytes
Reviewed-by: chegar, sherman


 421                 JarVerifier.debug.println("done with meta!");
 422             }
 423 
 424             if (jv.nothingToVerify()) {
 425                 if (JarVerifier.debug != null) {
 426                     JarVerifier.debug.println("nothing to verify!");
 427                 }
 428                 jv = null;
 429                 verify = false;
 430             }
 431         }
 432     }
 433 
 434     /*
 435      * Reads all the bytes for a given entry. Used to process the
 436      * META-INF files.
 437      */
 438     private byte[] getBytes(ZipEntry ze) throws IOException {
 439         try (InputStream is = super.getInputStream(ze)) {
 440             int len = (int)ze.getSize();
 441             byte[] b = is.readAllBytes();
 442             if (len != -1 && b.length != len)
 443                 throw new EOFException("Expected:" + len + ", read:" + b.length);
 444 







 445             return b;
 446         }
 447     }
 448 
 449     /**
 450      * Returns an input stream for reading the contents of the specified
 451      * zip file entry.
 452      * @param ze the zip file entry
 453      * @return an input stream for reading the contents of the specified
 454      *         zip file entry
 455      * @throws ZipException if a zip file format error has occurred
 456      * @throws IOException if an I/O error has occurred
 457      * @throws SecurityException if any of the jar file entries
 458      *         are incorrectly signed.
 459      * @throws IllegalStateException
 460      *         may be thrown if the jar file has been closed
 461      */
 462     public synchronized InputStream getInputStream(ZipEntry ze)
 463         throws IOException
 464     {




 421                 JarVerifier.debug.println("done with meta!");
 422             }
 423 
 424             if (jv.nothingToVerify()) {
 425                 if (JarVerifier.debug != null) {
 426                     JarVerifier.debug.println("nothing to verify!");
 427                 }
 428                 jv = null;
 429                 verify = false;
 430             }
 431         }
 432     }
 433 
 434     /*
 435      * Reads all the bytes for a given entry. Used to process the
 436      * META-INF files.
 437      */
 438     private byte[] getBytes(ZipEntry ze) throws IOException {
 439         try (InputStream is = super.getInputStream(ze)) {
 440             int len = (int)ze.getSize();
 441             byte[] b;
 442             // trust specified entry sizes when reasonably small
 443             if (len != -1 && len <= 65535) {
 444                 b = new byte[len];
 445                 len = is.readNBytes(b, 0, len);
 446                 if (len != b.length) {
 447                     throw new EOFException("Expected:" + b.length + ", read:" + len);
 448                 }
 449             } else {
 450                 b = is.readAllBytes();
 451             }
 452             return b;
 453         }
 454     }
 455 
 456     /**
 457      * Returns an input stream for reading the contents of the specified
 458      * zip file entry.
 459      * @param ze the zip file entry
 460      * @return an input stream for reading the contents of the specified
 461      *         zip file entry
 462      * @throws ZipException if a zip file format error has occurred
 463      * @throws IOException if an I/O error has occurred
 464      * @throws SecurityException if any of the jar file entries
 465      *         are incorrectly signed.
 466      * @throws IllegalStateException
 467      *         may be thrown if the jar file has been closed
 468      */
 469     public synchronized InputStream getInputStream(ZipEntry ze)
 470         throws IOException
 471     {


< prev index next >