OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 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.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 public class AndroidFileSystem extends FileSystem |
| 29 { |
| 30 private FileSystemUtils fileSystemUtils = new FileSystemUtils(); |
| 31 private File basePath; |
| 32 |
| 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(); |
| 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()) |
| 64 { |
| 65 return null; |
| 66 } |
| 67 |
| 68 try |
| 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(); |
| 85 } |
| 86 |
| 87 try |
| 88 { |
| 89 fileSystemUtils.writeFile(file, data); |
| 90 } |
| 91 catch (IOException e) |
| 92 { |
| 93 throw new AdblockPlusException(e); |
| 94 } |
| 95 } |
| 96 |
| 97 @Override |
| 98 public void move(String fromPath, String toPath) |
| 99 { |
| 100 File fullFromFile = buildFullPath(fromPath); |
| 101 try |
| 102 { |
| 103 String data = fileSystemUtils.readFile(fullFromFile); |
| 104 write(toPath, data); |
| 105 remove(fromPath); |
| 106 } |
| 107 catch (FileNotFoundException e) |
| 108 { |
| 109 throw new AdblockPlusException(e); |
| 110 } |
| 111 } |
| 112 |
| 113 @Override |
| 114 public void remove(String path) |
| 115 { |
| 116 File file = buildFullPath(path); |
| 117 if (file.exists()) |
| 118 { |
| 119 file.delete(); |
| 120 } |
| 121 } |
| 122 |
| 123 @Override |
| 124 public StatResult stat(String path) |
| 125 { |
| 126 File file = buildFullPath(path); |
| 127 return new StatResult( |
| 128 file.exists(), |
| 129 file.isDirectory(), |
| 130 file.isFile(), |
| 131 file.lastModified()); |
| 132 } |
| 133 |
| 134 @Override |
| 135 public String resolve(String path) |
| 136 { |
| 137 String fullPath = buildFullPath(path).getAbsolutePath(); |
| 138 return basePath != null |
| 139 ? fullPath.substring(basePath.getAbsolutePath().length()) |
| 140 : fullPath; |
| 141 } |
| 142 } |
OLD | NEW |