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: changed impl for reading file as bytes array, modified test. AndroidFileSystem now does not resolveā€¦ Created May 29, 2017, 11:26 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..d529209a08a5b15c39ad59c2e1ed756b3beba1ff
--- /dev/null
+++ b/libadblockplus-android/src/org/adblockplus/libadblockplus/FileSystemUtils.java
@@ -0,0 +1,135 @@
+/*
+ * 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.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.UUID;
+
+public class FileSystemUtils
+{
+
+ private static byte[] readStream(InputStream inputStream, int length) throws IOException
+ {
+ byte[] data = new byte[length];
+ int bytesRead, totalBytesRead = 0;
+ while ((bytesRead = inputStream.read(data, totalBytesRead, length - totalBytesRead)) > 0)
+ {
+ totalBytesRead += bytesRead;
+ }
+ return data;
+ }
+
+ /**
+ * Convert java signed int to byte
+ * @param b int representation of byte
+ * @return byte representation of byte
+ */
+ public static byte byteFromInt(int b)
+ {
+ return (byte)(b & 0xFF);
+ }
+
+ /**
+ * Read all the file data to string
+ *
+ * @param file path to read data
+ * @return file data
+ * @throws java.io.IOException
+ */
+ public static byte[] readFile(File file) throws IOException
+ {
+ FileInputStream fileInputStream = new FileInputStream(file);
+ try
+ {
+ return readStream(fileInputStream, (int) file.length());
+ }
+ finally
+ {
+ try
+ {
+ fileInputStream.close();
+ }
+ catch (IOException e)
+ {
+ // ignored
+ }
+ }
+ }
+
+ /**
+ * Write data to file (with rewriting)
+ *
+ * @param file file
+ * @param data file data
+ */
+ public static void writeFile(File file, byte[] data) throws IOException
+ {
+ FileOutputStream fos = null;
+ try
+ {
+ fos = new FileOutputStream(file);
+ if (data != null)
+ {
+ fos.write(data);
+ }
+ }
+ 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