KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > PreservingFileWriter


1 package antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/dev/klaren.dev/antlr/PreservingFileWriter.java $
8  * @author Ric Klaren <klaren@cs.utwente.nl>
9  */

10
11 import java.io.*;
12
13 /** PreservingFileWriter only overwrites target if the new file is different.
14  Mainly added in order to prevent big and unnecessary recompiles in C++
15  projects.
16  I/O is buffered.
17 */

18 public class PreservingFileWriter extends FileWriter {
19     protected File target_file; /// the file we intend to write to
20
protected File tmp_file; /// the tmp file we create at first
21

22     public PreservingFileWriter(String JavaDoc file) throws IOException
23     {
24         super(file+".antlr.tmp");
25
26         // set up File thingy for target..
27
target_file = new File(file);
28
29         File parentdir = target_file.getParentFile();
30         if( parentdir != null )
31         {
32             if (!parentdir.exists())
33                 throw new IOException("destination directory of '"+file+"' doesn't exist");
34             if (!parentdir.canWrite())
35                 throw new IOException("destination directory of '"+file+"' isn't writeable");
36         }
37         if( target_file.exists() && ! target_file.canWrite() )
38             throw new IOException("cannot write to '"+file+"'");
39
40         // and for the temp file
41
tmp_file = new File(file+".antlr.tmp");
42         // have it nuked at exit
43
tmp_file.deleteOnExit();
44     }
45
46     /** Close the file and see if the actual target is different
47      * if so the target file is overwritten by the copy. If not we do nothing
48      */

49     public void close() throws IOException
50     {
51         Reader source = null;
52         Writer target = null;
53
54         try {
55             // close the tmp file so we can access it safely...
56
super.close();
57
58             char[] buffer = new char[1024];
59             int cnt;
60
61             // target_file != tmp_file so we have to compare and move it..
62
if( target_file.length() == tmp_file.length() )
63             {
64                 // Do expensive read'n'compare
65
Reader tmp;
66                 char[] buf2 = new char[1024];
67
68                 source = new BufferedReader(new FileReader(tmp_file));
69                 tmp = new BufferedReader(new FileReader(target_file));
70                 int cnt1, cnt2;
71                 boolean equal = true;
72
73                 while( equal )
74                 {
75                     cnt1 = source.read(buffer,0,1024);
76                     cnt2 = tmp.read(buf2,0,1024);
77                     if( cnt1 != cnt2 )
78                     {
79                         equal = false;
80                         break;
81                     }
82                     if( cnt1 == -1 ) // EOF
83
break;
84                     for( int i = 0; i < cnt1; i++ )
85                     {
86                         if( buffer[i] != buf2[i] )
87                         {
88                             equal = false;
89                             break;
90                         }
91                     }
92                 }
93                 // clean up...
94
source.close();
95                 tmp.close();
96                 source = tmp = null;
97
98                 if( equal )
99                     return;
100             }
101
102             source = new BufferedReader(new FileReader(tmp_file));
103             target = new BufferedWriter(new FileWriter(target_file));
104
105             while(true)
106             {
107                 cnt = source.read(buffer,0,1024);
108                 if( cnt == -1 )
109                     break;
110                 target.write(buffer, 0, cnt );
111             }
112         }
113         finally {
114             if( source != null )
115             {
116                 try { source.close(); }
117                 catch( IOException e ) { ; }
118             }
119             if( target != null )
120             {
121                 try { target.close(); }
122                 catch( IOException e ) { ; }
123             }
124         }
125     }
126 }
127
Popular Tags