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: now using smart_ptr, changed impl for reading file as string, minor changes Created May 22, 2017, 1:03 p.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..55a8d53514424b6834dbbf98675f94aadaa2226d
--- /dev/null
+++ b/libadblockplus-android/src/org/adblockplus/libadblockplus/FileSystemUtils.java
@@ -0,0 +1,132 @@
+/*
+ * 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.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.NoSuchElementException;
+import java.util.Scanner;
+import java.util.UUID;
+
+public class FileSystemUtils
+{
+ private static final int BUFFER_SIZE = 10 * 1024; // 10 Kb
+ private static final String CHARSET = "UTF-8";
+
+ private static String readStream(InputStream inputStream, int bufferSize) throws IOException
sergei 2017/05/22 13:51:04 What about returning some bytes array or some byte
anton 2017/05/23 05:46:47 Strings in java are not like null-terminated strin
sergei 2017/05/23 08:08:19 I'm not talking only about null characters, what i
sergei 2017/05/23 08:17:50 Here are http://www.cl.cam.ac.uk/~mgk25/ucs/exampl
anton 2017/05/23 11:11:18 we should decide first if we consider all the file
sergei 2017/05/23 13:00:37 Well, C++ API is using std::istream which is for b
anton 2017/05/29 11:30:02 moved to byte array instead of String. Check out n
+ {
+ StringBuilder sb = new StringBuilder();
+
+ byte buffer[] = new byte[bufferSize];
+ int bytesRead;
+ while ((bytesRead = inputStream.read(buffer)) > 0)
+ {
+ sb.append(new String(buffer, 0, bytesRead, CHARSET));
+ }
+ return sb.toString();
+ }
+
+ /**
+ * Read all the file data to string
+ *
+ * @param file path to read data
+ * @return file data
+ * @throws java.io.IOException
+ */
+ public static String readFile(File file) throws IOException
+ {
+ FileInputStream fileInputStream = new FileInputStream(file);
+ try
+ {
+ return readStream(fileInputStream, BUFFER_SIZE);
+ }
+ 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, String data) throws IOException
+ {
+ FileOutputStream fos = null;
+ try
+ {
+ fos = new FileOutputStream(file);
+ if (data != null)
+ {
+ fos.write(data.getBytes(CHARSET));
+ }
+ }
+ 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