Left: | ||
Right: |
OLD | NEW |
---|---|
(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 bytes array though JNI layer | |
33 * | |
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)`) | |
38 */ | |
39 public class AndroidFileSystem extends FileSystem | |
40 { | |
41 private File basePath; | |
42 | |
43 public File getBasePath() | |
44 { | |
45 return basePath; | |
46 } | |
47 | |
48 public AndroidFileSystem() | |
49 { | |
50 } | |
51 | |
52 /* | |
53 * Sets the base path, all paths are considered relative to it. | |
54 * @param basePath base path | |
55 */ | |
56 public AndroidFileSystem(File basePath) | |
57 { | |
58 this(); | |
59 this.basePath = basePath; | |
60 } | |
61 | |
62 @Override | |
63 public byte[] read(String path) | |
64 { | |
65 File file = new File(path); | |
anton
2017/05/29 11:30:03
Warning: it does not do resolve() just like in Def
| |
66 if (!file.exists()) | |
67 { | |
68 return null; | |
69 } | |
70 | |
71 try | |
72 { | |
73 return FileSystemUtils.readFile(file); | |
74 } | |
75 catch (IOException e) | |
76 { | |
77 throw new AdblockPlusException(e); | |
78 } | |
79 } | |
80 | |
81 @Override | |
82 public void write(String path, byte[] data) | |
83 { | |
84 File file = new File(path); | |
anton
2017/05/29 11:30:02
Warning: it does not do resolve() just like in Def
| |
85 if (file.exists()) | |
86 { | |
87 file.delete(); | |
88 } | |
89 | |
90 try | |
91 { | |
92 FileSystemUtils.writeFile(file, data); | |
93 } | |
94 catch (IOException e) | |
95 { | |
96 throw new AdblockPlusException(e); | |
97 } | |
98 } | |
99 | |
100 @Override | |
101 public void move(String fromPath, String toPath) | |
102 { | |
103 File fromFile = new File(fromPath); | |
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)) | |
109 { | |
110 throw new AdblockPlusException("Failed to move " + fromPath + " to " + toF ile); | |
111 } | |
112 } | |
113 | |
114 @Override | |
115 public void remove(String path) | |
116 { | |
117 File file = new File(path); | |
118 if (file.exists()) | |
119 { | |
120 file.delete(); | |
121 } | |
122 } | |
123 | |
124 @Override | |
125 public StatResult stat(String path) | |
126 { | |
127 File file = new File(path); | |
128 return new StatResult( | |
129 file.exists(), | |
130 file.isDirectory(), | |
131 file.isFile(), | |
132 file.lastModified()); | |
133 } | |
134 | |
135 @Override | |
136 public String resolve(String path) | |
137 { | |
138 return (basePath != null ? new File(basePath, path).getAbsolutePath() : path ); | |
139 } | |
140 } | |
OLD | NEW |