Subversion Repository Public Repository

litesoft

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

Diff revisions: vs.
  @@ -1,71 +1,57 @@
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.base.*;
7 5 import org.litesoft.commonfoundation.exceptions.*;
8 6 import org.litesoft.commonfoundation.stringmatching.*;
7 + import org.litesoft.commonfoundation.typeutils.Objects;
9 8 import org.litesoft.commonfoundation.typeutils.*;
10 9
10 + import java.io.*;
11 11 import java.util.*;
12 12
13 - import org.litesoft.commonfoundation.typeutils.Objects;
14 -
15 - public class FileUtils extends FileUtil
16 - {
13 + public class FileUtils extends FileUtil {
17 14 public static Reader[] filesToReaders( File pDir, String... pFileNames )
18 - throws FileNotFoundException
19 - {
20 - if ( Objects.isNullOrEmpty( pFileNames ) )
21 - {
15 + throws FileNotFoundException {
16 + if ( Objects.isNullOrEmpty( pFileNames ) ) {
22 17 throw new IllegalArgumentException( "No Files provided" );
23 18 }
24 19 Reader[] readers = new Reader[pFileNames.length];
25 - for ( int i = 0; i < pFileNames.length; i++ )
26 - {
20 + for ( int i = 0; i < pFileNames.length; i++ ) {
27 21 File file = new File( pDir, pFileNames[i] );
28 22 readers[i] = new InformativeFileReader( file );
29 23 }
30 24 return readers;
31 25 }
32 26
33 - private static class InformativeFileReader extends FileReader implements ToStringInformative
34 - {
27 + private static class InformativeFileReader extends FileReader implements ToStringInformative {
35 28 private File mFile;
36 29
37 30 private InformativeFileReader( File pFile )
38 - throws FileNotFoundException
39 - {
31 + throws FileNotFoundException {
40 32 super( pFile );
41 33 mFile = pFile;
42 34 }
43 35
44 36 @Override
45 - public String toString()
46 - {
37 + public String toString() {
47 38 return mFile.getAbsolutePath();
48 39 }
49 40 }
50 41
51 42 public static void deleteIfExists( File pFile )
52 - throws FileSystemException
53 - {
43 + throws FileSystemException {
54 44 Objects.assertNotNull( "File", pFile );
55 - if ( pFile.isFile() )
56 - {
57 - if ( !pFile.delete() || pFile.exists() )
58 - {
45 + if ( pFile.isFile() ) {
46 + if ( !pFile.delete() || pFile.exists() ) {
59 47 throw new FileSystemException( "Unable to delete: " + pFile.getAbsolutePath() );
60 48 }
61 49 }
62 50 }
63 51
64 - public static FilenameFilter createFileNameFilter( String... pParts )
65 - {
52 + public static FilenameFilter createFileNameFilter( String... pParts ) {
66 53 StringMatcher zSM = StringMatcherFactory.createStartsWith( pParts );
67 - if ( zSM == null )
68 - {
54 + if ( zSM == null ) {
69 55 throw new IllegalArgumentException( "Must Provide something to Filter on!" );
70 56 }
71 57 return new StringMatcherFilenameFilter( zSM );
  @@ -73,19 +59,14 @@
73 59
74 60 @SuppressWarnings({"UnusedDeclaration"})
75 61 public static String locateFile( String pFile )
76 - throws FileSystemException
77 - {
78 - try
79 - {
62 + throws FileSystemException {
63 + try {
80 64 File file = new File( pFile );
81 - while ( !file.isFile() )
82 - {
65 + while ( !file.isFile() ) {
83 66 File parent = file.getParentFile();
84 - if ( parent != null )
85 - {
67 + if ( parent != null ) {
86 68 File grandparent = parent.getParentFile();
87 - if ( grandparent != null )
88 - {
69 + if ( grandparent != null ) {
89 70 file = new File( grandparent, pFile );
90 71 continue;
91 72 }
  @@ -94,109 +75,88 @@
94 75 }
95 76 return file.getAbsolutePath();
96 77 }
97 - catch ( IOException e )
98 - {
78 + catch ( IOException e ) {
99 79 throw new FileSystemException( e );
100 80 }
101 81 }
102 82
103 83 public static byte[] load( File pFile, int pMaxAllowedSize )
104 - throws FileSystemException
105 - {
84 + throws FileSystemException {
106 85 Objects.assertNotNull( "File", pFile );
107 - try
108 - {
109 - if ( !pFile.exists() )
110 - {
86 + try {
87 + if ( !pFile.exists() ) {
111 88 throw new FileNotFoundException( pFile.getAbsolutePath() );
112 89 }
113 90 long fSize = pFile.length();
114 - if ( fSize > pMaxAllowedSize )
115 - {
91 + if ( fSize > pMaxAllowedSize ) {
116 92 throw new FileSystemException( "File (" + pFile + ") Too large (" + fSize + "), but max set to: " + pMaxAllowedSize );
117 93 }
118 94 int fileSize = (int) fSize;
119 95 byte[] b = new byte[fileSize];
120 96 InputStream is = new FileInputStream( pFile );
121 97 boolean closed = false;
122 - try
123 - {
98 + try {
124 99 int offset = 0;
125 - while ( offset < fileSize )
126 - {
100 + while ( offset < fileSize ) {
127 101 int i = is.read( b, offset, fileSize - offset );
128 - if ( i < 1 )
129 - {
102 + if ( i < 1 ) {
130 103 throw new FileSystemException( "Unable to read the entire file (" + pFile + "). Expected " + fileSize + ", but only got: " + offset );
131 104 }
132 105 offset += i;
133 106 }
134 - if ( -1 != is.read() )
135 - {
107 + if ( -1 != is.read() ) {
136 108 throw new FileSystemException( "Read beyond file (" + pFile + ") size (" + fileSize + ")!" );
137 109 }
138 110 closed = true;
139 111 is.close();
140 112 }
141 - finally
142 - {
143 - if ( !closed )
144 - {
113 + finally {
114 + if ( !closed ) {
145 115 Utils.dispose( is );
146 116 }
147 117 }
148 118 return b;
149 119 }
150 - catch ( IOException e )
151 - {
120 + catch ( IOException e ) {
152 121 throw new FileSystemException( e );
153 122 }
154 123 }
155 124
156 125 public static void appendTextFile( File pFile, String... pLines )
157 - throws FileSystemException
158 - {
126 + throws FileSystemException {
159 127 Objects.assertNotNull( "File", pFile );
160 128 File file = new File( pFile.getAbsolutePath() );
161 129 addLines( file, true, pLines );
162 130 }
163 131
164 132 public static void store( File pFile, byte[] pBytes )
165 - throws FileSystemException
166 - {
133 + throws FileSystemException {
167 134 Objects.assertNotNull( "File", pFile );
168 135 File file = Directories.insureParent( new File( pFile.getAbsolutePath() + ".new" ) );
169 - try
170 - {
136 + try {
171 137 OutputStream os = new FileOutputStream( file );
172 138 boolean closed = false;
173 - try
174 - {
175 - if ( (pBytes != null) && (pBytes.length != 0) )
176 - {
139 + try {
140 + if ( (pBytes != null) && (pBytes.length != 0) ) {
177 141 os.write( pBytes );
178 142 }
179 143 closed = true;
180 144 os.close();
181 145 }
182 - finally
183 - {
184 - if ( !closed )
185 - {
146 + finally {
147 + if ( !closed ) {
186 148 Utils.dispose( os );
187 149 }
188 150 }
189 151 }
190 - catch ( IOException e )
191 - {
152 + catch ( IOException e ) {
192 153 throw new FileSystemException( e );
193 154 }
194 155 rollIn( file, pFile, new File( pFile.getAbsolutePath() + ".bak" ) );
195 156 }
196 157
197 158 public static void storeTextFile( File pFile, String... pLines )
198 - throws FileSystemException
199 - {
159 + throws FileSystemException {
200 160 Objects.assertNotNull( "File", pFile );
201 161 File file = new File( pFile.getAbsolutePath() + ".new" );
202 162 addLines( file, false, pLines );
  @@ -204,28 +164,21 @@
204 164 }
205 165
206 166 private static void addLines( File pFile, boolean pAppend, String... pLines )
207 - throws FileSystemException
208 - {
209 - try
210 - {
167 + throws FileSystemException {
168 + try {
211 169 addLines( createWriter( pFile, pAppend ), pLines );
212 170 }
213 - catch ( IOException e )
214 - {
171 + catch ( IOException e ) {
215 172 throw new FileSystemException( e );
216 173 }
217 174 }
218 175
219 176 private static void addLines( BufferedWriter pWriter, String... pLines )
220 - throws IOException
221 - {
177 + throws IOException {
222 178 boolean closed = false;
223 - try
224 - {
225 - for ( String line : Strings.deNull( pLines ) )
226 - {
227 - if ( line != null )
228 - {
179 + try {
180 + for ( String line : Strings.deNull( pLines ) ) {
181 + if ( line != null ) {
229 182 pWriter.write( line );
230 183 pWriter.write( '\n' );
231 184 }
  @@ -233,81 +186,64 @@
233 186 closed = true;
234 187 pWriter.close();
235 188 }
236 - finally
237 - {
238 - if ( !closed )
239 - {
189 + finally {
190 + if ( !closed ) {
240 191 Utils.dispose( pWriter );
241 192 }
242 193 }
243 194 }
244 195
245 196 public static String[] loadTextFile( File pFile )
246 - throws FileSystemException
247 - {
197 + throws FileSystemException {
248 198 Objects.assertNotNull( "File", pFile );
249 - try
250 - {
251 - if ( !pFile.exists() )
252 - {
199 + try {
200 + if ( !pFile.exists() ) {
253 201 throw new FileNotFoundException( pFile.getAbsolutePath() );
254 202 }
255 203 List<String> lines = new LinkedList<String>();
256 204 BufferedReader reader = createReader( pFile );
257 205 boolean closed = false;
258 - try
259 - {
260 - for ( String line; null != (line = reader.readLine()); )
261 - {
206 + try {
207 + for ( String line; null != (line = reader.readLine()); ) {
262 208 lines.add( line );
263 209 }
264 210 closed = true;
265 211 reader.close();
266 212 }
267 - finally
268 - {
269 - if ( !closed )
270 - {
213 + finally {
214 + if ( !closed ) {
271 215 Utils.dispose( reader );
272 216 }
273 217 }
274 218 return lines.toArray( new String[lines.size()] );
275 219 }
276 - catch ( IOException e )
277 - {
220 + catch ( IOException e ) {
278 221 throw new FileSystemException( e );
279 222 }
280 223 }
281 224
282 225 @SuppressWarnings({"UnusedDeclaration"})
283 226 public static String loadTextFileAsString( File pFile )
284 - throws FileSystemException
285 - {
227 + throws FileSystemException {
286 228 return loadTextFileAsString( pFile, null );
287 229 }
288 230
289 231 public static String loadTextFileAsString( File pFile, String pLineSeparator )
290 - throws FileSystemException
291 - {
232 + throws FileSystemException {
292 233 Objects.assertNotNull( "File", pFile );
293 234 pLineSeparator = Strings.deNull( pLineSeparator, "\n" );
294 - try
295 - {
296 - if ( !pFile.exists() )
297 - {
235 + try {
236 + if ( !pFile.exists() ) {
298 237 throw new FileNotFoundException( pFile.getAbsolutePath() );
299 238 }
300 239
301 240 StringBuilder buffer = new StringBuilder();
302 241 BufferedReader reader = createReader( pFile );
303 242 boolean closed = false;
304 - try
305 - {
243 + try {
306 244 int count = 0;
307 - for ( String line; null != (line = reader.readLine()); count++ )
308 - {
309 - if ( count > 0 )
310 - {
245 + for ( String line; null != (line = reader.readLine()); count++ ) {
246 + if ( count > 0 ) {
311 247 buffer.append( pLineSeparator );
312 248 }
313 249 buffer.append( line );
  @@ -315,47 +251,38 @@
315 251 closed = true;
316 252 reader.close();
317 253 }
318 - finally
319 - {
320 - if ( !closed )
321 - {
254 + finally {
255 + if ( !closed ) {
322 256 Utils.dispose( reader );
323 257 }
324 258 }
325 259
326 260 return buffer.toString();
327 261 }
328 - catch ( IOException e )
329 - {
262 + catch ( IOException e ) {
330 263 throw new FileSystemException( e );
331 264 }
332 265 }
333 266
334 267 public static void rollIn( File pNewFile, File pTargetFile, File pBackupFile )
335 - throws FileSystemException
336 - {
268 + throws FileSystemException {
337 269 Objects.assertNotNull( "NewFile", pNewFile );
338 270 Objects.assertNotNull( "TargetFile", pTargetFile );
339 271 Objects.assertNotNull( "BackupFile", pBackupFile );
340 - if ( !pNewFile.exists() )
341 - {
272 + if ( !pNewFile.exists() ) {
342 273 throw new FileSystemException( "Does not Exist: " + pNewFile.getPath() );
343 274 }
344 275 boolean targetExisted = false;
345 - if ( pTargetFile.exists() )
346 - {
276 + if ( pTargetFile.exists() ) {
347 277 targetExisted = true;
348 278 deleteIfExists( pBackupFile );
349 279 renameFromTo( pTargetFile, pBackupFile );
350 280 }
351 - try
352 - {
281 + try {
353 282 renameFromTo( pNewFile, pTargetFile );
354 283 }
355 - catch ( FileSystemException e )
356 - {
357 - if ( targetExisted )
358 - {
284 + catch ( FileSystemException e ) {
285 + if ( targetExisted ) {
359 286 attemptToRollBack( pNewFile, pTargetFile, pBackupFile );
360 287 }
361 288 throw e;
  @@ -363,10 +290,8 @@
363 290 }
364 291
365 292 @SuppressWarnings({"ResultOfMethodCallIgnored"})
366 - private static void attemptToRollBack( File pNewFile, File pTargetFile, File pBackupFile )
367 - {
368 - switch ( (pNewFile.exists() ? 0 : 4) + (pTargetFile.exists() ? 0 : 2) + (pBackupFile.exists() ? 0 : 1) )
369 - {
293 + private static void attemptToRollBack( File pNewFile, File pTargetFile, File pBackupFile ) {
294 + switch ( (pNewFile.exists() ? 0 : 4) + (pTargetFile.exists() ? 0 : 2) + (pBackupFile.exists() ? 0 : 1) ) {
370 295 // What Happen'd
371 296 case 0: // There: ------------- Nobody --------------
372 297 case 2: // There: pTargetFile
  @@ -395,17 +320,14 @@
395 320 * to mess with the "as linear" assumptions
396 321 */
397 322 private static void renameFromTo( File pSourceFile, File pDestinationFile )
398 - throws FileSystemException
399 - {
323 + throws FileSystemException {
400 324 Objects.assertNotNull( "SourceFile", pSourceFile );
401 325 Objects.assertNotNull( "DestinationFile", pDestinationFile );
402 326 // Win 1 Start
403 - if ( !pSourceFile.exists() )
404 - {
327 + if ( !pSourceFile.exists() ) {
405 328 throw new FileSystemException( "SourceFile does not exist: " + pSourceFile.getAbsolutePath() );
406 329 }
407 - if ( pDestinationFile.exists() )
408 - {
330 + if ( pDestinationFile.exists() ) {
409 331 throw new FileSystemException( "DestinationFile already exists: " + pDestinationFile.getAbsolutePath() );
410 332 }
411 333 // Win 2 Start
  @@ -416,73 +338,58 @@
416 338 boolean sThere = pSourceFile.exists();
417 339 boolean dThere = pDestinationFile.exists();
418 340 // Win 2 End
419 - if ( sThere )
420 - {
341 + if ( sThere ) {
421 342 throw renameFailed( pSourceFile, pDestinationFile, "claims Succeess, but Source still there" + (dThere ? " and so is the Destination!" : "!") );
422 343 }
423 - if ( !dThere )
424 - {
344 + if ( !dThere ) {
425 345 throw renameFailed( pSourceFile, pDestinationFile, "claims Succeess, but the Destination is NOT there!" );
426 346 }
427 347 }
428 348
429 - private static FileSystemException renameFailed( File pSourceFile, File pDestinationFile, String pAdditionalExplanation )
430 - {
431 - throw new FileSystemException( "Rename (" + pSourceFile.getAbsolutePath() + ") to (" + pDestinationFile.getAbsolutePath() + ") " + pAdditionalExplanation );
349 + private static FileSystemException renameFailed( File pSourceFile, File pDestinationFile, String pAdditionalExplanation ) {
350 + throw new FileSystemException(
351 + "Rename (" + pSourceFile.getAbsolutePath() + ") to (" + pDestinationFile.getAbsolutePath() + ") " + pAdditionalExplanation );
432 352 }
433 353
434 - private static class StringMatcherFilenameFilter implements FilenameFilter
435 - {
354 + private static class StringMatcherFilenameFilter implements FilenameFilter {
436 355 private StringMatcher mSM;
437 356
438 - public StringMatcherFilenameFilter( StringMatcher pSM )
439 - {
357 + public StringMatcherFilenameFilter( StringMatcher pSM ) {
440 358 mSM = pSM;
441 359 }
442 360
443 361 @Override
444 - public boolean accept( File dir, String name )
445 - {
362 + public boolean accept( File dir, String name ) {
446 363 return mSM.matches( name );
447 364 }
448 365 }
449 366
450 367 public static void main( String[] args )
451 - throws Exception
452 - {
368 + throws Exception {
453 369 File zFile = new File( "PersistanceTier/design/NextGEN.xml" );
454 370 String[] lines = loadTextFile( zFile );
455 371 process( lines );
456 372 storeTextFile( zFile, lines );
457 373 }
458 374
459 - private static void process( String[] pLines )
460 - {
461 - for ( int i = 0; i < pLines.length; i++ )
462 - {
375 + private static void process( String[] pLines ) {
376 + for ( int i = 0; i < pLines.length; i++ ) {
463 377 String zLine = pLines[i];
464 - if ( zLine != null )
465 - {
466 - if ( zLine.trim().startsWith( "<column " ) )
467 - {
378 + if ( zLine != null ) {
379 + if ( zLine.trim().startsWith( "<column " ) ) {
468 380 processColumn( pLines, i, findEONode( pLines, i ) );
469 - }
470 - else if ( zLine.trim().startsWith( "<foreignKey " ) )
471 - {
381 + } else if ( zLine.trim().startsWith( "<foreignKey " ) ) {
472 382 processForeignKey( pLines, i, findEONode( pLines, i ) );
473 383 }
474 384 }
475 385 }
476 386 }
477 387
478 - private static int findEONode( String[] pLines, int pFromIndex )
479 - {
388 + private static int findEONode( String[] pLines, int pFromIndex ) {
480 389 int zThruIndex = pFromIndex;
481 - while ( zThruIndex < pLines.length )
482 - {
390 + while ( zThruIndex < pLines.length ) {
483 391 String zLine = pLines[zThruIndex];
484 - if ( (zLine != null) && zLine.trim().endsWith( ">" ) )
485 - {
392 + if ( (zLine != null) && zLine.trim().endsWith( ">" ) ) {
486 393 break;
487 394 }
488 395 zThruIndex++;
  @@ -490,52 +397,41 @@
490 397 return zThruIndex;
491 398 }
492 399
493 - private static boolean isAttributeOneOf( String pLine, String... pOneOf )
494 - {
495 - for ( String s : pOneOf )
496 - {
497 - if ( pLine.startsWith( s + "=" ) )
498 - {
400 + private static boolean isAttributeOneOf( String pLine, String... pOneOf ) {
401 + for ( String s : pOneOf ) {
402 + if ( pLine.startsWith( s + "=" ) ) {
499 403 return true;
500 404 }
501 405 }
502 406 return false;
503 407 }
504 408
505 - private static void processColumn( String[] pLines, int pBegLine, int pEndLine )
506 - {
507 - for ( int i = pBegLine + 1; i < pEndLine; i++ )
508 - {
409 + private static void processColumn( String[] pLines, int pBegLine, int pEndLine ) {
410 + for ( int i = pBegLine + 1; i < pEndLine; i++ ) {
509 411 String zLine = pLines[i];
510 - if ( zLine != null )
511 - {
412 + if ( zLine != null ) {
512 413 zLine = zLine.trim();
513 414 if ( zLine.equals( "type=\"foreignKey\"" ) ) // No More Columns as ForeignKeys
514 415 {
515 - for ( int j = pBegLine; j <= pEndLine; j++ )
516 - {
416 + for ( int j = pBegLine; j <= pEndLine; j++ ) {
517 417 pLines[j] = null;
518 418 }
519 419 return;
520 420 }
521 - if ( isAttributeOneOf( zLine, "fkonly", "isContainer", "IsContainer", "ReferenceSortColumn", "ThemDependsOnUs", "UsDependsOnThem", "table", "updateReferencedPO" ) )
522 - {
421 + if ( isAttributeOneOf( zLine, "fkonly", "isContainer", "IsContainer", "ReferenceSortColumn", "ThemDependsOnUs", "UsDependsOnThem", "table",
422 + "updateReferencedPO" ) ) {
523 423 pLines[i] = null;
524 424 }
525 425 }
526 426 }
527 427 }
528 428
529 - private static void processForeignKey( String[] pLines, int pBegLine, int pEndLine )
530 - {
531 - for ( int i = pBegLine + 1; i < pEndLine; i++ )
532 - {
429 + private static void processForeignKey( String[] pLines, int pBegLine, int pEndLine ) {
430 + for ( int i = pBegLine + 1; i < pEndLine; i++ ) {
533 431 String zLine = pLines[i];
534 - if ( zLine != null )
535 - {
432 + if ( zLine != null ) {
536 433 zLine = zLine.trim();
537 - if ( isAttributeOneOf( zLine, "fkonly", "isContainer", "IsContainer", "defaultValue", "enumeration", "len" ) )
538 - {
434 + if ( isAttributeOneOf( zLine, "fkonly", "isContainer", "IsContainer", "defaultValue", "enumeration", "len" ) ) {
539 435 pLines[i] = null;
540 436 }
541 437 }