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

Delta Between Two Patch Sets: libadblockplus-android/src/org/adblockplus/libadblockplus/FileSystemUtils.java

Issue 29424615: Issue 4231 - Fix unstable FilterEngineTest.testSetRemoveFilterChangeCallback (Closed)
Left Patch Set: using method from c++ utils Created April 28, 2017, 10:44 a.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 package org.adblockplus.libadblockplus; 18 package org.adblockplus.libadblockplus;
19 19
20 import java.io.File; 20 import java.io.File;
21 import java.io.FileNotFoundException; 21 import java.io.FileInputStream;
22 import java.io.FileOutputStream; 22 import java.io.FileOutputStream;
23 import java.io.IOException; 23 import java.io.IOException;
24 import java.util.NoSuchElementException; 24 import java.io.InputStream;
25 import java.util.Scanner;
26 import java.util.UUID; 25 import java.util.UUID;
27 26
28 public class FileSystemUtils 27 public class FileSystemUtils
29 { 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
30 /** 51 /**
31 * Read all the file data to string 52 * Read all the file data to string
32 * 53 *
33 * @param file path to read data 54 * @param file path to read data
34 * @return file data 55 * @return file data
35 * @throws java.io.FileNotFoundException 56 * @throws java.io.IOException
36 */ 57 */
37 public static String readFile(File file) throws FileNotFoundException 58 public static byte[] readFile(File file) throws IOException
38 { 59 {
39 Scanner scanner = null; 60 FileInputStream fileInputStream = new FileInputStream(file);
40 try 61 try
41 { 62 {
42 scanner = new Scanner(file); 63 return readStream(fileInputStream, (int) file.length());
43 return scanner.useDelimiter("\\A").next();
sergei 2017/05/22 12:09:07 I'm not sure that it's the best way to read files
anton 2017/05/22 13:04:38 there are lots of ways for "reading file as string
44 }
45 catch (NoSuchElementException e)
46 {
47 return "";
48 } 64 }
49 finally 65 finally
50 { 66 {
51 if (scanner != null) 67 try
52 { 68 {
53 scanner.close(); 69 fileInputStream.close();
70 }
71 catch (IOException e)
72 {
73 // ignored
54 } 74 }
55 } 75 }
56 } 76 }
57 77
58 /** 78 /**
59 * Write data to file (with rewriting) 79 * Write data to file (with rewriting)
60 * 80 *
61 * @param file file 81 * @param file file
62 * @param data file data 82 * @param data file data
63 */ 83 */
64 public static void writeFile(File file, String data) throws IOException 84 public static void writeFile(File file, byte[] data) throws IOException
65 { 85 {
66 FileOutputStream fos = null; 86 FileOutputStream fos = null;
67 try 87 try
68 { 88 {
69 fos = new FileOutputStream(file); 89 fos = new FileOutputStream(file);
70 if (data != null) 90 if (data != null)
71 { 91 {
72 fos.write(data.getBytes()); 92 fos.write(data);
sergei 2017/05/22 12:09:07 If we still use String then I think we should pass
anton 2017/05/22 13:04:38 Acknowledged.
73 } 93 }
74 } 94 }
75 finally 95 finally
76 { 96 {
77 if (fos != null) 97 if (fos != null)
78 { 98 {
79 try 99 try
80 { 100 {
81 fos.close(); 101 fos.close();
82 } 102 }
(...skipping 23 matching lines...) Expand all
106 sb.append(UUID.randomUUID().toString()); 126 sb.append(UUID.randomUUID().toString());
107 127
108 if (suffix != null) 128 if (suffix != null)
109 { 129 {
110 sb.append(suffix); 130 sb.append(suffix);
111 } 131 }
112 132
113 return sb.toString(); 133 return sb.toString();
114 } 134 }
115 } 135 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld