Showing the changes to java/tools/javal/FileMerge.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,108 @@ | |||
| 1 | package javal; | ||
| 2 | |||
| 3 | import java.io.File; | ||
| 4 | import java.io.FileOutputStream; | ||
| 5 | import java.io.FileInputStream; | ||
| 6 | import java.io.DataInputStream; | ||
| 7 | import java.io.DataOutputStream; | ||
| 8 | import java.io.IOException; | ||
| 9 | import java.io.EOFException; | ||
| 10 | |||
| 11 | |||
| 12 | class FileMerge | ||
| 13 | { | ||
| 14 | /** | ||
| 15 | * open the two files and merge. The "tmpname" | ||
| 16 | * is the newer generated file, name is the older | ||
| 17 | * file and will become the merged file | ||
| 18 | */ | ||
| 19 | static void Merge( String name, String tmpname ) throws IOException | ||
| 20 | { | ||
| 21 | // rename 'name' | ||
| 22 | String oldname = "old_"+ name; | ||
| 23 | File old_f = new File(oldname); | ||
| 24 | if ( old_f.exists() ) | ||
| 25 | old_f.delete(); | ||
| 26 | new File( name ).renameTo( old_f ); | ||
| 27 | |||
| 28 | // open output for merged file | ||
| 29 | DataOutputStream merge; | ||
| 30 | DataInputStream ftmp, fold; | ||
| 31 | |||
| 32 | merge = new DataOutputStream(new FileOutputStream( name ) ); | ||
| 33 | ftmp = new DataInputStream( new FileInputStream( tmpname ) ); | ||
| 34 | fold = new DataInputStream( new FileInputStream( oldname ) ); | ||
| 35 | |||
| 36 | while ( true ) | ||
| 37 | { | ||
| 38 | try | ||
| 39 | { | ||
| 40 | ReadNWrite( ftmp, merge, "#>>" ); | ||
| 41 | Ignore( ftmp, "#<<" ); | ||
| 42 | } | ||
| 43 | catch ( EOFException ee ) | ||
| 44 | { | ||
| 45 | WriteRest( fold, merge ); | ||
| 46 | break; | ||
| 47 | } | ||
| 48 | try | ||
| 49 | { | ||
| 50 | Ignore( fold, "#>>" ); | ||
| 51 | ReadNWrite( fold, merge, "#<<" ); | ||
| 52 | } | ||
| 53 | catch ( EOFException ee ) | ||
| 54 | { | ||
| 55 | WriteRest( ftmp, merge ); | ||
| 56 | break; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | fold.close(); | ||
| 61 | ftmp.close(); | ||
| 62 | merge.close(); | ||
| 63 | new File( tmpname ).delete(); | ||
| 64 | |||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | private static void ReadNWrite( DataInputStream fin, DataOutputStream fout, String pat ) throws IOException | ||
| 69 | { | ||
| 70 | String line; | ||
| 71 | |||
| 72 | do | ||
| 73 | { | ||
| 74 | line = fin.readLine(); | ||
| 75 | if ( line == null ) throw new EOFException(); | ||
| 76 | fout.writeBytes( line ); | ||
| 77 | fout.writeBytes( linesep ); | ||
| 78 | } | ||
| 79 | while ( line.indexOf(pat) == -1 ); | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | private static void Ignore( DataInputStream fin, String pat ) throws IOException | ||
| 84 | { | ||
| 85 | String line; | ||
| 86 | do | ||
| 87 | { | ||
| 88 | line = fin.readLine(); | ||
| 89 | if ( line == null ) throw new EOFException(); | ||
| 90 | } | ||
| 91 | while ( line.indexOf(pat) == -1 ); | ||
| 92 | } | ||
| 93 | |||
| 94 | private static void WriteRest( DataInputStream fin, DataOutputStream fout ) throws IOException | ||
| 95 | { | ||
| 96 | while ( true ) | ||
| 97 | { | ||
| 98 | String line = fin.readLine(); | ||
| 99 | if (line == null) | ||
| 100 | return; | ||
| 101 | fout.writeBytes(line); | ||
| 102 | fout.writeBytes( linesep ); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | private static String linesep = System.getProperties().getProperty("line.separator"); | ||
| 107 | |||
| 108 | } | ||