1 30 31 32 package org.hsqldb.lib; 33 34 import java.io.File ; 35 import java.io.FileDescriptor ; 36 import java.io.FileInputStream ; 37 import java.io.FileOutputStream ; 38 import java.io.IOException ; 39 import java.util.Random ; 40 41 import org.hsqldb.lib.java.JavaSystem; 42 43 53 public class FileUtil implements FileAccess { 54 55 private static FileUtil fileUtil; 56 57 58 FileUtil() {} 59 60 public static FileUtil getDefaultInstance() { 61 62 if (fileUtil == null) { 63 fileUtil = new FileUtil(); 64 } 65 66 return fileUtil; 67 } 68 69 public boolean isStreamElement(java.lang.String elementName) { 70 return (new File (elementName)).exists(); 71 } 72 73 public java.io.InputStream openInputStreamElement( 74 java.lang.String streamName) throws java.io.IOException { 75 76 try { 77 return new FileInputStream (new File (streamName)); 78 } catch (Throwable e) { 79 throw toIOException(e); 80 } 81 } 82 83 public void createParentDirs(java.lang.String filename) { 84 makeParentDirectories(new File (filename)); 85 } 86 87 public void removeElement(java.lang.String filename) { 88 89 if (isStreamElement(filename)) { 90 delete(filename); 91 } 92 } 93 94 public void renameElement(java.lang.String oldName, 95 java.lang.String newName) { 96 renameOverwrite(oldName, newName); 97 } 98 99 public java.io.OutputStream openOutputStreamElement( 100 java.lang.String streamName) throws java.io.IOException { 101 return new FileOutputStream (new File (streamName)); 102 } 103 104 public static final boolean fsIsIgnoreCase = 114 (new File ("A")).equals(new File ("a")); 115 116 public static final boolean fsNormalizesPosixSeparator = 119 (new File ("/")).getPath().endsWith(File.separator); 120 121 static final Random random = new Random (System.currentTimeMillis()); 123 124 127 public static void delete(String filename) { 128 (new File (filename)).delete(); 129 } 130 131 146 public static void deleteOnExit(File f) { 147 JavaSystem.deleteOnExit(f); 148 } 149 150 153 public static boolean exists(String filename) { 154 return (new File (filename)).exists(); 155 } 156 157 public static boolean exists(String fileName, boolean resource, 158 Class cla) { 159 160 if (fileName == null || fileName.length() == 0) { 161 return false; 162 } 163 164 return resource ? null != cla.getResource(fileName) 165 : FileUtil.exists(fileName); 166 } 167 168 175 public static void renameOverwrite(String oldname, String newname) { 176 177 delete(newname); 178 179 if (exists(oldname)) { 180 File file = new File (oldname); 181 182 file.renameTo(new File (newname)); 183 } 184 } 185 186 public static IOException toIOException(Throwable e) { 187 188 if (e instanceof IOException ) { 189 return (IOException ) e; 190 } else { 191 return new IOException (e.toString()); 192 } 193 } 194 195 201 public static String absolutePath(String path) { 202 return (new File (path)).getAbsolutePath(); 203 } 204 205 212 public static File canonicalFile(File f) throws IOException { 213 return new File (f.getCanonicalPath()); 214 } 215 216 223 public static File canonicalFile(String path) throws IOException { 224 return new File (new File (path).getCanonicalPath()); 225 } 226 227 234 public static String canonicalPath(File f) throws IOException { 235 return f.getCanonicalPath(); 236 } 237 238 245 public static String canonicalPath(String path) throws IOException { 246 return new File (path).getCanonicalPath(); 247 } 248 249 257 public static String canonicalOrAbsolutePath(String path) { 258 259 try { 260 return canonicalPath(path); 261 } catch (Exception e) { 262 return absolutePath(path); 263 } 264 } 265 266 public static void makeParentDirectories(File f) { 267 268 String parent = f.getParent(); 269 270 if (parent != null) { 271 new File (parent).mkdirs(); 272 } else { 273 274 parent = f.getPath(); 276 277 int index = parent.lastIndexOf('/'); 278 279 if (index > 0) { 280 parent = parent.substring(0, index); 281 282 new File (parent).mkdirs(); 283 } 284 } 285 } 286 287 public class FileSync implements FileAccess.FileSync { 288 289 FileDescriptor outDescriptor; 290 291 FileSync(FileOutputStream os) throws java.io.IOException { 292 outDescriptor = os.getFD(); 293 } 294 295 public void sync() throws java.io.IOException { 296 outDescriptor.sync(); 297 } 298 } 299 300 public FileAccess.FileSync getFileSync(java.io.OutputStream os) 301 throws java.io.IOException { 302 return new FileSync((FileOutputStream ) os); 303 } 304 } 305 | Popular Tags |