Index: libadblockplus-android/src/org/adblockplus/libadblockplus/FileSystemUtils.java |
diff --git a/libadblockplus-android/src/org/adblockplus/libadblockplus/FileSystemUtils.java b/libadblockplus-android/src/org/adblockplus/libadblockplus/FileSystemUtils.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..47302dc7027f5fdb43b0ecb13f0780e65566f1e2 |
--- /dev/null |
+++ b/libadblockplus-android/src/org/adblockplus/libadblockplus/FileSystemUtils.java |
@@ -0,0 +1,115 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2016 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+package org.adblockplus.libadblockplus; |
+ |
+import java.io.File; |
+import java.io.FileNotFoundException; |
+import java.io.FileOutputStream; |
+import java.io.IOException; |
+import java.util.NoSuchElementException; |
+import java.util.Scanner; |
+import java.util.UUID; |
+ |
+public class FileSystemUtils |
+{ |
+ /** |
+ * Read all the file data to string |
+ * |
+ * @param file path to read data |
+ * @return file data |
+ * @throws java.io.FileNotFoundException |
+ */ |
+ public String readFile(File file) throws FileNotFoundException |
+ { |
+ Scanner scanner = null; |
+ try |
+ { |
+ scanner = new Scanner(file); |
+ return scanner.useDelimiter("\\A").next(); |
+ } |
+ catch (NoSuchElementException e) |
+ { |
+ return ""; |
+ } |
+ finally |
+ { |
+ if (scanner != null) |
+ { |
+ scanner.close(); |
+ } |
+ } |
+ } |
+ |
+ /** |
+ * Write data to file (with rewriting) |
+ * |
+ * @param file file |
+ * @param data file data |
+ */ |
+ public void writeFile(File file, String data) throws IOException |
+ { |
+ FileOutputStream fos = null; |
+ try |
+ { |
+ fos = new FileOutputStream(file); |
+ if (data != null) |
+ { |
+ fos.write(data.getBytes()); |
+ } |
+ } |
+ finally |
+ { |
+ if (fos != null) |
+ { |
+ try |
+ { |
+ fos.close(); |
+ } |
+ catch (IOException e) |
+ { |
+ // ignored |
+ } |
+ } |
+ } |
+ } |
+ |
+ /** |
+ * Generate unique filename |
+ * |
+ * @param prefix prefix |
+ * @param suffix suffix |
+ * @return generated unique filename |
+ */ |
+ public String generateUniqueFileName(String prefix, String suffix) |
+ { |
+ StringBuilder sb = new StringBuilder(); |
+ if (prefix != null) |
+ { |
+ sb.append(prefix); |
+ } |
+ |
+ sb.append(UUID.randomUUID().toString()); |
+ |
+ if (suffix != null) |
+ { |
+ sb.append(suffix); |
+ } |
+ |
+ return sb.toString(); |
+ } |
+} |