001 package org.shiftone.jrat.util.io; 002 003 004 import org.shiftone.jrat.core.JRatException; 005 import org.shiftone.jrat.util.Assert; 006 import org.shiftone.jrat.util.log.Logger; 007 008 import java.io.*; 009 import java.net.Socket; 010 011 012 /** 013 * Class IOUtil 014 * <p/> 015 * $astChangedBy$ 016 * $LastChangedDate$ 017 * $LastChangedRevision$ 018 * $HeadURL$ 019 * $Id$ 020 * <p/> 021 * todo - make sure streams get closed 022 */ 023 public class IOUtil { 024 025 private static final Logger LOG = Logger.getLogger(IOUtil.class); 026 public static final int DEFAULT_BUFFER_SIZE = 1024 * 4; 027 public static final int MAX_HEURISTIC_BUFFER_SIZE = 1024 * 16; 028 029 public static void delete(File file) { 030 031 Assert.assertNotNull("file", file); 032 LOG.debug("delete(" + file + ")"); 033 034 if (!file.delete()) { 035 if (file.exists()) { 036 throw new JRatException("unable to delete file : " + file.getAbsolutePath()); 037 } else { 038 throw new JRatException("unable to delete non-existant file : " + file.getAbsolutePath()); 039 } 040 } 041 } 042 043 public static boolean createNewFile(File file) { 044 045 try { 046 return file.createNewFile(); 047 } catch (IOException e) { 048 throw new JRatException("failed to column new file : " + file.getAbsolutePath(), e); 049 } 050 } 051 052 public static void deleteIfExists(File file) { 053 054 Assert.assertNotNull("file", file); 055 056 if (file.exists()) { 057 delete(file); 058 } 059 } 060 061 062 public static void mkdir(File dir) { 063 064 Assert.assertNotNull("dir", dir); 065 LOG.info("mkdir(" + dir.getAbsolutePath() + ")"); 066 067 if (dir.exists()) { 068 if (dir.isDirectory()) { 069 return; 070 } else { 071 throw new JRatException("unable to column directory because file with same name exists " + dir); 072 } 073 } 074 075 if (!dir.mkdirs()) { 076 throw new JRatException("unable to column directory : " + dir); 077 } 078 } 079 080 081 public static void rename(File source, File target, boolean replace) { 082 083 Assert.assertNotNull("source", source); 084 Assert.assertNotNull("target", target); 085 086 if (!source.exists()) { 087 throw new JRatException("source file does not exist : " + source); 088 } 089 090 if ((target.exists()) && (replace == true)) { 091 LOG.debug("rename.delete(" + target + ")"); 092 093 if (!target.delete()) { 094 throw new JRatException("unable to delete file : " + target.getAbsolutePath()); 095 } 096 } 097 098 LOG.debug("rename(" + source + " , " + target + ")"); 099 100 if (!source.renameTo(target)) { 101 throw new JRatException("unable to rename " + source.getAbsolutePath() + " to " 102 + target.getAbsolutePath()); 103 } 104 } 105 106 107 public static void copy(InputStream sourceStream, OutputStream targetStream, int bufferSize) { 108 109 byte[] buffer = new byte[bufferSize]; 110 int b = 0; 111 112 Assert.assertNotNull("sourceStream", sourceStream); 113 Assert.assertNotNull("targetStream", targetStream); 114 115 try { 116 for (b = 0; b >= 0; b = sourceStream.read(buffer)) { 117 if (b != 0) { 118 targetStream.write(buffer, 0, b); 119 } 120 } 121 } 122 catch (IOException e) { 123 throw new JRatException("error copying streams", e); 124 } 125 } 126 127 128 public static byte[] readAndClose(InputStream inputStream) { 129 130 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE); 131 132 try { 133 copy(inputStream, outputStream); 134 } 135 finally { 136 close(inputStream); 137 } 138 139 return outputStream.toByteArray(); 140 } 141 142 143 public static void copy(InputStream sourceStream, OutputStream targetStream) { 144 copy(sourceStream, targetStream, DEFAULT_BUFFER_SIZE); 145 } 146 147 148 public static boolean copy(File source, File target) { 149 150 Assert.assertNotNull("source", source); 151 Assert.assertNotNull("target", target); 152 LOG.debug("copy(" + source.getAbsolutePath() + " , " + target.getAbsolutePath() + ")"); 153 154 if (source.equals(target)) { 155 LOG.debug("copy doing nothing, source and target are same"); 156 157 return false; 158 } else { 159 int bufferSize = (int) Math.min(MAX_HEURISTIC_BUFFER_SIZE, source.length()); 160 InputStream inputStream = null; 161 OutputStream outputStream = null; 162 163 try { 164 inputStream = openInputStream(source, bufferSize); 165 outputStream = openOutputStream(target, bufferSize); 166 167 copy(inputStream, outputStream, bufferSize); 168 } 169 finally { 170 close(outputStream); 171 close(inputStream); 172 } 173 174 return true; 175 } 176 } 177 178 public static InputStream openInputStream(File file) { 179 return openInputStream(file, DEFAULT_BUFFER_SIZE); 180 } 181 182 public static InputStream openInputStream(File file, int bufferSize) { 183 184 LOG.debug("openInputStream " + file.getAbsolutePath()); 185 186 try { 187 InputStream inputStream = new FileInputStream(file); 188 189 if (bufferSize > 0) { 190 inputStream = new BufferedInputStream(inputStream, bufferSize); 191 } 192 193 return inputStream; 194 } 195 catch (IOException e) { 196 throw new JRatException("unable to open file for read " + file.getAbsolutePath()); 197 } 198 } 199 200 201 public static OutputStream openOutputStream(File file, int bufferSize) { 202 203 LOG.debug("openOutputStream " + file.getAbsolutePath()); 204 205 try { 206 OutputStream outputStream = new FileOutputStream(file); 207 208 if (bufferSize > 0) { 209 outputStream = new BufferedOutputStream(outputStream, bufferSize); 210 } 211 212 return outputStream; 213 } 214 catch (IOException e) { 215 throw new JRatException("unable to open file for read " + file.getAbsolutePath()); 216 } 217 } 218 219 220 public static void close(Reader reader) { 221 222 try { 223 if (reader != null) { 224 reader.close(); 225 } 226 } 227 catch (Exception e) { 228 LOG.warn("close Reader failes", e); 229 } 230 } 231 232 233 public static void close(Writer writer) { 234 235 try { 236 if (writer != null) { 237 LOG.debug("close " + writer); 238 writer.close(); 239 } 240 } 241 catch (Exception e) { 242 LOG.warn("close Writer failes", e); 243 } 244 } 245 246 247 public static void close(Socket socket) { 248 249 try { 250 if (socket != null) { 251 socket.close(); 252 } 253 } 254 catch (Exception e) { 255 LOG.warn("close Socket failes", e); 256 } 257 } 258 259 260 public static void close(InputStream inputStream) { 261 262 try { 263 if (inputStream != null) { 264 inputStream.close(); 265 } 266 } 267 catch (Exception e) { 268 LOG.warn("close InputStream failes", e); 269 } 270 } 271 272 273 public static void close(OutputStream outputStream) { 274 275 try { 276 if (outputStream != null) { 277 outputStream.close(); 278 } 279 } 280 catch (Exception e) { 281 LOG.warn("close OutputStream failes", e); 282 } 283 } 284 285 286 public static void flush(OutputStream outputStream) { 287 288 try { 289 if (outputStream != null) { 290 outputStream.flush(); 291 } 292 } 293 catch (Exception e) { 294 LOG.warn("flush OutputStream failes", e); 295 } 296 } 297 298 299 public static String getExtention(String fileName) { 300 301 Assert.assertNotNull("fileName", fileName); 302 303 int lastDot = fileName.lastIndexOf('.'); 304 305 return (lastDot == -1) || (lastDot == fileName.length() - 1) 306 ? null 307 : fileName.substring(lastDot + 1); 308 } 309 310 311 public static String getExtention(File file) { 312 313 Assert.assertNotNull("file", file); 314 315 return getExtention(file.getName()); 316 } 317 318 319 public static File getNearestExistingParent(File file) { 320 321 Assert.assertNotNull("file", file); 322 323 File p = file.getParentFile(); 324 325 while ((p != null) && (p.exists() == false)) { 326 p = p.getParentFile(); 327 } 328 329 return p; 330 } 331 }