1 22 23 package org.gjt.sp.util; 24 25 import java.io.*; 26 27 34 public class IOUtilities 35 { 36 51 public static boolean moveFile(File source, File dest) 52 { 53 boolean ok = false; 54 55 if ((dest.exists() && dest.canWrite()) 56 || (!dest.exists() && dest.getParentFile().canWrite())) 57 { 58 OutputStream fos = null; 59 InputStream fis = null; 60 try 61 { 62 fos = new FileOutputStream(dest); 63 fis = new FileInputStream(source); 64 ok = copyStream(32768,null,fis,fos,false); 65 } 66 catch (IOException ioe) 67 { 68 Log.log(Log.WARNING, IOUtilities.class, 69 "Error moving file: " + ioe + " : " + ioe.getMessage()); 70 } 71 finally 72 { 73 closeQuietly(fos); 74 closeQuietly(fis); 75 } 76 77 if(ok) 78 source.delete(); 79 } 80 return ok; 81 } 83 95 public static boolean copyStream(int bufferSize, ProgressObserver progress, 96 InputStream in, OutputStream out, boolean canStop) 97 throws IOException 98 { 99 byte[] buffer = new byte[bufferSize]; 100 int n; 101 long copied = 0L; 102 while (-1 != (n = in.read(buffer))) 103 { 104 out.write(buffer, 0, n); 105 copied += n; 106 if(progress != null) 107 progress.setValue(copied); 108 if(canStop && Thread.interrupted()) return false; 109 } 110 return true; 111 } 113 124 public static boolean copyStream(ProgressObserver progress, 125 InputStream in, OutputStream out, boolean canStop) 126 throws IOException 127 { 128 return copyStream(4096,progress, in, out, canStop); 129 } 131 137 public static void closeQuietly(InputStream in) 138 { 139 if(in != null) 140 { 141 try 142 { 143 in.close(); 144 } 145 catch (IOException e) 146 { 147 } 149 } 150 } 152 158 public static void closeQuietly(OutputStream out) 159 { 160 if(out != null) 161 { 162 try 163 { 164 out.close(); 165 } 166 catch (IOException e) 167 { 168 } 170 } 171 } 173 180 public static void closeQuietly(Reader r) 181 { 182 if(r != null) 183 { 184 try 185 { 186 r.close(); 187 } 188 catch (IOException e) 189 { 190 } 192 } 193 } 195 202 public static void closeQuietly(Closeable closeable) 203 { 204 if(closeable != null) 205 { 206 try 207 { 208 closeable.close(); 209 } 210 catch (IOException e) 211 { 212 } 214 } 215 } 216 217 private IOUtilities(){} 218 } 219 | Popular Tags |