1 23 24 package org.enhydra.xml.driver; 25 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileNotFoundException ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.OutputStream ; 33 import java.io.RandomAccessFile ; 34 import java.net.URL ; 35 import java.util.ArrayList ; 36 37 40 public class TestFileOps { 41 48 public static final double TOUCH_DELAY = 1.0; 49 50 51 public static void ensureFileDir(File file) { 52 File dir = file.getParentFile(); 53 if (dir != null) { 54 dir.mkdirs(); 55 } 56 } 57 58 59 public static void copyFile(File srcFile, 60 File destFile) { 61 try { 62 ensureFileDir(destFile); 63 InputStream src = new FileInputStream (srcFile); 64 try { 65 OutputStream dest = new FileOutputStream (destFile); 66 try { 67 int byteCnt; 68 byte[] buffer = new byte[8*1024]; 69 while ((byteCnt = src.read(buffer, 0, buffer.length)) >= 0) { 70 dest.write(buffer, 0, byteCnt); 71 } 72 } finally { 73 dest.close(); 74 } 75 } finally { 76 src.close(); 77 } 78 } catch (IOException except) { 79 throw new TestException("copy of \"" + srcFile + "\" to \"" 80 + destFile + "\" failed", except); 81 } 82 } 83 84 85 public static void copyFileToDir(File srcFile, 86 File destDir) { 87 destDir.mkdirs(); 88 File destFile = new File (destDir, srcFile.getName()); 89 copyFile(srcFile, destFile); 90 } 91 92 93 public static void copyFilesToDir(File [] srcFiles, 94 File destDir) { 95 for (int idx = 0; idx < srcFiles.length; idx++) { 96 copyFileToDir(srcFiles[idx], destDir); 97 } 98 } 99 100 101 public static void copyFilesToDir(String [] srcFiles, 102 File destDir) { 103 for (int idx = 0; idx < srcFiles.length; idx++) { 104 copyFileToDir(new File (srcFiles[idx]), destDir); 105 } 106 } 107 108 109 public static void copyFilesToDir(ArrayList srcFiles, 110 File destDir) { 111 for (int idx = 0; idx < srcFiles.size(); idx++) { 112 Object file = srcFiles.get(idx); 113 if (file instanceof File ) { 114 copyFileToDir((File )file, destDir); 115 } else { 116 copyFileToDir(new File ((String )file), destDir); 117 } 118 } 119 } 120 121 124 public static void sleep(double secs) { 125 try { 126 Thread.sleep((long)(secs*1000)); 127 } catch (InterruptedException except) { 128 throw new TestError(except); 129 } 130 } 131 132 135 public static void touchFile(File file) { 136 sleep(TOUCH_DELAY); 138 139 try { 140 RandomAccessFile raFile = new RandomAccessFile (file, "rw"); 141 int fileByte = raFile.read(); 142 if (fileByte < 0) { 143 throw new TestError("unexpected EOF on " + file); 144 } 145 raFile.seek(0); 146 raFile.write(fileByte); 147 raFile.close(); 148 } catch (FileNotFoundException except) { 149 throw new TestError("error touching " + file, except); 150 } catch (IOException except) { 151 throw new TestError("error touching " + file, except); 152 } 153 } 154 155 156 157 static public URL dirToUrl(File file) { 158 if (!file.isDirectory()) { 159 throw new TestError("file for URL not a directory: \"" + file 160 + "\""); 161 } 162 try { 163 return new URL ("file", null, -1, file.getCanonicalPath() + "/"); 164 } catch (IOException except) { 165 throw new TestError("can't construct URL from File", except); 166 } 167 } 168 169 170 static public URL fileToUrl(File file) { 171 try { 172 String path = file.getAbsolutePath(); 174 if (!(path.endsWith(".zip") || path.endsWith(".jar"))) { 175 path += '/'; 176 } 177 return new URL ("file", null, -1, path); 178 } catch (IOException except) { 179 throw new TestError("can't construct URL from File", except); 180 } 181 } 182 183 186 public static String getFileExt(String fname) { 187 int dotIdx = fname.lastIndexOf('.'); 188 if (dotIdx < 0) { 189 return null; 190 } else { 191 return fname.substring(dotIdx+1); 192 } 193 } 194 195 198 public static String getFileExt(File file) { 199 return getFileExt(file.getPath()); 200 } 201 } 202 | Popular Tags |