litesoft
@ 959
litesoft / trunk / Java / ScarPlus / src / com / esotericsoftware / utils / FileUtil.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
package com.esotericsoftware.utils; import java.io.*; import java.nio.channels.*; @SuppressWarnings({"UnusedDeclaration"}) public class FileUtil extends Util { public static final File CANONICAL_USER_DIR; static { try { CANONICAL_USER_DIR = new File( System.getProperty( "user.dir" ) ).getCanonicalFile(); } catch ( IOException e ) { throw new Error( e ); } } private static final IFileSystem FILE_SYSTEM = new IFileSystem() { @Override public boolean isWindows() { return isWindows; } @Override public char separatorChar() { return File.separatorChar; } @Override public String canonicalCurrentPath() { return CANONICAL_USER_DIR.getPath(); } @Override public boolean exists( String path ) { return new File( path ).exists(); } @Override public String canonicalizeNormalizedExisting( String path ) { File zFile = new File( path ); if ( zFile.exists() ) { return getCanonicalPath( zFile ); } throw new WrappedIOException( new FileNotFoundException( path ) ); } }; public static File assertExists( String pWhat, File pToTest ) { assertNotNull( pWhat, pToTest ); if ( !pToTest.exists() ) { throw new IllegalArgumentException( pWhat + " not found: " + pToTest.getAbsolutePath() ); } return pToTest; } public static File assertIsFile( String pWhat, File pToTest ) { if ( !assertExists( pWhat, pToTest ).isFile() ) { throw new IllegalArgumentException( pWhat + " not a file: " + pToTest.getAbsolutePath() ); } return pToTest; } public static File assertIsDirectory( String pWhat, File pToTest ) { if ( !assertExists( pWhat, pToTest ).isDirectory() ) { throw new IllegalArgumentException( pWhat + " not a directory: " + pToTest.getAbsolutePath() ); } return pToTest; } /** * Reads to the end of the input stream and writes the bytes to the output stream. */ public static void copyStreamAndCloseEm( InputStream input, OutputStream output ) { try { writeStream( input, output ); } finally { dispose( input ); } } public static void writeStream( InputStream input, OutputStream output ) { try { append( input, output ); Closeable zCloseable = output; output = null; close( zCloseable ); } finally { dispose( output ); } } public static void append( InputStream input, OutputStream output ) { assertNotNull( "input", input ); assertNotNull( "output", output ); byte[] buf = new byte[1024]; try { for ( int len; (len = input.read( buf )) > -1; ) { if ( len != 0 ) { output.write( buf, 0, len ); } } } catch ( IOException e ) { throw new WrappedIOException( e ); } } /** * Copies one file to another. */ @SuppressWarnings({"ResultOfMethodCallIgnored"}) public static void copyFile( File in, File out ) { assertNotNull( "in", in ); assertNotNull( "out", out ); LOGGER.trace.log( "Copying file: ", in, " -> ", out ); FileChannel sourceChannel = createFileInputStream( in ).getChannel(); out.getParentFile().mkdirs(); try { FileChannel destinationChannel = createFileOutputStream( out ).getChannel(); try { sourceChannel.transferTo( 0, sourceChannel.size(), destinationChannel ); Closeable zCloseable = destinationChannel; destinationChannel = null; close( zCloseable ); } catch ( IOException e ) { throw new WrappedIOException( e ); } finally { dispose( destinationChannel ); } } finally { dispose( sourceChannel ); } } /** * Copies a file, overwriting any existing file at the destination. */ public static String copyFile( String in, String out ) { copyFile( new File( assertNotEmpty( "in", in ) ), new File( out = assertNotEmpty( "out", out ) ) ); return out; } /** * Moves a file, overwriting any existing file at the destination. */ static public String moveFile( String in, String out ) { copyFile( in = assertNotEmpty( "in", in ), out ); delete( in ); return out; } /** * Returns the textual contents of the specified file. */ static public String fileContents( String path ) { return fileContents( new File( path ) ); } /** * Returns the textual contents of the specified file. */ static public String fileContents( File pFile ) { StringBuilder stringBuffer = new StringBuilder( 4096 ); FileReader reader = createFileReader( pFile ); try { char[] buffer = new char[2048]; for ( int length; -1 != (length = reader.read( buffer )); ) { stringBuffer.append( buffer, 0, length ); } } catch ( IOException e ) { throw new WrappedIOException( e ); } finally { dispose( reader ); } return stringBuffer.toString(); } /** * Update the File Contents. */ static public void updateFileContents( File pFile, String pNewContents ) { FileWriter writer = createFileWriter( pFile ); try { writer.write( pNewContents ); Closeable zCloseable = writer; writer = null; close( zCloseable ); } catch ( IOException e ) { throw new WrappedIOException( e ); } finally { dispose( writer ); } } public static BufferedOutputStream createBufferedFileOutputStream( String filePath ) { return createBufferedFileOutputStream( new File( filePath ) ); } public static BufferedOutputStream createBufferedFileOutputStream( File out ) { return new BufferedOutputStream( createFileOutputStream( out ) ); } public static FileOutputStream createFileOutputStream( File out ) { mkdir( out.getParentFile() ); try { return new FileOutputStream( out ); } catch ( FileNotFoundException e ) { throw new WrappedIOException( e ); } } public static FileInputStream createFileInputStream( File in ) { try { return new FileInputStream( in ); } catch ( FileNotFoundException e ) { throw new WrappedIOException( e ); } } public static FileReader createFileReader( File in ) { try { return new FileReader( in ); } catch ( FileNotFoundException e ) { throw new WrappedIOException( e ); } } public static FileWriter createFileWriter( File out ) { try { return new FileWriter( out ); } catch ( IOException e ) { throw new WrappedIOException( e ); } } /** * Creates the directories in the specified path. */ public static File mkdir( File path ) { assertNotNull( "path", path ); if ( path.mkdirs() ) { LOGGER.trace.log( "Created directory: ", path.getPath() ); } return path; } /** * Creates the directories in the specified path. */ public static String mkdir( String path ) { mkdir( new File( path = assertNotEmpty( "path", path ) ) ); return path; } /** * Deletes a directory and all files and directories it contains. */ public static boolean delete( File pFile ) { assertNotNull( "File", pFile ); if ( pFile.isDirectory() ) { File[] zFiles = pFile.listFiles(); for ( File zFile : zFiles ) { if ( !delete( zFile ) ) { return false; } } } LOGGER.trace.log( "Deleting file: ", pFile ); return pFile.delete(); } /** * Deletes a file or directory and all files and subdirecties under it. */ public static boolean delete( String fileName ) { fileName = noEmpty( fileName ); return (fileName != null) && delete( new File( fileName ) ); } public static Closeable dispose( Closeable pCloseable ) { if ( pCloseable != null ) { try { pCloseable.close(); } catch ( IOException ignore ) { // Whatever! } pCloseable = null; } return pCloseable; } public static void close( Closeable pCloseable ) { if ( pCloseable != null ) { try { pCloseable.close(); } catch ( IOException e ) { throw new WrappedIOException( e ); } } } public static String getCanonicalPath( File pFile ) { return getCanonicalFile( pFile ).getPath(); } public static File getCanonicalFile( File pFile ) { try { return pFile.getCanonicalFile(); } catch ( IOException e ) { throw new WrappedIOException( e ); } } public static boolean isAbsolutePath( String path ) { assertNotNull( "path", path ); return FileSupport.isAbsoluteNormalizedPath( FILE_SYSTEM, CANONICAL_USER_DIR.getPath(), FileSupport.normalizePath( FILE_SYSTEM, path ) ); } public static String normalizePath( String path ) { assertNotNull( "path", path ); return FileSupport.normalizePath( FILE_SYSTEM, path ); } public static File canonicalizePath( String path ) { return canonicalizePath( CANONICAL_USER_DIR, path ); } public static File canonicalizePath( File pCanonicalParentDirIfPathRelative, String path ) { assertNotNull( "path", path ); return new File( FileSupport.canonicalizeNormalizedPath( FILE_SYSTEM, pCanonicalParentDirIfPathRelative.getPath(), FileSupport.normalizePath( FILE_SYSTEM, path ) ) ); } } |
Commits for litesoft/trunk/Java/ScarPlus/src/com/esotericsoftware/utils/FileUtil.java
Revision | Author | Commited | Message |
---|---|---|---|
959 Diff | GeorgeS | Sat 19 Jul, 2014 15:27:50 +0000 | Scar update |
360 Diff | GeorgeS | Sun 07 Aug, 2011 04:10:11 +0000 | |
346 Diff | GeorgeS | Tue 02 Aug, 2011 00:41:14 +0000 | |
343 Diff | GeorgeS | Mon 01 Aug, 2011 03:19:50 +0000 | |
320 Diff | GeorgeS | Sat 23 Jul, 2011 20:24:31 +0000 | |
319 Diff | GeorgeS | Sat 23 Jul, 2011 00:38:50 +0000 | |
317 Diff | GeorgeS | Tue 19 Jul, 2011 20:11:58 +0000 | |
315 | GeorgeS | Sun 17 Jul, 2011 15:48:36 +0000 |