1 /*
2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * This utils class is been used by test T8003512 which is compiled with Java 6
26  * only features. So if this class is modified, it should be so using Java 6
27  * features only.
28  */
29 
30 import java.io.BufferedInputStream;
31 import java.io.BufferedOutputStream;
32 import java.io.Closeable;
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.OutputStream;
39 import java.io.PrintStream;
40 
41 public class Utils {
42 
43     static final sun.tools.jar.Main jarTool =
44             new sun.tools.jar.Main(System.out, System.err, "jar-tool");
45 
46     static final com.sun.tools.javac.Main javac =
47             new com.sun.tools.javac.Main();
48 
Utils()49     private Utils(){}
50 
compile(String... args)51     public static boolean compile(String... args) {
52         return javac.compile(args) == 0;
53     }
54 
createClassFile(File javaFile, File superClass, boolean delete)55     public static void createClassFile(File javaFile, File superClass,
56             boolean delete) throws IOException {
57         createJavaFile(javaFile, superClass);
58         if (!compile(javaFile.getName())) {
59             throw new RuntimeException("compile failed unexpectedly");
60         }
61         if (delete) javaFile.delete();
62     }
63 
createJavaFile(File outFile)64     public static void createJavaFile(File outFile) throws IOException {
65         createJavaFile(outFile, null);
66     }
67 
createJavaFile(File outFile, File superClass)68     public static void createJavaFile(File outFile, File superClass) throws IOException {
69         PrintStream ps = null;
70         String srcStr = "public class " + getSimpleName(outFile) + " ";
71         if (superClass != null) {
72             srcStr = srcStr.concat("extends " + getSimpleName(superClass) + " ");
73         }
74         srcStr = srcStr.concat("{}");
75         try {
76             FileOutputStream fos = new FileOutputStream(outFile);
77             ps = new PrintStream(fos);
78             ps.println(srcStr);
79         } finally {
80             close(ps);
81         }
82     }
83 
getClassFileName(File javaFile)84     static String getClassFileName(File javaFile) {
85         return javaFile.getName().endsWith(".java")
86                 ? javaFile.getName().replace(".java", ".class")
87                 : null;
88     }
89 
getSimpleName(File inFile)90     static String getSimpleName(File inFile) {
91         String fname = inFile.getName();
92         return fname.substring(0, fname.indexOf("."));
93     }
94 
copyStream(InputStream in, OutputStream out)95     public static void copyStream(InputStream in, OutputStream out) throws IOException {
96         byte[] buf = new byte[8192];
97         int n = in.read(buf);
98         while (n > 0) {
99             out.write(buf, 0, n);
100             n = in.read(buf);
101         }
102     }
103 
close(Closeable c)104     public static void close(Closeable c) {
105         if (c != null) {
106             try {
107                 c.close();
108             } catch (IOException ignore) {}
109         }
110     }
111 
deleteFile(File f)112     public static void deleteFile(File f) {
113         if (!f.delete()) {
114             throw new RuntimeException("could not delete file: " + f.getAbsolutePath());
115         }
116     }
117 
cat(File output, File... files)118     public static void cat(File output, File... files) throws IOException {
119         BufferedInputStream bis = null;
120         BufferedOutputStream bos = null;
121         FileOutputStream fos = null;
122         try {
123             fos = new FileOutputStream(output);
124             bos = new BufferedOutputStream(fos);
125             for (File x : files) {
126                 FileInputStream fis = new FileInputStream(x);
127                 bis = new BufferedInputStream(fis);
128                 copyStream(bis, bos);
129                 Utils.close(bis);
130             }
131         } finally {
132             Utils.close(bis);
133             Utils.close(bos);
134             Utils.close(fos);
135         }
136     }
137 }
138