Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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.android; | 18 package org.adblockplus.android; |
19 | 19 |
20 import org.adblockplus.libadblockplus.AdblockPlusException; | 20 import org.adblockplus.libadblockplus.AdblockPlusException; |
21 import org.adblockplus.libadblockplus.FileSystem; | 21 import org.adblockplus.libadblockplus.FileSystem; |
22 import org.adblockplus.libadblockplus.FileSystemHelper; | 22 import org.adblockplus.libadblockplus.FileSystemUtils; |
23 | 23 |
24 import java.io.File; | 24 import java.io.File; |
25 import java.io.FileNotFoundException; | 25 import java.io.FileNotFoundException; |
26 import java.io.IOException; | 26 import java.io.IOException; |
27 | 27 |
28 public class AndroidFileSystem extends FileSystem | 28 public class AndroidFileSystem extends FileSystem |
29 { | 29 { |
30 private FileSystemHelper fileSystemHelper = new FileSystemHelper(); | 30 private FileSystemUtils fileSystemUtils = new FileSystemUtils(); |
31 private File basePath; | 31 private File basePath; |
32 | 32 |
33 public File getBasePath() | 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(); | |
diegocarloslima
2017/04/27 12:22:40
This call isn't required
anton
2017/04/28 08:25:45
I think it's required as if we have some init logi
| |
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()) | |
34 { | 64 { |
35 return basePath; | 65 return null; |
36 } | 66 } |
37 | 67 |
38 public AndroidFileSystem() | 68 try |
39 { | 69 { |
70 return fileSystemUtils.readFile(file); | |
71 } | |
72 catch (FileNotFoundException e) | |
73 { | |
74 throw new AdblockPlusException(e); | |
75 } | |
76 } | |
77 | |
78 @Override | |
79 public void write(String path, String data) | |
80 { | |
81 File file = buildFullPath(path); | |
82 if (file.exists()) | |
83 { | |
84 file.delete(); | |
40 } | 85 } |
41 | 86 |
42 /* | 87 try |
43 * Sets the base path, all paths are considered relative to it. | |
44 * @param basePath base path | |
45 */ | |
46 public AndroidFileSystem(File basePath) | |
47 { | 88 { |
48 this(); | 89 fileSystemUtils.writeFile(file, data); |
49 this.basePath = basePath; | |
50 } | 90 } |
91 catch (IOException e) | |
92 { | |
93 throw new AdblockPlusException(e); | |
94 } | |
95 } | |
51 | 96 |
52 private File buildFullPath(String path) | 97 @Override |
98 public void move(String fromPath, String toPath) | |
99 { | |
100 File fullFromFile = buildFullPath(fromPath); | |
101 try | |
53 { | 102 { |
54 return basePath != null | 103 String data = fileSystemUtils.readFile(fullFromFile); |
55 ? new File(basePath, path) | 104 write(toPath, data); |
56 : new File(path); | 105 remove(fromPath); |
57 } | 106 } |
107 catch (FileNotFoundException e) | |
108 { | |
109 throw new AdblockPlusException(e); | |
110 } | |
111 } | |
58 | 112 |
59 @Override | 113 @Override |
60 public String read(String path) | 114 public void remove(String path) |
115 { | |
116 File file = buildFullPath(path); | |
117 if (file.exists()) | |
61 { | 118 { |
62 File file = buildFullPath(path); | 119 file.delete(); |
63 if (!file.exists()) | 120 } |
64 return null; | 121 } |
65 | 122 |
66 try | 123 @Override |
67 { | 124 public StatResult stat(String path) |
68 return fileSystemHelper.readFile(file); | 125 { |
69 } | 126 File file = buildFullPath(path); |
70 catch (FileNotFoundException e) | 127 return new StatResult( |
71 { | 128 file.exists(), |
72 throw new AdblockPlusException(e); | 129 file.isDirectory(), |
73 } | 130 file.isFile(), |
74 } | 131 file.lastModified()); |
132 } | |
75 | 133 |
76 @Override | 134 @Override |
77 public void write(String path, String data) | 135 public String resolve(String path) |
78 { | 136 { |
79 File file = buildFullPath(path); | 137 String fullPath = buildFullPath(path).getAbsolutePath(); |
80 if (file.exists()) | 138 return basePath != null |
81 file.delete(); | 139 ? fullPath.substring(basePath.getAbsolutePath().length()) |
82 | 140 : fullPath; |
83 try | 141 } |
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 } | 142 } |
LEFT | RIGHT |