Utils.java revision 922
0N/A/*
870N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/Aimport java.io.BufferedInputStream;
0N/Aimport java.io.BufferedOutputStream;
0N/Aimport java.io.Closeable;
0N/Aimport java.io.File;
0N/Aimport java.io.FileInputStream;
0N/Aimport java.io.FileOutputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.PrintStream;
0N/A
0N/Apublic class Utils {
0N/A
0N/A static final sun.tools.jar.Main jarTool =
0N/A new sun.tools.jar.Main(System.out, System.err, "jar-tool");
0N/A
0N/A static final com.sun.tools.javac.Main javac =
0N/A new com.sun.tools.javac.Main();
0N/A
0N/A private Utils(){}
0N/A
0N/A public static boolean compile(String... args) {
0N/A return javac.compile(args) == 0;
0N/A }
0N/A
0N/A public static void createClassFile(File javaFile, File superClass,
0N/A boolean delete) throws IOException {
0N/A createJavaFile(javaFile, superClass);
0N/A if (!compile(javaFile.getName())) {
0N/A throw new RuntimeException("compile failed unexpectedly");
0N/A }
0N/A if (delete) javaFile.delete();
0N/A }
0N/A
0N/A public static void createJavaFile(File outFile) throws IOException {
0N/A createJavaFile(outFile, null);
0N/A }
0N/A
0N/A public static void createJavaFile(File outFile, File superClass) throws IOException {
0N/A PrintStream ps = null;
0N/A String srcStr = "public class " + getSimpleName(outFile) + " ";
0N/A if (superClass != null) {
0N/A srcStr = srcStr.concat("extends " + getSimpleName(superClass) + " ");
0N/A }
0N/A srcStr = srcStr.concat("{}");
0N/A try {
0N/A FileOutputStream fos = new FileOutputStream(outFile);
0N/A ps = new PrintStream(fos);
0N/A ps.println(srcStr);
0N/A } finally {
0N/A close(ps);
0N/A }
0N/A }
0N/A
0N/A static String getClassFileName(File javaFile) {
0N/A return javaFile.getName().endsWith(".java")
0N/A ? javaFile.getName().replace(".java", ".class")
0N/A : null;
0N/A }
0N/A
0N/A static String getSimpleName(File inFile) {
0N/A String fname = inFile.getName();
0N/A return fname.substring(0, fname.indexOf("."));
0N/A }
0N/A
0N/A public static void copyStream(InputStream in, OutputStream out) throws IOException {
0N/A byte[] buf = new byte[8192];
0N/A int n = in.read(buf);
0N/A while (n > 0) {
0N/A out.write(buf, 0, n);
0N/A n = in.read(buf);
0N/A }
0N/A }
0N/A
0N/A public static void close(Closeable c) {
0N/A if (c != null) {
0N/A try {
0N/A c.close();
0N/A } catch (IOException ignore) {}
0N/A }
0N/A }
0N/A
0N/A public static void deleteFile(File f) {
0N/A if (!f.delete()) {
0N/A throw new RuntimeException("could not delete file: " + f.getAbsolutePath());
0N/A }
0N/A }
0N/A
0N/A public static void cat(File output, File... files) throws IOException {
0N/A BufferedInputStream bis = null;
0N/A BufferedOutputStream bos = null;
0N/A FileOutputStream fos = null;
0N/A try {
0N/A fos = new FileOutputStream(output);
0N/A bos = new BufferedOutputStream(fos);
0N/A for (File x : files) {
0N/A FileInputStream fis = new FileInputStream(x);
0N/A bis = new BufferedInputStream(fis);
0N/A copyStream(bis, bos);
0N/A Utils.close(bis);
0N/A }
0N/A } finally {
0N/A Utils.close(bis);
0N/A Utils.close(bos);
0N/A Utils.close(fos);
0N/A }
0N/A }
0N/A}
0N/A