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.FileNotFoundException; |
| 26 import java.io.IOException; |
| 27 |
| 28 /** |
| 29 * 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. |
| 31 * |
| 32 * So in case of any stream routines full stream content is read in either C++ o
r Java side |
| 33 * and it's is being passed as string though JNI layer |
| 34 * |
| 35 * AndroidFileSystem is meant to be used in tests mostly |
| 36 */ |
| 37 public class AndroidFileSystem extends FileSystem |
| 38 { |
| 39 private File basePath; |
| 40 |
| 41 public File getBasePath() |
| 42 { |
| 43 return basePath; |
| 44 } |
| 45 |
| 46 public AndroidFileSystem() |
| 47 { |
| 48 } |
| 49 |
| 50 /* |
| 51 * Sets the base path, all paths are considered relative to it. |
| 52 * @param basePath base path |
| 53 */ |
| 54 public AndroidFileSystem(File basePath) |
| 55 { |
| 56 this(); |
| 57 this.basePath = basePath; |
| 58 } |
| 59 |
| 60 private File buildFullPath(String path) |
| 61 { |
| 62 return basePath != null |
| 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()) |
| 72 { |
| 73 return null; |
| 74 } |
| 75 |
| 76 try |
| 77 { |
| 78 return FileSystemUtils.readFile(file); |
| 79 } |
| 80 catch (FileNotFoundException e) |
| 81 { |
| 82 throw new AdblockPlusException(e); |
| 83 } |
| 84 } |
| 85 |
| 86 @Override |
| 87 public void write(String path, String data) |
| 88 { |
| 89 File file = buildFullPath(path); |
| 90 if (file.exists()) |
| 91 { |
| 92 file.delete(); |
| 93 } |
| 94 |
| 95 try |
| 96 { |
| 97 FileSystemUtils.writeFile(file, data); |
| 98 } |
| 99 catch (IOException e) |
| 100 { |
| 101 throw new AdblockPlusException(e); |
| 102 } |
| 103 } |
| 104 |
| 105 @Override |
| 106 public void move(String fromPath, String toPath) |
| 107 { |
| 108 File fullFromFile = buildFullPath(fromPath); |
| 109 try |
| 110 { |
| 111 String data = FileSystemUtils.readFile(fullFromFile); |
| 112 write(toPath, data); |
| 113 remove(fromPath); |
| 114 } |
| 115 catch (FileNotFoundException e) |
| 116 { |
| 117 throw new AdblockPlusException(e); |
| 118 } |
| 119 } |
| 120 |
| 121 @Override |
| 122 public void remove(String path) |
| 123 { |
| 124 File file = buildFullPath(path); |
| 125 if (file.exists()) |
| 126 { |
| 127 file.delete(); |
| 128 } |
| 129 } |
| 130 |
| 131 @Override |
| 132 public StatResult stat(String path) |
| 133 { |
| 134 File file = buildFullPath(path); |
| 135 return new StatResult( |
| 136 file.exists(), |
| 137 file.isDirectory(), |
| 138 file.isFile(), |
| 139 file.lastModified()); |
| 140 } |
| 141 |
| 142 @Override |
| 143 public String resolve(String path) |
| 144 { |
| 145 String fullPath = buildFullPath(path).getAbsolutePath(); |
| 146 return basePath != null |
| 147 ? fullPath.substring(basePath.getAbsolutePath().length()) |
| 148 : fullPath; |
| 149 } |
| 150 } |
OLD | NEW |