1 22 package fr.dyade.aaa.util; 23 24 import java.io.*; 25 26 final class FileRepository implements Repository { 27 File dir = null; 28 29 private int nbsaved = 0; 30 31 36 public int getNbSavedObjects() { 37 return nbsaved; 38 } 39 40 private int nbdeleted = 0; 41 42 47 public int getNbDeletedObjects() { 48 return nbdeleted; 49 } 50 51 private int baddeleted = 0; 52 53 58 public int getNbBadDeletedObjects() { 59 return baddeleted; 60 } 61 62 private int nbloaded = 0; 63 64 69 public int getNbLoadedObjects() { 70 return nbloaded; 71 } 72 73 FileRepository() { 74 } 75 76 80 public void init(File dir) throws IOException { 81 this.dir = dir; 82 } 83 84 89 public String [] list(String prefix) throws IOException { 90 return dir.list(new StartWithFilter(prefix)); 91 } 92 93 96 public void save(String dirName, String name, byte[] content) throws IOException { 97 File file; 98 if (dirName == null) { 99 file = new File(dir, name); 100 } else { 101 File parentDir = new File(dir, dirName); 102 if (!parentDir.exists()) { 103 parentDir.mkdirs(); 104 } 105 file = new File(parentDir, name); 106 } 107 108 FileOutputStream fos = new FileOutputStream(file); 109 fos.write(content); 110 fos.getFD().sync(); 111 fos.close(); 112 113 nbsaved += 1; 114 } 115 116 121 public Object loadobj(String dirName, String name) throws IOException, ClassNotFoundException { 122 File file; 123 Object obj; 124 if (dirName == null) { 125 file = new File(dir, name); 126 } else { 127 File parentDir = new File(dir, dirName); 128 file = new File(parentDir, name); 129 } 130 131 FileInputStream fis = new FileInputStream(file); 132 ObjectInputStream ois = new ObjectInputStream(fis); 133 try { 134 obj = ois.readObject(); 135 } finally { 136 ois.close(); 137 fis.close(); 138 } 139 140 nbloaded += 1; 141 return obj; 142 } 143 144 149 public byte[] load(String dirName, String name) throws IOException { 150 File file; 152 if (dirName == null) { 153 file = new File(dir, name); 154 } else { 155 File parentDir = new File(dir, dirName); 156 file = new File(parentDir, name); 157 } 158 FileInputStream fis = new FileInputStream(file); 159 byte[] buf = new byte[(int) file.length()]; 160 for (int nb=0; nb<buf.length; ) { 161 int ret = fis.read(buf, nb, buf.length-nb); 162 if (ret == -1) throw new EOFException(); 163 nb += ret; 164 } 165 fis.close(); 166 167 nbloaded += 1; 168 return buf; 169 } 170 171 174 public void delete(String dirName, String name) throws IOException { 175 File file; 176 if (dirName == null) { 177 if (! new File(dir, name).delete()) baddeleted += 1; 178 } else { 179 File parentDir = new File(dir, dirName); 180 file = new File(parentDir, name); 181 if (! new File(parentDir, name).delete()) baddeleted += 1; 182 deleteDir(parentDir); 183 } 184 nbdeleted += 1; 185 } 186 187 191 private final void deleteDir(File dir) { 192 String [] children = dir.list(); 195 if (children != null && children.length == 0) { 197 dir.delete(); 198 if (dir.getAbsolutePath().length() > 199 this.dir.getAbsolutePath().length()) { 200 deleteDir(dir.getParentFile()); 201 } 202 } 203 } 204 205 208 public void commit() throws IOException {} 209 210 213 public void close() throws IOException {} 214 } 215 | Popular Tags |