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

Side by Side 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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package org.adblockplus.libadblockplus;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.UUID;
26
27 public class FileSystemUtils
28 {
29
30 private static byte[] readStream(InputStream inputStream, int length) throws I OException
31 {
32 byte[] data = new byte[length];
33 int bytesRead, totalBytesRead = 0;
34 while ((bytesRead = inputStream.read(data, totalBytesRead, length - totalByt esRead)) > 0)
35 {
36 totalBytesRead += bytesRead;
37 }
38 return data;
39 }
40
41 /**
42 * Convert java signed int to byte
43 * @param b int representation of byte
44 * @return byte representation of byte
45 */
46 public static byte byteFromInt(int b)
47 {
48 return (byte)(b & 0xFF);
49 }
50
51 /**
52 * Read all the file data to string
53 *
54 * @param file path to read data
55 * @return file data
56 * @throws java.io.IOException
57 */
58 public static byte[] readFile(File file) throws IOException
59 {
60 FileInputStream fileInputStream = new FileInputStream(file);
61 try
62 {
63 return readStream(fileInputStream, (int) file.length());
64 }
65 finally
66 {
67 try
68 {
69 fileInputStream.close();
70 }
71 catch (IOException e)
72 {
73 // ignored
74 }
75 }
76 }
77
78 /**
79 * Write data to file (with rewriting)
80 *
81 * @param file file
82 * @param data file data
83 */
84 public static void writeFile(File file, byte[] data) throws IOException
85 {
86 FileOutputStream fos = null;
87 try
88 {
89 fos = new FileOutputStream(file);
90 if (data != null)
91 {
92 fos.write(data);
93 }
94 }
95 finally
96 {
97 if (fos != null)
98 {
99 try
100 {
101 fos.close();
102 }
103 catch (IOException e)
104 {
105 // ignored
106 }
107 }
108 }
109 }
110
111 /**
112 * Generate unique filename
113 *
114 * @param prefix prefix
115 * @param suffix suffix
116 * @return generated unique filename
117 */
118 public static String generateUniqueFileName(String prefix, String suffix)
119 {
120 StringBuilder sb = new StringBuilder();
121 if (prefix != null)
122 {
123 sb.append(prefix);
124 }
125
126 sb.append(UUID.randomUUID().toString());
127
128 if (suffix != null)
129 {
130 sb.append(suffix);
131 }
132
133 return sb.toString();
134 }
135 }
OLDNEW

Powered by Google App Engine
This is Rietveld