From 44d6b8da1b20b27454481e3a8c77f8fab1f72bc9 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Fri, 29 Jan 2016 12:23:03 -0800 Subject: [PATCH] sort FileUtils methods --- .../microsoft/codepush/react/FileUtils.java | 164 +++++++++--------- 1 file changed, 82 insertions(+), 82 deletions(-) diff --git a/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java b/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java index 5b68a1c..16c5731 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java @@ -17,6 +17,81 @@ public class FileUtils { public static final int WRITE_BUFFER_SIZE = 1024 * 8; + public static void copyDirectoryContents(String sourceDirectoryPath, String destinationDirectoryPath) throws IOException { + File sourceDir = new File(sourceDirectoryPath); + File destDir = new File(destinationDirectoryPath); + if (!destDir.exists()) { + destDir.mkdir(); + } + + for (File sourceFile : sourceDir.listFiles()) { + if (sourceFile.isDirectory()) { + copyDirectoryContents( + CodePushUtils.appendPathComponent(sourceDirectoryPath, sourceFile.getName()), + CodePushUtils.appendPathComponent(destinationDirectoryPath, sourceFile.getName())); + } else { + File destFile = new File(destDir, sourceFile.getName()); + FileInputStream fromFileStream = null; + BufferedInputStream fromBufferedStream = null; + FileOutputStream destStream = null; + byte[] buffer = new byte[WRITE_BUFFER_SIZE]; + try { + fromFileStream = new FileInputStream(sourceFile); + fromBufferedStream = new BufferedInputStream(fromFileStream); + destStream = new FileOutputStream(destFile); + int bytesRead; + while ((bytesRead = fromBufferedStream.read(buffer)) > 0) { + destStream.write(buffer, 0, bytesRead); + } + } finally { + try { + if (fromFileStream != null) fromFileStream.close(); + if (fromBufferedStream != null) fromBufferedStream.close(); + if (destStream != null) destStream.close(); + } catch (IOException e) { + throw new CodePushUnknownException("Error closing IO resources.", e); + } + } + } + } + } + + public static boolean createFolderAtPath(String filePath) { + File file = new File(filePath); + return file.mkdir(); + } + + public static void deleteDirectory(File directory) { + if (directory.exists()) { + File[] files = directory.listFiles(); + if (files != null) { + for (int i=0; i 0) { - destStream.write(buffer, 0, bytesRead); - } - } finally { - try { - if (fromFileStream != null) fromFileStream.close(); - if (fromBufferedStream != null) fromBufferedStream.close(); - if (destStream != null) destStream.close(); - } catch (IOException e) { - throw new CodePushUnknownException("Error closing IO resources.", e); - } - } - } - } - } - - public static void deleteFileAtPathSilently(String path) { - deleteFileSilently(new File(path)); - } - - public static void deleteFileSilently(File file) { - if (!file.delete()) { - CodePushUtils.log("Error deleting file " + file.getName()); + public static void writeStringToFile(String content, String filePath) throws IOException { + PrintWriter out = null; + try { + out = new PrintWriter(filePath); + out.print(content); + } finally { + if (out != null) out.close(); } } }