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