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