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

Print this page
rev 3516 : 7021582: convert jar/zip code and tests to use try-with-resources
Reviewed-by: alanb, dholmes, sherman


 359             if (JarVerifier.debug != null) {
 360                 JarVerifier.debug.println("done with meta!");
 361             }
 362 
 363             if (jv.nothingToVerify()) {
 364                 if (JarVerifier.debug != null) {
 365                     JarVerifier.debug.println("nothing to verify!");
 366                 }
 367                 jv = null;
 368                 verify = false;
 369             }
 370         }
 371     }
 372 
 373     /*
 374      * Reads all the bytes for a given entry. Used to process the
 375      * META-INF files.
 376      */
 377     private byte[] getBytes(ZipEntry ze) throws IOException {
 378         byte[] b = new byte[(int)ze.getSize()];
 379         DataInputStream is = new DataInputStream(super.getInputStream(ze));
 380         is.readFully(b, 0, b.length);
 381         is.close();
 382         return b;
 383     }
 384 
 385     /**
 386      * Returns an input stream for reading the contents of the specified
 387      * zip file entry.
 388      * @param ze the zip file entry
 389      * @return an input stream for reading the contents of the specified
 390      *         zip file entry
 391      * @throws ZipException if a zip file format error has occurred
 392      * @throws IOException if an I/O error has occurred
 393      * @throws SecurityException if any of the jar file entries
 394      *         are incorrectly signed.
 395      * @throws IllegalStateException
 396      *         may be thrown if the jar file has been closed
 397      */
 398     public synchronized InputStream getInputStream(ZipEntry ze)
 399         throws IOException
 400     {
 401         maybeInstantiateVerifier();


 463                 }
 464             }
 465         }
 466         return manEntry;
 467     }
 468 
 469     // Returns true iff this jar file has a manifest with a class path
 470     // attribute. Returns false if there is no manifest or the manifest
 471     // does not contain a "Class-Path" attribute. Currently exported to
 472     // core libraries via sun.misc.SharedSecrets.
 473     boolean hasClassPathAttribute() throws IOException {
 474         if (computedHasClassPathAttribute) {
 475             return hasClassPathAttribute;
 476         }
 477 
 478         hasClassPathAttribute = false;
 479         if (!isKnownToNotHaveClassPathAttribute()) {
 480             JarEntry manEntry = getManEntry();
 481             if (manEntry != null) {
 482                 byte[] b = new byte[(int)manEntry.getSize()];
 483                 DataInputStream dis = new DataInputStream(
 484                                                           super.getInputStream(manEntry));
 485                 dis.readFully(b, 0, b.length);
 486                 dis.close();
 487 
 488                 int last = b.length - src.length;
 489                 int i = 0;
 490                 next:
 491                 while (i<=last) {
 492                     for (int j=9; j>=0; j--) {
 493                         char c = (char) b[i+j];
 494                         c = (((c-'A')|('Z'-c)) >= 0) ? (char)(c + 32) : c;
 495                         if (c != src[j]) {
 496                             i += Math.max(j + 1 - lastOcc[c&0x7F], optoSft[j]);
 497                             continue next;
 498                         }
 499                     }
 500                     hasClassPathAttribute = true;
 501                     break;
 502                 }
 503             }
 504         }
 505         computedHasClassPathAttribute = true;
 506         return hasClassPathAttribute;




 359             if (JarVerifier.debug != null) {
 360                 JarVerifier.debug.println("done with meta!");
 361             }
 362 
 363             if (jv.nothingToVerify()) {
 364                 if (JarVerifier.debug != null) {
 365                     JarVerifier.debug.println("nothing to verify!");
 366                 }
 367                 jv = null;
 368                 verify = false;
 369             }
 370         }
 371     }
 372 
 373     /*
 374      * Reads all the bytes for a given entry. Used to process the
 375      * META-INF files.
 376      */
 377     private byte[] getBytes(ZipEntry ze) throws IOException {
 378         byte[] b = new byte[(int)ze.getSize()];
 379         try (DataInputStream is = new DataInputStream(super.getInputStream(ze))) {
 380             is.readFully(b, 0, b.length);
 381         }
 382         return b;
 383     }
 384 
 385     /**
 386      * Returns an input stream for reading the contents of the specified
 387      * zip file entry.
 388      * @param ze the zip file entry
 389      * @return an input stream for reading the contents of the specified
 390      *         zip file entry
 391      * @throws ZipException if a zip file format error has occurred
 392      * @throws IOException if an I/O error has occurred
 393      * @throws SecurityException if any of the jar file entries
 394      *         are incorrectly signed.
 395      * @throws IllegalStateException
 396      *         may be thrown if the jar file has been closed
 397      */
 398     public synchronized InputStream getInputStream(ZipEntry ze)
 399         throws IOException
 400     {
 401         maybeInstantiateVerifier();


 463                 }
 464             }
 465         }
 466         return manEntry;
 467     }
 468 
 469     // Returns true iff this jar file has a manifest with a class path
 470     // attribute. Returns false if there is no manifest or the manifest
 471     // does not contain a "Class-Path" attribute. Currently exported to
 472     // core libraries via sun.misc.SharedSecrets.
 473     boolean hasClassPathAttribute() throws IOException {
 474         if (computedHasClassPathAttribute) {
 475             return hasClassPathAttribute;
 476         }
 477 
 478         hasClassPathAttribute = false;
 479         if (!isKnownToNotHaveClassPathAttribute()) {
 480             JarEntry manEntry = getManEntry();
 481             if (manEntry != null) {
 482                 byte[] b = new byte[(int)manEntry.getSize()];
 483                 try (DataInputStream dis = new DataInputStream(
 484                          super.getInputStream(manEntry))) {
 485                     dis.readFully(b, 0, b.length);
 486                 }
 487 
 488                 int last = b.length - src.length;
 489                 int i = 0;
 490                 next:
 491                 while (i<=last) {
 492                     for (int j=9; j>=0; j--) {
 493                         char c = (char) b[i+j];
 494                         c = (((c-'A')|('Z'-c)) >= 0) ? (char)(c + 32) : c;
 495                         if (c != src[j]) {
 496                             i += Math.max(j + 1 - lastOcc[c&0x7F], optoSft[j]);
 497                             continue next;
 498                         }
 499                     }
 500                     hasClassPathAttribute = true;
 501                     break;
 502                 }
 503             }
 504         }
 505         computedHasClassPathAttribute = true;
 506         return hasClassPathAttribute;