1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/file/zip/Utils.java Mon Mar 07 17:39:42 2011 -0800 1.3 @@ -0,0 +1,131 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +import java.io.BufferedInputStream; 1.28 +import java.io.BufferedOutputStream; 1.29 +import java.io.Closeable; 1.30 +import java.io.File; 1.31 +import java.io.FileInputStream; 1.32 +import java.io.FileOutputStream; 1.33 +import java.io.IOException; 1.34 +import java.io.InputStream; 1.35 +import java.io.OutputStream; 1.36 +import java.io.PrintStream; 1.37 + 1.38 +public class Utils { 1.39 + 1.40 + static final sun.tools.jar.Main jarTool = 1.41 + new sun.tools.jar.Main(System.out, System.err, "jar-tool"); 1.42 + 1.43 + static final com.sun.tools.javac.Main javac = 1.44 + new com.sun.tools.javac.Main(); 1.45 + 1.46 + private Utils(){} 1.47 + 1.48 + public static boolean compile(String... args) { 1.49 + return javac.compile(args) == 0; 1.50 + } 1.51 + 1.52 + public static void createClassFile(File javaFile, File superClass, 1.53 + boolean delete) throws IOException { 1.54 + createJavaFile(javaFile, superClass); 1.55 + if (!compile(javaFile.getName())) { 1.56 + throw new RuntimeException("compile failed unexpectedly"); 1.57 + } 1.58 + if (delete) javaFile.delete(); 1.59 + } 1.60 + 1.61 + public static void createJavaFile(File outFile) throws IOException { 1.62 + createJavaFile(outFile, null); 1.63 + } 1.64 + 1.65 + public static void createJavaFile(File outFile, File superClass) throws IOException { 1.66 + PrintStream ps = null; 1.67 + String srcStr = "public class " + getSimpleName(outFile) + " "; 1.68 + if (superClass != null) { 1.69 + srcStr = srcStr.concat("extends " + getSimpleName(superClass) + " "); 1.70 + } 1.71 + srcStr = srcStr.concat("{}"); 1.72 + try { 1.73 + FileOutputStream fos = new FileOutputStream(outFile); 1.74 + ps = new PrintStream(fos); 1.75 + ps.println(srcStr); 1.76 + } finally { 1.77 + close(ps); 1.78 + } 1.79 + } 1.80 + 1.81 + static String getClassFileName(File javaFile) { 1.82 + return javaFile.getName().endsWith(".java") 1.83 + ? javaFile.getName().replace(".java", ".class") 1.84 + : null; 1.85 + } 1.86 + 1.87 + static String getSimpleName(File inFile) { 1.88 + String fname = inFile.getName(); 1.89 + return fname.substring(0, fname.indexOf(".")); 1.90 + } 1.91 + 1.92 + public static void copyStream(InputStream in, OutputStream out) throws IOException { 1.93 + byte[] buf = new byte[8192]; 1.94 + int n = in.read(buf); 1.95 + while (n > 0) { 1.96 + out.write(buf, 0, n); 1.97 + n = in.read(buf); 1.98 + } 1.99 + } 1.100 + 1.101 + public static void close(Closeable c) { 1.102 + if (c != null) { 1.103 + try { 1.104 + c.close(); 1.105 + } catch (IOException ignore) {} 1.106 + } 1.107 + } 1.108 + 1.109 + public static void deleteFile(File f) { 1.110 + if (!f.delete()) { 1.111 + throw new RuntimeException("could not delete file: " + f.getAbsolutePath()); 1.112 + } 1.113 + } 1.114 + 1.115 + public static void cat(File output, File... files) throws IOException { 1.116 + BufferedInputStream bis = null; 1.117 + BufferedOutputStream bos = null; 1.118 + FileOutputStream fos = null; 1.119 + try { 1.120 + fos = new FileOutputStream(output); 1.121 + bos = new BufferedOutputStream(fos); 1.122 + for (File x : files) { 1.123 + FileInputStream fis = new FileInputStream(x); 1.124 + bis = new BufferedInputStream(fis); 1.125 + copyStream(bis, bos); 1.126 + Utils.close(bis); 1.127 + } 1.128 + } finally { 1.129 + Utils.close(bis); 1.130 + Utils.close(bos); 1.131 + Utils.close(fos); 1.132 + } 1.133 + } 1.134 +}