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: now using smart_ptr, changed impl for reading file as string, minor changes Created May 22, 2017, 1:03 p.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.FileInputStream; 21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.FileOutputStream; 22 import java.io.FileOutputStream;
24 import java.io.IOException; 23 import java.io.IOException;
25 import java.io.InputStream; 24 import java.io.InputStream;
26 import java.util.NoSuchElementException;
27 import java.util.Scanner;
28 import java.util.UUID; 25 import java.util.UUID;
29 26
30 public class FileSystemUtils 27 public class FileSystemUtils
31 { 28 {
32 private static final int BUFFER_SIZE = 10 * 1024; // 10 Kb
33 private static final String CHARSET = "UTF-8";
34 29
35 private static String readStream(InputStream inputStream, int bufferSize) thro ws IOException 30 private static byte[] readStream(InputStream inputStream, int length) throws I OException
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
36 { 31 {
37 StringBuilder sb = new StringBuilder(); 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 }
38 40
39 byte buffer[] = new byte[bufferSize]; 41 /**
40 int bytesRead; 42 * Convert java signed int to byte
41 while ((bytesRead = inputStream.read(buffer)) > 0) 43 * @param b int representation of byte
42 { 44 * @return byte representation of byte
43 sb.append(new String(buffer, 0, bytesRead, CHARSET)); 45 */
44 } 46 public static byte byteFromInt(int b)
45 return sb.toString(); 47 {
48 return (byte)(b & 0xFF);
46 } 49 }
47 50
48 /** 51 /**
49 * Read all the file data to string 52 * Read all the file data to string
50 * 53 *
51 * @param file path to read data 54 * @param file path to read data
52 * @return file data 55 * @return file data
53 * @throws java.io.IOException 56 * @throws java.io.IOException
54 */ 57 */
55 public static String readFile(File file) throws IOException 58 public static byte[] readFile(File file) throws IOException
56 { 59 {
57 FileInputStream fileInputStream = new FileInputStream(file); 60 FileInputStream fileInputStream = new FileInputStream(file);
58 try 61 try
59 { 62 {
60 return readStream(fileInputStream, BUFFER_SIZE); 63 return readStream(fileInputStream, (int) file.length());
61 } 64 }
62 finally 65 finally
63 { 66 {
64 try 67 try
65 { 68 {
66 fileInputStream.close(); 69 fileInputStream.close();
67 } 70 }
68 catch (IOException e) 71 catch (IOException e)
69 { 72 {
70 // ignored 73 // ignored
71 } 74 }
72 } 75 }
73 } 76 }
74 77
75 /** 78 /**
76 * Write data to file (with rewriting) 79 * Write data to file (with rewriting)
77 * 80 *
78 * @param file file 81 * @param file file
79 * @param data file data 82 * @param data file data
80 */ 83 */
81 public static void writeFile(File file, String data) throws IOException 84 public static void writeFile(File file, byte[] data) throws IOException
82 { 85 {
83 FileOutputStream fos = null; 86 FileOutputStream fos = null;
84 try 87 try
85 { 88 {
86 fos = new FileOutputStream(file); 89 fos = new FileOutputStream(file);
87 if (data != null) 90 if (data != null)
88 { 91 {
89 fos.write(data.getBytes(CHARSET)); 92 fos.write(data);
90 } 93 }
91 } 94 }
92 finally 95 finally
93 { 96 {
94 if (fos != null) 97 if (fos != null)
95 { 98 {
96 try 99 try
97 { 100 {
98 fos.close(); 101 fos.close();
99 } 102 }
(...skipping 23 matching lines...) Expand all
123 sb.append(UUID.randomUUID().toString()); 126 sb.append(UUID.randomUUID().toString());
124 127
125 if (suffix != null) 128 if (suffix != null)
126 { 129 {
127 sb.append(suffix); 130 sb.append(suffix);
128 } 131 }
129 132
130 return sb.toString(); 133 return sb.toString();
131 } 134 }
132 } 135 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld