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 29347315: Issue 4231 - Fix unstable FilterEngineTest.testSetRemoveFilterChangeCallback (Closed)
Left Patch Set: Introduced file system abstraction to java and using LazyFileSystem for filter tests Created July 10, 2016, 10:47 a.m.
Right Patch Set: made helper methods static, fixed 'remove' for fs callback Created Dec. 13, 2016, 9:32 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
LEFTRIGHT
(no file at all)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 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.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.util.NoSuchElementException;
25 import java.util.Scanner;
26 import java.util.UUID;
27
28 public class FileSystemUtils
29 {
30 /**
31 * Read all the file data to string
32 *
33 * @param file path to read data
34 * @return file data
35 * @throws java.io.FileNotFoundException
36 */
37 public static String readFile(File file) throws FileNotFoundException
38 {
39 Scanner scanner = null;
40 try
41 {
42 scanner = new Scanner(file);
43 return scanner.useDelimiter("\\A").next();
44 }
45 catch (NoSuchElementException e)
46 {
47 return "";
48 }
49 finally
50 {
51 if (scanner != null)
52 {
53 scanner.close();
54 }
55 }
56 }
57
58 /**
59 * Write data to file (with rewriting)
60 *
61 * @param file file
62 * @param data file data
63 */
64 public static void writeFile(File file, String data) throws IOException
65 {
66 FileOutputStream fos = null;
67 try
68 {
69 fos = new FileOutputStream(file);
70 if (data != null)
71 {
72 fos.write(data.getBytes());
73 }
74 }
75 finally
76 {
77 if (fos != null)
78 {
79 try
80 {
81 fos.close();
diegocarloslima 2017/04/27 12:22:40 Are we using Java 7 already in libadblockplus-andr
anton 2017/04/28 08:25:45 it's not clear but seems that "not yet". For now i
82 }
83 catch (IOException e)
84 {
85 // ignored
86 }
87 }
88 }
89 }
90
91 /**
92 * Generate unique filename
93 *
94 * @param prefix prefix
95 * @param suffix suffix
96 * @return generated unique filename
97 */
98 public static String generateUniqueFileName(String prefix, String suffix)
99 {
100 StringBuilder sb = new StringBuilder();
101 if (prefix != null)
102 {
103 sb.append(prefix);
104 }
105
106 sb.append(UUID.randomUUID().toString());
107
108 if (suffix != null)
109 {
110 sb.append(suffix);
111 }
112
113 return sb.toString();
114 }
115 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld