Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: libadblockplus-android/src/org/adblockplus/libadblockplus/FileSystemUtils.java

Issue 29424615: Issue 4231 - Fix unstable FilterEngineTest.testSetRemoveFilterChangeCallback (Closed)
Patch Set: fixed typo Created April 28, 2017, 8:46 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..a16c4198c030206ef151aff9ad1654fdffa9521a
--- /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-2017 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 static 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 static 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 static 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();
+ }
+}

Powered by Google App Engine
This is Rietveld