Subversion Repository Public Repository

litesoft

Diff Revisions 947 vs 948 for /trunk/Java/core/Server/src/org/litesoft/util/Directories.java

Diff revisions: vs.
  @@ -1,67 +1,51 @@
1 1 // This Source Code is in the Public Domain per: http://unlicense.org
2 2 package org.litesoft.util;
3 3
4 - import java.io.*;
5 -
6 4 import org.litesoft.commonfoundation.exceptions.*;
5 + import org.litesoft.commonfoundation.typeutils.Objects;
7 6 import org.litesoft.commonfoundation.typeutils.*;
8 7
8 + import java.io.*;
9 9 import java.util.*;
10 10
11 - import org.litesoft.commonfoundation.typeutils.Objects;
12 -
13 11 @SuppressWarnings("UnusedDeclaration")
14 - public class Directories
15 - {
16 - public static String currentWorking()
17 - {
12 + public class Directories {
13 + public static String currentWorking() {
18 14 return System.getProperty( "user.dir" );
19 15 }
20 16
21 - public static String temp()
22 - {
17 + public static String temp() {
23 18 return System.getProperty( "java.io.tmpdir" );
24 19 }
25 20
26 - public static File findFromCurrent( String pSubPath )
27 - {
21 + public static File findFromCurrent( String pSubPath ) {
28 22 return find( currentWorking(), pSubPath );
29 23 }
30 24
31 - public static File find( String pStartingPath, String pSubPath )
32 - {
25 + public static File find( String pStartingPath, String pSubPath ) {
33 26 Objects.assertNotNull( "StartingPath", pStartingPath );
34 27 pSubPath = Strings.assertNotNullNotEmpty( "SubPath", pSubPath );
35 28
36 29 File target = locate( pStartingPath, pSubPath, Paths.justTheLastName( pSubPath ) + ".path" );
37 - if ( target != null )
38 - {
30 + if ( target != null ) {
39 31 return target;
40 32 }
41 33 throw new IllegalStateException( "Unable to locate Directory: " + pSubPath );
42 34 }
43 35
44 - private static File locate( String pStartingPath, String pSubPath, String pRedirectorName )
45 - {
36 + private static File locate( String pStartingPath, String pSubPath, String pRedirectorName ) {
46 37 File base = new File( pStartingPath );
47 - do
48 - {
38 + do {
49 39 File target = new File( base, pSubPath );
50 - if ( target.exists() )
51 - {
40 + if ( target.exists() ) {
52 41 return target;
53 42 }
54 - if ( pRedirectorName != null )
55 - {
43 + if ( pRedirectorName != null ) {
56 44 target = new File( base, pRedirectorName );
57 - if ( target.isFile() )
58 - {
59 - for ( String line : FileUtils.loadTextFile( target ) )
60 - {
61 - if ( Strings.isNotNullOrEmpty( line ) )
62 - {
63 - if ( null != (target = locate( line, pSubPath, null )) )
64 - {
45 + if ( target.isFile() ) {
46 + for ( String line : FileUtils.loadTextFile( target ) ) {
47 + if ( Strings.isNotNullOrEmpty( line ) ) {
48 + if ( null != (target = locate( line, pSubPath, null )) ) {
65 49 return target;
66 50 }
67 51 }
  @@ -75,65 +59,52 @@
75 59 }
76 60
77 61 public static File assertDirectory( File pExpectedDir )
78 - throws FileSystemException
79 - {
62 + throws FileSystemException {
80 63 Objects.assertNotNull( "ExpectedDir", pExpectedDir );
81 - if ( !pExpectedDir.isDirectory() )
82 - {
64 + if ( !pExpectedDir.isDirectory() ) {
83 65 throw new FileSystemException( "Not a Directory: " + pExpectedDir.getAbsolutePath() );
84 66 }
85 67 return pExpectedDir;
86 68 }
87 69
88 70 public static void checkDirectoryable( File pExpectedDir )
89 - throws FileSystemException
90 - {
71 + throws FileSystemException {
91 72 Objects.assertNotNull( "ExpectedDir", pExpectedDir );
92 - if ( !pExpectedDir.isDirectory() && pExpectedDir.exists() )
93 - {
73 + if ( !pExpectedDir.isDirectory() && pExpectedDir.exists() ) {
94 74 throw new FileSystemException( "Exists, but is Not a Directory: " + pExpectedDir.getAbsolutePath() );
95 75 }
96 76 }
97 77
98 78 public static File insureParent( File pExpectedFile )
99 - throws FileSystemException
100 - {
79 + throws FileSystemException {
101 80 Objects.assertNotNull( "ExpectedFile", pExpectedFile );
102 81 File zParentFile = pExpectedFile.getParentFile();
103 - if ( zParentFile != null )
104 - {
82 + if ( zParentFile != null ) {
105 83 insure( zParentFile );
106 84 }
107 85 return pExpectedFile;
108 86 }
109 87
110 88 public static File insure( File pExpectedDir )
111 - throws FileSystemException
112 - {
89 + throws FileSystemException {
113 90 Objects.assertNotNull( "ExpectedDir", pExpectedDir );
114 - if ( !pExpectedDir.isDirectory() )
115 - {
116 - if ( pExpectedDir.exists() )
117 - {
91 + if ( !pExpectedDir.isDirectory() ) {
92 + if ( pExpectedDir.exists() ) {
118 93 throw new FileSystemException( "Exists, but is Not a Directory: " + pExpectedDir.getAbsolutePath() );
119 94 }
120 - if ( !pExpectedDir.mkdirs() || !pExpectedDir.isDirectory() )
121 - {
95 + if ( !pExpectedDir.mkdirs() || !pExpectedDir.isDirectory() ) {
122 96 throw new FileSystemException( "Unable to create Directory: " + pExpectedDir.getAbsolutePath() );
123 97 }
124 98 }
125 99 return pExpectedDir;
126 100 }
127 101
128 - public static void moveAllFiles( String pFromPath, String pToPath )
129 - {
102 + public static void moveAllFiles( String pFromPath, String pToPath ) {
130 103 File zFrom = assertDirectory( new File( pFromPath ) );
131 104 File zTo = insure( new File( pToPath ) );
132 105 File[] zFiles = zFrom.listFiles();
133 - if ( zFiles != null )
134 - {
135 - for ( File zFile : zFiles )
136 - {
106 + if ( zFiles != null ) {
107 + for ( File zFile : zFiles ) {
137 108 String zName = zFile.getName();
138 109 moveFile( zFile, new File( zTo, zName ) );
139 110 }
  @@ -141,41 +112,32 @@
141 112 }
142 113
143 114 public static void moveFile( File pSourceFile, File pTargetFile )
144 - throws FileSystemException
145 - {
115 + throws FileSystemException {
146 116 Objects.assertNotNull( "SourceFile", pSourceFile );
147 117 Objects.assertNotNull( "TargetFile", pTargetFile );
148 118 Throwable zProblem = null;
149 - try
150 - {
151 - if ( pSourceFile.exists() )
152 - {
119 + try {
120 + if ( pSourceFile.exists() ) {
153 121 insureParent( pTargetFile );
154 122 }
155 - if ( pSourceFile.renameTo( pTargetFile ) )
156 - {
157 - if ( pTargetFile.exists() )
158 - {
123 + if ( pSourceFile.renameTo( pTargetFile ) ) {
124 + if ( pTargetFile.exists() ) {
159 125 return;
160 126 }
161 127 }
162 128 }
163 - catch ( RuntimeException e )
164 - {
129 + catch ( RuntimeException e ) {
165 130 zProblem = e;
166 131 }
167 132 throw new FileSystemException( "Failed to Move (rename): " + pSourceFile + " to " + pTargetFile, zProblem );
168 133 }
169 134
170 135 public static void deleteIfExists( File pDirectory )
171 - throws FileSystemException
172 - {
136 + throws FileSystemException {
173 137 Objects.assertNotNull( "Directory", pDirectory );
174 - if ( pDirectory.isDirectory() )
175 - {
138 + if ( pDirectory.isDirectory() ) {
176 139 LowLevelDeleteAllEntries( pDirectory );
177 - if ( !pDirectory.delete() || pDirectory.exists() )
178 - {
140 + if ( !pDirectory.delete() || pDirectory.exists() ) {
179 141 throw new FileSystemException( "Unable to delete: " + pDirectory.getAbsolutePath() );
180 142 }
181 143 }
  @@ -183,28 +145,21 @@
183 145
184 146 @SuppressWarnings({"UnusedDeclaration"})
185 147 public static void deleteAllEntries( File pDirectory )
186 - throws FileSystemException
187 - {
148 + throws FileSystemException {
188 149 Objects.assertNotNull( "Directory", pDirectory );
189 - if ( pDirectory.isDirectory() )
190 - {
150 + if ( pDirectory.isDirectory() ) {
191 151 LowLevelDeleteAllEntries( pDirectory );
192 152 }
193 153 }
194 154
195 155 private static void LowLevelDeleteAllEntries( File pDirectory )
196 - throws FileSystemException
197 - {
156 + throws FileSystemException {
198 157 String[] files = pDirectory.list();
199 - for ( String file : files )
200 - {
158 + for ( String file : files ) {
201 159 File entry = new File( pDirectory, file );
202 - if ( entry.isDirectory() )
203 - {
160 + if ( entry.isDirectory() ) {
204 161 deleteIfExists( entry );
205 - }
206 - else
207 - {
162 + } else {
208 163 FileUtils.deleteIfExists( entry );
209 164 }
210 165 }
  @@ -217,16 +172,13 @@
217 172 * @return error if not null or empty
218 173 */
219 174 @SuppressWarnings({"UnusedDeclaration"})
220 - public static String compare( String pExpectedDirectory, String pActualDirectory, String... pSkipEntries )
221 - {
175 + public static String compare( String pExpectedDirectory, String pActualDirectory, String... pSkipEntries ) {
222 176 File expected = new File( pExpectedDirectory );
223 177 File actual = new File( pActualDirectory );
224 - if ( !expected.isDirectory() )
225 - {
178 + if ( !expected.isDirectory() ) {
226 179 return "'" + expected.getAbsolutePath() + "' Not a Directory";
227 180 }
228 - if ( !actual.isDirectory() )
229 - {
181 + if ( !actual.isDirectory() ) {
230 182 return "'" + actual.getAbsolutePath() + "' Not a Directory";
231 183 }
232 184
  @@ -235,13 +187,11 @@
235 187 String[] entriesActual = actual.list( filter );
236 188 Arrays.sort( entriesExpected );
237 189 Arrays.sort( entriesActual );
238 - if ( entriesActual.length == 0 )
239 - {
190 + if ( entriesActual.length == 0 ) {
240 191 return (entriesExpected.length == 0) ? null : // No Error
241 192 "Missing: " + addEntries( entriesExpected, 0 ).substring( 1 );
242 193 }
243 - if ( entriesExpected.length == 0 )
244 - {
194 + if ( entriesExpected.length == 0 ) {
245 195 return "Unexpected: " + addEntries( entriesActual, 0 ).substring( 1 );
246 196 }
247 197 List<FileMatcher> matchers = new ArrayList<FileMatcher>();
  @@ -249,119 +199,92 @@
249 199 StringBuilder missing = new StringBuilder();
250 200 int atActual = 0;
251 201 int atExpected = 0;
252 - while ( (atActual < entriesActual.length) && (atExpected < entriesExpected.length) )
253 - {
202 + while ( (atActual < entriesActual.length) && (atExpected < entriesExpected.length) ) {
254 203 int cmp = entriesActual[atActual].compareTo( entriesExpected[atExpected] );
255 - if ( cmp == 0 )
256 - {
204 + if ( cmp == 0 ) {
257 205 // Equal
258 206 matchers.add( new FileMatcher( pExpectedDirectory, pActualDirectory, entriesExpected[atExpected++] ) );
259 207 atActual++;
260 - }
261 - else if ( cmp > 0 )
262 - {
208 + } else if ( cmp > 0 ) {
263 209 // Actual > Expected
264 210 add( missing, entriesExpected[atExpected++] );
265 - }
266 - else
267 - {
211 + } else {
268 212 // Actual < Expected
269 213 add( unexpected, entriesActual[atActual++] );
270 214 }
271 215 }
272 - while ( atActual < entriesActual.length )
273 - {
216 + while ( atActual < entriesActual.length ) {
274 217 add( unexpected, entriesActual[atActual++] );
275 218 }
276 - while ( atExpected < entriesExpected.length )
277 - {
219 + while ( atExpected < entriesExpected.length ) {
278 220 add( missing, entriesExpected[atExpected++] );
279 221 }
280 222 StringBuilder issues = new StringBuilder();
281 - if ( unexpected.length() != 0 )
282 - {
223 + if ( unexpected.length() != 0 ) {
283 224 issues.append( "<br>" ).append( "Unexpected: " ).append( unexpected.toString().substring( 1 ) );
284 225 }
285 - if ( missing.length() != 0 )
286 - {
226 + if ( missing.length() != 0 ) {
287 227 issues.append( "<br>" ).append( "Missing: " ).append( missing.toString().substring( 1 ) );
288 228 }
289 - if ( issues.length() != 0 )
290 - {
229 + if ( issues.length() != 0 ) {
291 230 return issues.toString();
292 231 }
293 - for ( FileMatcher matcher : matchers )
294 - {
232 + for ( FileMatcher matcher : matchers ) {
295 233 matcher.checkLengths( issues );
296 234 }
297 - if ( issues.length() != 0 )
298 - {
235 + if ( issues.length() != 0 ) {
299 236 return issues.toString();
300 237 }
301 - for ( FileMatcher matcher : matchers )
302 - {
238 + for ( FileMatcher matcher : matchers ) {
303 239 matcher.checkContents( issues );
304 240 }
305 - if ( issues.length() != 0 )
306 - {
241 + if ( issues.length() != 0 ) {
307 242 return issues.toString();
308 243 }
309 244 return null; // No error
310 245 }
311 246
312 - private static String addEntries( String[] pEntries, int pAt )
313 - {
247 + private static String addEntries( String[] pEntries, int pAt ) {
314 248 StringBuilder sb = new StringBuilder();
315 - while ( pAt < pEntries.length )
316 - {
249 + while ( pAt < pEntries.length ) {
317 250 add( sb, pEntries[pAt++] );
318 251 }
319 252 return sb.toString();
320 253 }
321 254
322 - private static void add( StringBuilder pSb, String pEntry )
323 - {
255 + private static void add( StringBuilder pSb, String pEntry ) {
324 256 pSb.append( ", " ).append( pEntry );
325 257 }
326 258
327 - public static class DirsOnlyFilter implements FilenameFilter
328 - {
259 + public static class DirsOnlyFilter implements FilenameFilter {
329 260 public static final FilenameFilter INSTANCE = new DirsOnlyFilter();
330 261
331 262 @Override
332 - public boolean accept( File dir, String name )
333 - {
263 + public boolean accept( File dir, String name ) {
334 264 return new File( dir, name ).isDirectory();
335 265 }
336 266 }
337 267
338 - public static class DirsOnlyFileFilter implements FileFilter
339 - {
268 + public static class DirsOnlyFileFilter implements FileFilter {
340 269 public static final FileFilter INSTANCE = new DirsOnlyFileFilter();
341 270
342 271 @Override
343 - public boolean accept( File pathname )
344 - {
272 + public boolean accept( File pathname ) {
345 273 return pathname.isDirectory();
346 274 }
347 275 }
348 276
349 - public static class AllButFilter implements FilenameFilter
350 - {
277 + public static class AllButFilter implements FilenameFilter {
351 278 private String[] mButs;
352 279
353 - public AllButFilter( String... pButs )
354 - {
280 + public AllButFilter( String... pButs ) {
355 281 mButs = pButs;
356 282 }
357 283
358 284 @Override
359 - public boolean accept( File dir, String name )
360 - {
361 - for ( String but : mButs )
362 - {
363 - if ( name.equals( but ) )
364 - {
285 + public boolean accept( File dir, String name ) {
286 + for ( String but : mButs ) {
287 + if ( name.equals( but ) ) {
365 288 return false;
366 289 }
367 290 }
  @@ -369,110 +292,89 @@
369 292 }
370 293 }
371 294
372 - public static class FileMatcher
373 - {
295 + public static class FileMatcher {
374 296 private File mExpected, mActual;
375 297 private String mName;
376 298
377 - public FileMatcher( String pExpectedDirectory, String pActualDirectory, String pName )
378 - {
299 + public FileMatcher( String pExpectedDirectory, String pActualDirectory, String pName ) {
379 300 mName = pName;
380 301 mExpected = new File( pExpectedDirectory, pName );
381 302 mActual = new File( pActualDirectory, pName );
382 303 }
383 304
384 - public void checkLengths( StringBuilder pIssues )
385 - {
305 + public void checkLengths( StringBuilder pIssues ) {
386 306 long expected = mExpected.length();
387 307 long actual = mActual.length();
388 308
389 - if ( actual != expected )
390 - {
309 + if ( actual != expected ) {
391 310 pIssues.append( "<br>Files '" ).append( mName ).append( "' Lengths MisMatch " ).append( "Actual(" ).append( actual ).append( ") != Expected(" )
392 311 .append( expected ).append( ")" );
393 312 }
394 313 }
395 314
396 - public void checkContents( StringBuilder pIssues )
397 - {
315 + public void checkContents( StringBuilder pIssues ) {
398 316 StreamBlockReader eSBR = new StreamBlockReader();
399 317 StreamBlockReader aSBR = new StreamBlockReader();
400 - try
401 - {
318 + try {
402 319 eSBR.initialize( new FileInputStream( mExpected ) );
403 320 aSBR.initialize( new FileInputStream( mActual ) );
404 321 eSBR.readNextBlock();
405 322 aSBR.readNextBlock();
406 - do
407 - {
408 - if ( !eSBR.blocksEqual( aSBR ) )
409 - {
323 + do {
324 + if ( !eSBR.blocksEqual( aSBR ) ) {
410 325 pIssues.append( "<br>Files '" ).append( mName ).append( "' Contents Mismatch" );
411 326 return;
412 327 }
413 328 }
414 329 while ( 0 != (eSBR.readNextBlock() + aSBR.readNextBlock()) );
415 330 }
416 - catch ( IOException e )
417 - {
331 + catch ( IOException e ) {
418 332 pIssues.append( "<br>Files '" ).append( mName ).append( "' IOException: " ).append( e.getMessage() );
419 333 }
420 - finally
421 - {
334 + finally {
422 335 eSBR.dispose();
423 336 aSBR.dispose();
424 337 }
425 338 }
426 339 }
427 340
428 - public static class StreamBlockReader
429 - {
341 + public static class StreamBlockReader {
430 342 private InputStream mInputStream = null;
431 343 private byte[] mBuf = new byte[16 * 1024];
432 344 private int mCount = 0;
433 345
434 - public void initialize( InputStream pInputStream )
435 - {
346 + public void initialize( InputStream pInputStream ) {
436 347 mInputStream = pInputStream;
437 348 mCount = 0;
438 349 }
439 350
440 351 public int readNextBlock()
441 - throws IOException
442 - {
352 + throws IOException {
443 353 mCount = 0;
444 354 int bytes;
445 - while ( 0 < (bytes = mInputStream.read( mBuf, mCount, mBuf.length - mCount )) )
446 - {
355 + while ( 0 < (bytes = mInputStream.read( mBuf, mCount, mBuf.length - mCount )) ) {
447 356 mCount += bytes;
448 - if ( mCount == mBuf.length )
449 - {
357 + if ( mCount == mBuf.length ) {
450 358 break;
451 359 }
452 360 }
453 361 return mCount;
454 362 }
455 363
456 - public boolean blocksEqual( StreamBlockReader pThem )
457 - {
458 - if ( this.mCount != pThem.mCount )
459 - {
364 + public boolean blocksEqual( StreamBlockReader pThem ) {
365 + if ( this.mCount != pThem.mCount ) {
460 366 return false;
461 367 }
462 - for ( int i = 0; i < mCount; i++ )
463 - {
464 - if ( this.mBuf[i] != pThem.mBuf[i] )
465 - {
368 + for ( int i = 0; i < mCount; i++ ) {
369 + if ( this.mBuf[i] != pThem.mBuf[i] ) {
466 370 return false;
467 371 }
468 372 }
469 373 return true;
470 374 }
471 375
472 - public void dispose()
473 - {
474 - if ( mInputStream != null )
475 - {
376 + public void dispose() {
377 + if ( mInputStream != null ) {
476 378 Utils.dispose( mInputStream );
477 379 mInputStream = null;
478 380 }
  @@ -480,38 +382,31 @@
480 382 }
481 383
482 384 public static void main( String[] args )
483 - throws Exception
484 - {
385 + throws Exception {
485 386 DirectoriesDepthFirst.Visitor zVisitor = new MyVisitor( FileUtils.createFileNameFilter( Strings.parseChar( args[0], '*' ) ) );
486 387
487 388 new DirectoriesDepthFirst( zVisitor ).process( new File( currentWorking() ) );
488 389 }
489 390
490 - private static class MyVisitor implements DirectoriesDepthFirst.Visitor
491 - {
391 + private static class MyVisitor implements DirectoriesDepthFirst.Visitor {
492 392 private FilenameFilter mFilter;
493 393
494 - public MyVisitor( FilenameFilter pFilter )
495 - {
394 + public MyVisitor( FilenameFilter pFilter ) {
496 395 mFilter = pFilter;
497 396 }
498 397
499 398 @Override
500 399 public void process( File pDirectory )
501 - throws IOException
502 - {
400 + throws IOException {
503 401 File[] zFiles = pDirectory.listFiles( mFilter );
504 - if ( Objects.isNotNullOrEmpty( zFiles ) )
505 - {
402 + if ( Objects.isNotNullOrEmpty( zFiles ) ) {
506 403 System.out.println( pDirectory );
507 - for ( File zFile : zFiles )
508 - {
404 + for ( File zFile : zFiles ) {
509 405 boolean zDeleted = zFile.delete();
510 406 System.out.println( " " + (zDeleted ? " " : "!") + " " + zFile.getName() );
511 407 }
512 408 String[] zFileNames = pDirectory.list();
513 - if ( Objects.isNullOrEmpty( zFileNames ) )
514 - {
409 + if ( Objects.isNullOrEmpty( zFileNames ) ) {
515 410 boolean zDeleted = pDirectory.delete();
516 411 System.out.println( " " + (zDeleted ? "x!" : "!x") );
517 412 }