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

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

Issue 29347315: Issue 4231 - Fix unstable FilterEngineTest.testSetRemoveFilterChangeCallback (Closed)
Patch Set: Introduced file system abstraction to java and using LazyFileSystem for filter tests Created July 10, 2016, 10:47 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/FileSystemHelper.java
diff --git a/libadblockplus-android/src/org/adblockplus/libadblockplus/FileSystemHelper.java b/libadblockplus-android/src/org/adblockplus/libadblockplus/FileSystemHelper.java
new file mode 100644
index 0000000000000000000000000000000000000000..3998c8f3f794f88c36233a0e8fe39a063562cf17
--- /dev/null
+++ b/libadblockplus-android/src/org/adblockplus/libadblockplus/FileSystemHelper.java
@@ -0,0 +1,103 @@
+/*
+ * 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 FileSystemHelper
anton 2016/07/12 10:49:41 could be called 'FileSystemUtils' in order to be c
+{
+ /**
+ * 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();
+ }
+}

Powered by Google App Engine
This is Rietveld