*/
package org.tizen.common.util;
+import static org.tizen.common.util.IOUtil.redirect;
import static org.tizen.common.util.IOUtil.tryClose;
+import static org.tizen.common.util.StringUtil.isEmpty;
+import static org.tizen.common.util.StringUtil.trim;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.tizen.common.ui.view.console.ConsoleManager;
public abstract class HostUtil{
+ protected static Logger logger = LoggerFactory.getLogger( HostUtil.class );
+
public static final String LINE_SEPARATOR = System.getProperty("line.separator"); //$NON-NLS-1$
public static final String FILE_SEPARATOR = System.getProperty("file.separator"); //$NON-NLS-1$
private static final String SHELL_COMMAND_WINDOW= "cmd"; //$NON-NLS-1$
public static String getContents(String filePath) {
- BufferedReader input = null;
+ FileReader input = null;
StringBuilder contents = new StringBuilder();
- String line = null;
try {
- input = new BufferedReader(new FileReader(filePath));
- while((line=input.readLine())!=null){
- contents.append(line);
- contents.append(LINE_SEPARATOR);
- }
+ input = new FileReader(filePath);
+ redirect( input, contents );
+ return trim( contents.toString() );
}catch(Exception e) {
-// e.printStackTrace();
return null;
}finally{
- tryClose( input );
+ tryClose( input, contents );
}
-
- return contents.toString().trim();
}
public static boolean exists(String path) {
- if( path == null || path.equals("") ) //$NON-NLS-1$
+ if( isEmpty( path ) )
+ {
return false;
+ }
File file = new File( path );
e.printStackTrace();
return false;
} finally{
- System.out.println("HostUtil execute - exit value : " + i);
+ logger.info( "HostUtil execute - exit value : {}", i );
if(proc!=null)
+ {
proc.destroy();
+ }
}
return i==0;
String[] fullCommand = getCommand(command);
Runtime run = Runtime.getRuntime();
- Process p = null;
-
- int i = 0;
- try{
- p = run.exec(fullCommand);
- i = p.waitFor();
- StreamGobbler gb1 = new StreamGobbler(p.getInputStream());
- StreamGobbler gb2 = new StreamGobbler(p.getErrorStream());
- gb1.start();
- gb2.start();
- }catch(Exception e){
- e.printStackTrace();
- return false;
- }
+ Process p = null;
+
+ int i = 0;
+ try{
+ p = run.exec(fullCommand);
+ i = p.waitFor();
+ StreamGobbler gb1 = new StreamGobbler(p.getInputStream());
+ StreamGobbler gb2 = new StreamGobbler(p.getErrorStream());
+ gb1.start();
+ gb2.start();
+ }catch(Exception e){
+ e.printStackTrace();
+ return false;
+ }
return i == 0;
}
String[] fullCommand = getCommand(command);
Runtime run = Runtime.getRuntime();
- Process p = null;
+ Process p = null;
- StreamGobbler gb1 = null;
- StreamGobbler gb2 = null;
- try{
- p = run.exec(fullCommand, envp, dir);
- gb1 = new StreamGobbler(p.getInputStream());
- gb2 = new StreamGobbler(p.getErrorStream());
- gb1.start();
- gb2.start();
- }catch(Exception e){
- e.printStackTrace();
- return false;
- }finally{
- System.out.println(gb1.getResult());
- System.out.println(gb2.getResult());
- }
+ StreamGobbler gb1 = null;
+ StreamGobbler gb2 = null;
+ try{
+ p = run.exec(fullCommand, envp, dir);
+ gb1 = new StreamGobbler(p.getInputStream());
+ gb2 = new StreamGobbler(p.getErrorStream());
+ gb1.start();
+ gb2.start();
+ }catch(Exception e){
+ e.printStackTrace();
+ return false;
+ }finally{
+ logger.debug( "Output Stream :{}", gb1.getResult() );
+ logger.debug( "Error Stream :{}", gb2.getResult() );
+ }
return true;
}
public static String returnExecute(String command, String workingDir) {
-
+
if(command == null) {
return null;
}
BufferedReader input = null;
StringBuilder contents = new StringBuilder();
String line = null;
-
+
Process proc = null;
-
+
String[] fullCommand = getCommand(command);
-
+
try {
- ProcessBuilder pb = new ProcessBuilder();
- pb.command(fullCommand);
- if(workingDir != null) {
- pb.directory(new File(workingDir));
- }
- proc = pb.start();
- input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
- while((line=input.readLine())!=null){
- contents.append(line);
- contents.append(LINE_SEPARATOR);
- }
+ ProcessBuilder pb = new ProcessBuilder();
+ pb.command(fullCommand);
+ if(workingDir != null) {
+ pb.directory(new File(workingDir));
+ }
+ proc = pb.start();
+ input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
+ while((line=input.readLine())!=null){
+ contents.append(line);
+ contents.append(LINE_SEPARATOR);
+ }
} catch (Exception e) {
e.printStackTrace();
return null;
proc.waitFor();
// abnormal termination
if (proc.exitValue() != 0)
+ {
throw new Exception("Failed to execute command: " + command);
+ }
} finally {
tryClose( input );
if(proc!=null)
return returnExecute(command, null);
}
-
+
public static String[] getCommand(String command){
-
+
if(OSChecker.isWindows()){
return new String[]{SHELL_COMMAND_WINDOW,"/c",command}; //$NON-NLS-1$
}else
return new String[]{SHELL_COMMAND_LINUX,"-c",command}; //$NON-NLS-1$
}
}
-
+
/**
- * @deprecated Use {@link FileUtil#createFile(File)},
- * {@link FileUtil#createFile(String)},
- * {@link FileUtil#createFile(String, String)}
- * or
- * {@link FileUtil#createTextFile(File, String, String)},
- * {@link FileUtil#createTextFile(String, String, String)},
- * {@link FileUtil#createTextFile(String, String, String, String)}
- * and
- * {@link FileUtil#createDirectory(File)},
- * {@link FileUtil#createDirectory(String)},
- * {@link FileUtil#createDirectory(String, String)}
- * instead.
- */
+ * @deprecated Use {@link FileUtil#createFile(File)},
+ * {@link FileUtil#createFile(String)},
+ * {@link FileUtil#createFile(String, String)}
+ * or
+ * {@link FileUtil#createTextFile(File, String, String)},
+ * {@link FileUtil#createTextFile(String, String, String)},
+ * {@link FileUtil#createTextFile(String, String, String, String)}
+ * and
+ * {@link FileUtil#createDirectory(File)},
+ * {@link FileUtil#createDirectory(String)},
+ * {@link FileUtil#createDirectory(String, String)}
+ * instead.
+ */
@Deprecated
public static boolean createFile(String path, String contents) {
BufferedWriter output = null;
File folder = file.getParentFile();
if(folder!=null)
+ {
folder.mkdirs();
+ }
file.createNewFile();
FileOutputStream fos=null;
FileChannel fcin=null;
FileChannel fcout=null;
-
+
canExecute = srcFile.canExecute();
-
+
desFile= new File(des);
File paths = new File(desFile.getAbsolutePath().substring(0, desFile.getAbsolutePath().lastIndexOf("/"))); //$NON-NLS-1$
if (!paths.exists()) {
paths.mkdirs();
}
-
+
try {
fis= new FileInputStream(srcFile);
fos= new FileOutputStream(desFile);
fcin= fis.getChannel();
fcout= fos.getChannel();
-
+
long size= fcin.size();
fcin.transferTo(0, size, fcout);
desFile.setExecutable(canExecute);