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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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.libadblockplus.android; | 18 package org.adblockplus.libadblockplus.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.FileSystemUtils; | 22 import org.adblockplus.libadblockplus.FileSystemUtils; |
23 | 23 |
24 import java.io.File; | 24 import java.io.File; |
25 import java.io.FileNotFoundException; | |
26 import java.io.IOException; | 25 import java.io.IOException; |
27 | 26 |
28 /** | 27 /** |
29 * AndroidFileSystem is inefficient for production files routines | 28 * AndroidFileSystem is inefficient for production files routines |
30 * as C++ streams can't pass JNI layer and be converted into Java streams and vi ce versa. | 29 * as C++ streams can't pass JNI layer and be converted into Java streams and vi ce versa. |
31 * | 30 * |
32 * So in case of any stream routines full stream content is read in either C++ o r Java side | 31 * So in case of any stream routines full stream content is read in either C++ o r Java side |
33 * and it's being passed as string though JNI layer | 32 * and it's being passed as bytes array though JNI layer |
34 * | 33 * |
35 * AndroidFileSystem is meant to be used in tests mostly | 34 * AndroidFileSystem is meant to be used in tests mostly |
35 * | |
36 * All paths are considered relative to the base path, or to be absolute | |
37 * (see `resolve(String path)`) if no base path is set (see `AndroidFileSystem(F ile basePath)`) | |
36 */ | 38 */ |
37 public class AndroidFileSystem extends FileSystem | 39 public class AndroidFileSystem extends FileSystem |
38 { | 40 { |
39 private File basePath; | 41 private File basePath; |
40 | 42 |
41 public File getBasePath() | 43 public File getBasePath() |
42 { | 44 { |
43 return basePath; | 45 return basePath; |
44 } | 46 } |
45 | 47 |
46 public AndroidFileSystem() | 48 public AndroidFileSystem() |
47 { | 49 { |
48 } | 50 } |
49 | 51 |
50 /* | 52 /* |
51 * Sets the base path, all paths are considered relative to it. | 53 * Sets the base path, all paths are considered relative to it. |
52 * @param basePath base path | 54 * @param basePath base path |
53 */ | 55 */ |
54 public AndroidFileSystem(File basePath) | 56 public AndroidFileSystem(File basePath) |
55 { | 57 { |
56 this(); | 58 this(); |
57 this.basePath = basePath; | 59 this.basePath = basePath; |
58 } | 60 } |
59 | 61 |
60 private File buildFullPath(String path) | 62 @Override |
63 public byte[] read(String path) | |
61 { | 64 { |
62 return basePath != null | 65 File file = new File(path); |
anton
2017/05/29 11:30:03
Warning: it does not do resolve() just like in Def
| |
63 ? new File(basePath, path) | |
64 : new File(path); | |
65 } | |
66 | |
67 @Override | |
68 public String read(String path) | |
69 { | |
70 File file = buildFullPath(path); | |
71 if (!file.exists()) | 66 if (!file.exists()) |
72 { | 67 { |
73 return null; | 68 return null; |
74 } | 69 } |
75 | 70 |
76 try | 71 try |
77 { | 72 { |
78 return FileSystemUtils.readFile(file); | 73 return FileSystemUtils.readFile(file); |
79 } | 74 } |
80 catch (FileNotFoundException e) | 75 catch (IOException e) |
81 { | 76 { |
82 throw new AdblockPlusException(e); | 77 throw new AdblockPlusException(e); |
83 } | 78 } |
84 } | 79 } |
85 | 80 |
86 @Override | 81 @Override |
87 public void write(String path, String data) | 82 public void write(String path, byte[] data) |
88 { | 83 { |
89 File file = buildFullPath(path); | 84 File file = new File(path); |
anton
2017/05/29 11:30:02
Warning: it does not do resolve() just like in Def
| |
90 if (file.exists()) | 85 if (file.exists()) |
91 { | 86 { |
92 file.delete(); | 87 file.delete(); |
93 } | 88 } |
94 | 89 |
95 try | 90 try |
96 { | 91 { |
97 FileSystemUtils.writeFile(file, data); | 92 FileSystemUtils.writeFile(file, data); |
98 } | 93 } |
99 catch (IOException e) | 94 catch (IOException e) |
100 { | 95 { |
101 throw new AdblockPlusException(e); | 96 throw new AdblockPlusException(e); |
102 } | 97 } |
103 } | 98 } |
104 | 99 |
105 @Override | 100 @Override |
106 public void move(String fromPath, String toPath) | 101 public void move(String fromPath, String toPath) |
107 { | 102 { |
108 File fullFromFile = buildFullPath(fromPath); | 103 File fromFile = new File(fromPath); |
109 try | 104 if (!fromFile.exists()) |
105 throw new AdblockPlusException("File does not exist: " + fromPath); | |
106 | |
107 File toFile = new File(toPath); | |
108 if (!fromFile.renameTo(toFile)) | |
110 { | 109 { |
111 String data = FileSystemUtils.readFile(fullFromFile); | 110 throw new AdblockPlusException("Failed to move " + fromPath + " to " + toF ile); |
112 write(toPath, data); | |
113 remove(fromPath); | |
114 } | |
115 catch (FileNotFoundException e) | |
116 { | |
117 throw new AdblockPlusException(e); | |
118 } | 111 } |
119 } | 112 } |
120 | 113 |
121 @Override | 114 @Override |
122 public void remove(String path) | 115 public void remove(String path) |
123 { | 116 { |
124 File file = buildFullPath(path); | 117 File file = new File(path); |
125 if (file.exists()) | 118 if (file.exists()) |
126 { | 119 { |
127 file.delete(); | 120 file.delete(); |
128 } | 121 } |
129 } | 122 } |
130 | 123 |
131 @Override | 124 @Override |
132 public StatResult stat(String path) | 125 public StatResult stat(String path) |
133 { | 126 { |
134 File file = buildFullPath(path); | 127 File file = new File(path); |
135 return new StatResult( | 128 return new StatResult( |
136 file.exists(), | 129 file.exists(), |
137 file.isDirectory(), | 130 file.isDirectory(), |
138 file.isFile(), | 131 file.isFile(), |
139 file.lastModified()); | 132 file.lastModified()); |
140 } | 133 } |
141 | 134 |
142 @Override | 135 @Override |
143 public String resolve(String path) | 136 public String resolve(String path) |
144 { | 137 { |
145 String fullPath = buildFullPath(path).getAbsolutePath(); | 138 return (basePath != null ? new File(basePath, path).getAbsolutePath() : path ); |
146 return basePath != null | |
147 ? fullPath.substring(basePath.getAbsolutePath().length()) | |
148 : fullPath; | |
149 } | 139 } |
150 } | 140 } |
LEFT | RIGHT |