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

Side by Side Diff: libadblockplus-android/src/org/adblockplus/libadblockplus/android/AndroidFileSystem.java

Issue 29424615: Issue 4231 - Fix unstable FilterEngineTest.testSetRemoveFilterChangeCallback (Closed)
Patch Set: now using smart_ptr, changed impl for reading file as string, minor changes Created May 22, 2017, 1:03 p.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.android;
19
20 import org.adblockplus.libadblockplus.AdblockPlusException;
21 import org.adblockplus.libadblockplus.FileSystem;
22 import org.adblockplus.libadblockplus.FileSystemUtils;
23
24 import java.io.File;
25 import java.io.IOException;
26
27 /**
28 * AndroidFileSystem is inefficient for production files routines
29 * as C++ streams can't pass JNI layer and be converted into Java streams and vi ce versa.
30 *
31 * So in case of any stream routines full stream content is read in either C++ o r Java side
32 * and it's being passed as string though JNI layer
33 *
34 * AndroidFileSystem is meant to be used in tests mostly
35 */
36 public class AndroidFileSystem extends FileSystem
37 {
38 private File basePath;
39
40 public File getBasePath()
41 {
42 return basePath;
43 }
44
45 public AndroidFileSystem()
46 {
47 }
48
49 /*
50 * Sets the base path, all paths are considered relative to it.
51 * @param basePath base path
52 */
53 public AndroidFileSystem(File basePath)
54 {
55 this();
56 this.basePath = basePath;
57 }
58
59 private File buildFullPath(String path)
60 {
61 return basePath != null
62 ? new File(basePath, path)
63 : new File(path);
64 }
65
66 @Override
67 public String read(String path)
68 {
69 File file = buildFullPath(path);
70 if (!file.exists())
71 {
72 return null;
73 }
74
75 try
76 {
77 return FileSystemUtils.readFile(file);
78 }
79 catch (IOException e)
80 {
81 throw new AdblockPlusException(e);
82 }
83 }
84
85 @Override
86 public void write(String path, String data)
87 {
88 File file = buildFullPath(path);
89 if (file.exists())
90 {
91 file.delete();
92 }
93
94 try
95 {
96 FileSystemUtils.writeFile(file, data);
97 }
98 catch (IOException e)
99 {
100 throw new AdblockPlusException(e);
101 }
102 }
103
104 @Override
105 public void move(String fromPath, String toPath)
106 {
107 File fullFromFile = buildFullPath(fromPath);
108 try
109 {
110 String data = FileSystemUtils.readFile(fullFromFile);
111 write(toPath, data);
112 remove(fromPath);
113 }
114 catch (IOException e)
115 {
116 throw new AdblockPlusException(e);
117 }
118 }
119
120 @Override
121 public void remove(String path)
122 {
123 File file = buildFullPath(path);
124 if (file.exists())
125 {
126 file.delete();
127 }
128 }
129
130 @Override
131 public StatResult stat(String path)
132 {
133 File file = buildFullPath(path);
134 return new StatResult(
135 file.exists(),
136 file.isDirectory(),
137 file.isFile(),
138 file.lastModified());
139 }
140
141 @Override
142 public String resolve(String path)
143 {
144 String fullPath = buildFullPath(path).getAbsolutePath();
145 return basePath != null
146 ? fullPath.substring(basePath.getAbsolutePath().length())
147 : fullPath;
148 }
149 }
OLDNEW

Powered by Google App Engine
This is Rietveld