Left: | ||
Right: |
OLD | NEW |
---|---|
(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.FileNotFoundException; | |
23 import java.io.FileOutputStream; | |
24 import java.io.IOException; | |
25 import java.io.InputStream; | |
26 import java.util.NoSuchElementException; | |
27 import java.util.Scanner; | |
28 import java.util.UUID; | |
29 | |
30 public class FileSystemUtils | |
31 { | |
32 private static final int BUFFER_SIZE = 10 * 1024; // 10 Kb | |
33 private static final String CHARSET = "UTF-8"; | |
34 | |
35 private static String readStream(InputStream inputStream, int bufferSize) thro ws 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
| |
36 { | |
37 StringBuilder sb = new StringBuilder(); | |
38 | |
39 byte buffer[] = new byte[bufferSize]; | |
40 int bytesRead; | |
41 while ((bytesRead = inputStream.read(buffer)) > 0) | |
42 { | |
43 sb.append(new String(buffer, 0, bytesRead, CHARSET)); | |
44 } | |
45 return sb.toString(); | |
46 } | |
47 | |
48 /** | |
49 * Read all the file data to string | |
50 * | |
51 * @param file path to read data | |
52 * @return file data | |
53 * @throws java.io.IOException | |
54 */ | |
55 public static String readFile(File file) throws IOException | |
56 { | |
57 FileInputStream fileInputStream = new FileInputStream(file); | |
58 try | |
59 { | |
60 return readStream(fileInputStream, BUFFER_SIZE); | |
61 } | |
62 finally | |
63 { | |
64 try | |
65 { | |
66 fileInputStream.close(); | |
67 } | |
68 catch (IOException e) | |
69 { | |
70 // ignored | |
71 } | |
72 } | |
73 } | |
74 | |
75 /** | |
76 * Write data to file (with rewriting) | |
77 * | |
78 * @param file file | |
79 * @param data file data | |
80 */ | |
81 public static void writeFile(File file, String data) throws IOException | |
82 { | |
83 FileOutputStream fos = null; | |
84 try | |
85 { | |
86 fos = new FileOutputStream(file); | |
87 if (data != null) | |
88 { | |
89 fos.write(data.getBytes(CHARSET)); | |
90 } | |
91 } | |
92 finally | |
93 { | |
94 if (fos != null) | |
95 { | |
96 try | |
97 { | |
98 fos.close(); | |
99 } | |
100 catch (IOException e) | |
101 { | |
102 // ignored | |
103 } | |
104 } | |
105 } | |
106 } | |
107 | |
108 /** | |
109 * Generate unique filename | |
110 * | |
111 * @param prefix prefix | |
112 * @param suffix suffix | |
113 * @return generated unique filename | |
114 */ | |
115 public static String generateUniqueFileName(String prefix, String suffix) | |
116 { | |
117 StringBuilder sb = new StringBuilder(); | |
118 if (prefix != null) | |
119 { | |
120 sb.append(prefix); | |
121 } | |
122 | |
123 sb.append(UUID.randomUUID().toString()); | |
124 | |
125 if (suffix != null) | |
126 { | |
127 sb.append(suffix); | |
128 } | |
129 | |
130 return sb.toString(); | |
131 } | |
132 } | |
OLD | NEW |