< prev index next >

src/java.base/share/classes/com/sun/java/util/jar/pack/Driver.java

Print this page
rev 52881 : 8214971: Replace use of string.equals("") with isEmpty()
Reviewed-by: jlaskey, prappo, lancea, dfuchs, redestad


 191         // Deal with remaining non-engine properties:
 192         for (String opt : avProps.keySet()) {
 193             String val = avProps.get(opt);
 194             switch (opt) {
 195                 case "--repack":
 196                     doRepack = true;
 197                     break;
 198                 case "--no-gzip":
 199                     doZip = (val == null);
 200                     break;
 201                 case "--log-file=":
 202                     logFile = val;
 203                     break;
 204                 default:
 205                     throw new InternalError(MessageFormat.format(
 206                             RESOURCE.getString(DriverResource.BAD_OPTION),
 207                             opt, avProps.get(opt)));
 208             }
 209         }
 210 
 211         if (logFile != null && !logFile.equals("")) {
 212             if (logFile.equals("-")) {
 213                 System.setErr(System.out);
 214             } else {
 215                 OutputStream log = new FileOutputStream(logFile);
 216                 //log = new BufferedOutputStream(out);
 217                 System.setErr(new PrintStream(log));
 218             }
 219         }
 220 
 221         boolean verbose = (engProps.get(verboseProp) != null);
 222 
 223         String packfile = "";
 224         if (!av.isEmpty())
 225             packfile = av.remove(0);
 226 
 227         String jarfile = "";
 228         if (!av.isEmpty())
 229             jarfile = av.remove(0);
 230 
 231         String newfile = "";  // output JAR file if --repack
 232         String bakfile = "";  // temporary backup of input JAR
 233         String tmpfile = "";  // temporary file to be deleted
 234         if (doRepack) {
 235             // The first argument is the target JAR file.
 236             // (Note:  *.pac is nonstandard, but may be necessary
 237             // if a host OS truncates file extensions.)
 238             if (packfile.toLowerCase().endsWith(".pack") ||
 239                 packfile.toLowerCase().endsWith(".pac") ||
 240                 packfile.toLowerCase().endsWith(".gz")) {
 241                 System.err.println(MessageFormat.format(
 242                         RESOURCE.getString(DriverResource.BAD_REPACK_OUTPUT),
 243                         packfile));
 244                 printUsage(doPack, false, System.err);
 245                 System.exit(2);
 246             }
 247             newfile = packfile;
 248             // The optional second argument is the source JAR file.
 249             if (jarfile.equals("")) {
 250                 // If only one file is given, it is the only JAR.
 251                 // It serves as both input and output.
 252                 jarfile = newfile;
 253             }
 254             tmpfile = createTempFile(newfile, ".pack").getPath();
 255             packfile = tmpfile;
 256             doZip = false;  // no need to zip the temporary file
 257         }
 258 
 259         if (!av.isEmpty()
 260             // Accept jarfiles ending with .jar or .zip.
 261             // Accept jarfile of "-" (stdout), but only if unpacking.
 262             || !(jarfile.toLowerCase().endsWith(".jar")
 263                  || jarfile.toLowerCase().endsWith(".zip")
 264                  || (jarfile.equals("-") && !doPack))) {
 265             printUsage(doPack, false, System.err);
 266             System.exit(2);
 267             return;
 268         }
 269 


 335                 if (!okBackup) {
 336                         throw new Error(MessageFormat.format(RESOURCE.getString(DriverResource.SKIP_FOR_MOVE_FAILED),bakfile));
 337                 } else {
 338                     // Open jarfile recovery bracket.
 339                     bakfile = bakf.getPath();
 340                 }
 341             }
 342 
 343             if (doUnpack) {
 344                 // Mode = Unpack.
 345                 InputStream in;
 346                 if (packfile.equals("-"))
 347                     in = System.in;
 348                 else
 349                     in = new FileInputStream(new File(packfile));
 350                 BufferedInputStream inBuf = new BufferedInputStream(in);
 351                 in = inBuf;
 352                 if (Utils.isGZIPMagic(Utils.readMagic(inBuf))) {
 353                     in = new GZIPInputStream(in);
 354                 }
 355                 String outfile = newfile.equals("")? jarfile: newfile;
 356                 OutputStream fileOut;
 357                 if (outfile.equals("-"))
 358                     fileOut = System.out;
 359                 else
 360                     fileOut = new FileOutputStream(outfile);
 361                 fileOut = new BufferedOutputStream(fileOut);
 362                 try (JarOutputStream out = new JarOutputStream(fileOut)) {
 363                     junpack.unpack(in, out);
 364                     // p200 closes in but not out
 365                 }
 366                 // At this point, we have a good jarfile (or newfile, if -r)
 367             }
 368 
 369             if (!bakfile.equals("")) {
 370                         // On success, abort jarfile recovery bracket.
 371                         new File(bakfile).delete();
 372                         bakfile = "";
 373             }
 374 
 375         } finally {
 376             // Close jarfile recovery bracket.
 377             if (!bakfile.equals("")) {
 378                 File jarFile = new File(jarfile);
 379                 jarFile.delete(); // Win32 requires this, see above
 380                 new File(bakfile).renameTo(jarFile);
 381             }
 382             // In all cases, delete temporary *.pack.
 383             if (!tmpfile.equals(""))
 384                 new File(tmpfile).delete();
 385         }
 386     }
 387 
 388     private static
 389     File createTempFile(String basefile, String suffix) throws IOException {
 390         File base = new File(basefile);
 391         String prefix = base.getName();
 392         if (prefix.length() < 3)  prefix += "tmp";
 393 
 394         File where = (base.getParentFile() == null && suffix.equals(".bak"))
 395                 ? new File(".").getAbsoluteFile()
 396                 : base.getParentFile();
 397 
 398         Path tmpfile = (where == null)
 399                 ? Files.createTempFile(prefix, suffix)
 400                 : Files.createTempFile(where.toPath(), prefix, suffix);
 401 
 402         return tmpfile.toFile();
 403     }




 191         // Deal with remaining non-engine properties:
 192         for (String opt : avProps.keySet()) {
 193             String val = avProps.get(opt);
 194             switch (opt) {
 195                 case "--repack":
 196                     doRepack = true;
 197                     break;
 198                 case "--no-gzip":
 199                     doZip = (val == null);
 200                     break;
 201                 case "--log-file=":
 202                     logFile = val;
 203                     break;
 204                 default:
 205                     throw new InternalError(MessageFormat.format(
 206                             RESOURCE.getString(DriverResource.BAD_OPTION),
 207                             opt, avProps.get(opt)));
 208             }
 209         }
 210 
 211         if (logFile != null && !logFile.isEmpty()) {
 212             if (logFile.equals("-")) {
 213                 System.setErr(System.out);
 214             } else {
 215                 OutputStream log = new FileOutputStream(logFile);
 216                 //log = new BufferedOutputStream(out);
 217                 System.setErr(new PrintStream(log));
 218             }
 219         }
 220 
 221         boolean verbose = (engProps.get(verboseProp) != null);
 222 
 223         String packfile = "";
 224         if (!av.isEmpty())
 225             packfile = av.remove(0);
 226 
 227         String jarfile = "";
 228         if (!av.isEmpty())
 229             jarfile = av.remove(0);
 230 
 231         String newfile = "";  // output JAR file if --repack
 232         String bakfile = "";  // temporary backup of input JAR
 233         String tmpfile = "";  // temporary file to be deleted
 234         if (doRepack) {
 235             // The first argument is the target JAR file.
 236             // (Note:  *.pac is nonstandard, but may be necessary
 237             // if a host OS truncates file extensions.)
 238             if (packfile.toLowerCase().endsWith(".pack") ||
 239                 packfile.toLowerCase().endsWith(".pac") ||
 240                 packfile.toLowerCase().endsWith(".gz")) {
 241                 System.err.println(MessageFormat.format(
 242                         RESOURCE.getString(DriverResource.BAD_REPACK_OUTPUT),
 243                         packfile));
 244                 printUsage(doPack, false, System.err);
 245                 System.exit(2);
 246             }
 247             newfile = packfile;
 248             // The optional second argument is the source JAR file.
 249             if (jarfile.isEmpty()) {
 250                 // If only one file is given, it is the only JAR.
 251                 // It serves as both input and output.
 252                 jarfile = newfile;
 253             }
 254             tmpfile = createTempFile(newfile, ".pack").getPath();
 255             packfile = tmpfile;
 256             doZip = false;  // no need to zip the temporary file
 257         }
 258 
 259         if (!av.isEmpty()
 260             // Accept jarfiles ending with .jar or .zip.
 261             // Accept jarfile of "-" (stdout), but only if unpacking.
 262             || !(jarfile.toLowerCase().endsWith(".jar")
 263                  || jarfile.toLowerCase().endsWith(".zip")
 264                  || (jarfile.equals("-") && !doPack))) {
 265             printUsage(doPack, false, System.err);
 266             System.exit(2);
 267             return;
 268         }
 269 


 335                 if (!okBackup) {
 336                         throw new Error(MessageFormat.format(RESOURCE.getString(DriverResource.SKIP_FOR_MOVE_FAILED),bakfile));
 337                 } else {
 338                     // Open jarfile recovery bracket.
 339                     bakfile = bakf.getPath();
 340                 }
 341             }
 342 
 343             if (doUnpack) {
 344                 // Mode = Unpack.
 345                 InputStream in;
 346                 if (packfile.equals("-"))
 347                     in = System.in;
 348                 else
 349                     in = new FileInputStream(new File(packfile));
 350                 BufferedInputStream inBuf = new BufferedInputStream(in);
 351                 in = inBuf;
 352                 if (Utils.isGZIPMagic(Utils.readMagic(inBuf))) {
 353                     in = new GZIPInputStream(in);
 354                 }
 355                 String outfile = newfile.isEmpty()? jarfile: newfile;
 356                 OutputStream fileOut;
 357                 if (outfile.equals("-"))
 358                     fileOut = System.out;
 359                 else
 360                     fileOut = new FileOutputStream(outfile);
 361                 fileOut = new BufferedOutputStream(fileOut);
 362                 try (JarOutputStream out = new JarOutputStream(fileOut)) {
 363                     junpack.unpack(in, out);
 364                     // p200 closes in but not out
 365                 }
 366                 // At this point, we have a good jarfile (or newfile, if -r)
 367             }
 368 
 369             if (!bakfile.isEmpty()) {
 370                         // On success, abort jarfile recovery bracket.
 371                         new File(bakfile).delete();
 372                         bakfile = "";
 373             }
 374 
 375         } finally {
 376             // Close jarfile recovery bracket.
 377             if (!bakfile.isEmpty()) {
 378                 File jarFile = new File(jarfile);
 379                 jarFile.delete(); // Win32 requires this, see above
 380                 new File(bakfile).renameTo(jarFile);
 381             }
 382             // In all cases, delete temporary *.pack.
 383             if (!tmpfile.isEmpty())
 384                 new File(tmpfile).delete();
 385         }
 386     }
 387 
 388     private static
 389     File createTempFile(String basefile, String suffix) throws IOException {
 390         File base = new File(basefile);
 391         String prefix = base.getName();
 392         if (prefix.length() < 3)  prefix += "tmp";
 393 
 394         File where = (base.getParentFile() == null && suffix.equals(".bak"))
 395                 ? new File(".").getAbsoluteFile()
 396                 : base.getParentFile();
 397 
 398         Path tmpfile = (where == null)
 399                 ? Files.createTempFile(prefix, suffix)
 400                 : Files.createTempFile(where.toPath(), prefix, suffix);
 401 
 402         return tmpfile.toFile();
 403     }


< prev index next >