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

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

Issue 29347315: Issue 4231 - Fix unstable FilterEngineTest.testSetRemoveFilterChangeCallback (Closed)
Patch Set: made helper methods static, fixed 'remove' for fs callback Created Dec. 13, 2016, 9:32 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..f6c6b3528bba50ba7e259f6dd86bf48510850c4a
--- /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 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();
diegocarloslima 2017/04/27 12:22:40 Are we using Java 7 already in libadblockplus-andr
anton 2017/04/28 08:25:45 it's not clear but seems that "not yet". For now i
+ }
+ 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