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

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

Issue 29347315: Issue 4231 - Fix unstable FilterEngineTest.testSetRemoveFilterChangeCallback (Closed)
Patch Set: Introduced file system abstraction to java and using LazyFileSystem for filter tests Created July 10, 2016, 10:47 a.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-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.android;
19
20 import org.adblockplus.libadblockplus.AdblockPlusException;
21 import org.adblockplus.libadblockplus.FileSystem;
22 import org.adblockplus.libadblockplus.FileSystemHelper;
23
24 import java.io.File;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27
28 public class AndroidFileSystem extends FileSystem
29 {
30 private FileSystemHelper fileSystemHelper = new FileSystemHelper();
31 private File basePath;
32
33 public File getBasePath()
34 {
35 return basePath;
36 }
37
38 public AndroidFileSystem()
39 {
40 }
41
42 /*
43 * Sets the base path, all paths are considered relative to it.
44 * @param basePath base path
45 */
46 public AndroidFileSystem(File basePath)
47 {
48 this();
49 this.basePath = basePath;
50 }
51
52 private File buildFullPath(String path)
53 {
54 return basePath != null
55 ? new File(basePath, path)
56 : new File(path);
57 }
58
59 @Override
60 public String read(String path)
61 {
62 File file = buildFullPath(path);
63 if (!file.exists())
64 return null;
65
66 try
67 {
68 return fileSystemHelper.readFile(file);
69 }
70 catch (FileNotFoundException e)
71 {
72 throw new AdblockPlusException(e);
73 }
74 }
75
76 @Override
77 public void write(String path, String data)
78 {
79 File file = buildFullPath(path);
80 if (file.exists())
81 file.delete();
82
83 try
84 {
85 fileSystemHelper.writeFile(file, data);
86 }
87 catch (IOException e)
88 {
89 throw new AdblockPlusException(e);
90 }
91 }
92
93 @Override
94 public void move(String fromPath, String toPath)
95 {
96 File fullFromFile = buildFullPath(fromPath);
97 try
98 {
99 String data = fileSystemHelper.readFile(fullFromFile);
100 write(toPath, data);
101 remove(fromPath);
102 }
103 catch (FileNotFoundException e)
104 {
105 throw new AdblockPlusException(e);
106 }
107 }
108
109 @Override
110 public void remove(String path)
111 {
112 File file = buildFullPath(path);
113 if (file.exists())
114 file.delete();
115 }
116
117 @Override
118 public StatResult stat(String path)
119 {
120 File file = buildFullPath(path);
121 return new StatResult(
122 file.exists(),
123 file.isDirectory(),
124 file.isFile(),
125 file.lastModified());
126 }
127
128 @Override
129 public String resolve(String path)
130 {
131 String fullPath = buildFullPath(path).getAbsolutePath();
132 return basePath != null
133 ? fullPath.substring(basePath.getAbsolutePath().length())
134 : fullPath;
135 }
136 }
OLDNEW

Powered by Google App Engine
This is Rietveld