tool: used new copy() method in Java 7
authorSeokYeon Hwang <syeon.hwang@samsung.com>
Thu, 11 Jun 2015 05:43:15 +0000 (14:43 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Fri, 12 Jun 2015 05:12:27 +0000 (14:12 +0900)
Change-Id: I48a37af638025cdc01e62f089c349da360c4145c
Signed-off-by: SeokYeon Hwang <syeon.hwang@samsung.com>
src/org/tizen/emulator/manager/tool/CheckEmulatorDir.java

index c29dcb4..66abc78 100644 (file)
@@ -31,11 +31,11 @@ package org.tizen.emulator.manager.tool;
 
 import java.io.File;
 import java.io.FileFilter;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.nio.channels.FileChannel;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
 import java.util.ArrayList;
 import java.util.logging.Level;
 
@@ -106,76 +106,18 @@ public class CheckEmulatorDir {
                        dir.mkdir();
                }
 
-               File dest = null;
-               File src = null;
                for (String s : StringResources.SD_CARD) {
-                       dest = new File(FilePathResources.getSDCardPath()
-                                               + File.separator + s);
-                       if (!dest.exists()) {
-                               src = new File(FilePathResources.getBaseSdcardPath()
-                                               + File.separator +s);
-                               if (!src.exists()) {
-                                       EMLogger.getLogger().log(Level.WARNING,
-                                       "Failed to create SD card image.! + StringResources.NEW_LINE"
-                                       + "File does not exist :" + src.getPath());
-                                       continue;
-                               }
-                               
-                               try {
-                                       dest.createNewFile();
-                               } catch (IOException e1) {
-                                       EMLogger.getLogger().log(Level.WARNING,
-                                       "Failed to create SD card image!" + StringResources.NEW_LINE + e1.getMessage());
-                                       continue;
-                               }
+                       Path source = Paths.get(FilePathResources.getBaseSdcardPath(), s);
+                       Path target = Paths.get(FilePathResources.getSDCardPath(), s);
 
-                               FileInputStream inputStream = null;
-                               FileOutputStream outputStream = null;
-                               FileChannel fcin = null;
-                               FileChannel fcout = null;
+                       if (!target.toFile().exists()) {
                                try {
-                                       inputStream = new FileInputStream(src);
-                                       outputStream = new FileOutputStream(dest);
-                                       fcin =  inputStream.getChannel();
-                                       fcout = outputStream.getChannel();
-                                       long size = fcin.size();
-                                       long transferredSize = fcin.transferTo(0, size, fcout);
-                                       if(size != transferredSize) {
-                                               EMLogger.getLogger().log(Level.WARNING, "Failed to create SD card image!"
-                                                                                                                       + StringResources.NEW_LINE + "Copy failed.");
-                                               dest.delete();
-                                               continue;
-                                       }
-                               } catch (FileNotFoundException e) {
+                                       Files.copy(source, target, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
+                               } catch (IOException e2) {
                                        EMLogger.getLogger().log(Level.WARNING, "Failed to create SD card image!"
-                                                                                                                       + StringResources.NEW_LINE + e.getMessage());
-                                       dest.delete();
-                                       continue;
-                               } catch (IOException e) {
-                                       EMLogger.getLogger().log(Level.WARNING, "Failed to create SD card image!"
-                                                                                                                       + StringResources.NEW_LINE + e.getMessage());
-                                       dest.delete();
-                                       continue;
-                               }finally {
-                                       try {
-                                               if (fcout != null) {
-                                                       fcout.close();
-                                               }
-                                               if (fcin != null) {
-                                                       fcin.close();
-                                               }
-                                               if (outputStream != null) {
-                                                       outputStream.close();
-                                               }
-                                               if (inputStream != null) {
-                                                       inputStream.close();
-                                               }
-                                       } catch (IOException e) {
-                                               EMLogger.getLogger().log(Level.WARNING, e.getMessage());
-                                       }
+                                                       + StringResources.NEW_LINE + e2.getMessage());
                                }
                        }
                }
        }
-
 }