Source Code Cross Referenced for PreservingFileWriter.java in  » IDE-Netbeans » cnd » antlr » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » cnd » antlr 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package antlr;
002:
003:        /* ANTLR Translator Generator
004:         * Project led by Terence Parr at http://www.cs.usfca.edu
005:         * Software rights: http://www.antlr.org/license.html
006:         * @author Ric Klaren <klaren@cs.utwente.nl>
007:         */
008:
009:        import java.io.*;
010:
011:        /** PreservingFileWriter only overwrites target if the new file is different.
012:         Mainly added in order to prevent big and unnecessary recompiles in C++
013:         projects.
014:         I/O is buffered.
015:         */
016:        public class PreservingFileWriter extends FileWriter {
017:            protected File target_file; /// the file we intend to write to
018:            protected File tmp_file; /// the tmp file we create at first
019:
020:            public PreservingFileWriter(String file) throws IOException {
021:                super (file + ".antlr.tmp");
022:
023:                // set up File thingy for target..
024:                target_file = new File(file);
025:
026:                String parentdirname = target_file.getParent();
027:                if (parentdirname != null) {
028:                    File parentdir = new File(parentdirname);
029:
030:                    if (!parentdir.exists())
031:                        throw new IOException("destination directory of '"
032:                                + file + "' doesn't exist");
033:                    if (!parentdir.canWrite())
034:                        throw new IOException("destination directory of '"
035:                                + file + "' isn't writeable");
036:                }
037:                if (target_file.exists() && !target_file.canWrite())
038:                    throw new IOException("cannot write to '" + file + "'");
039:
040:                // and for the temp file
041:                tmp_file = new File(file + ".antlr.tmp");
042:                // have it nuked at exit
043:                // RK: this is broken on java 1.4 and
044:                // is not compatible with java 1.1 (which is a big problem I'm told :) )
045:                // sigh. Any real language would do this in a destructor ;) ;)
046:                // tmp_file.deleteOnExit();
047:            }
048:
049:            /** Close the file and see if the actual target is different
050:             * if so the target file is overwritten by the copy. If not we do nothing
051:             */
052:            public void close() throws IOException {
053:                Reader source = null;
054:                Writer target = null;
055:
056:                try {
057:                    // close the tmp file so we can access it safely...
058:                    super .close();
059:
060:                    char[] buffer = new char[1024];
061:                    int cnt;
062:
063:                    // target_file != tmp_file so we have to compare and move it..
064:                    if (target_file.length() == tmp_file.length()) {
065:                        // Do expensive read'n'compare
066:                        Reader tmp;
067:                        char[] buf2 = new char[1024];
068:
069:                        source = new BufferedReader(new FileReader(tmp_file));
070:                        tmp = new BufferedReader(new FileReader(target_file));
071:                        int cnt1, cnt2;
072:                        boolean equal = true;
073:
074:                        while (equal) {
075:                            cnt1 = source.read(buffer, 0, 1024);
076:                            cnt2 = tmp.read(buf2, 0, 1024);
077:                            if (cnt1 != cnt2) {
078:                                equal = false;
079:                                break;
080:                            }
081:                            if (cnt1 == -1) // EOF
082:                                break;
083:                            for (int i = 0; i < cnt1; i++) {
084:                                if (buffer[i] != buf2[i]) {
085:                                    equal = false;
086:                                    break;
087:                                }
088:                            }
089:                        }
090:                        // clean up...
091:                        source.close();
092:                        tmp.close();
093:
094:                        source = tmp = null;
095:
096:                        if (equal)
097:                            return;
098:                    }
099:
100:                    source = new BufferedReader(new FileReader(tmp_file));
101:                    target = new BufferedWriter(new FileWriter(target_file));
102:
103:                    while (true) {
104:                        cnt = source.read(buffer, 0, 1024);
105:                        if (cnt == -1)
106:                            break;
107:                        target.write(buffer, 0, cnt);
108:                    }
109:                } finally {
110:                    if (source != null) {
111:                        try {
112:                            source.close();
113:                        } catch (IOException e) {
114:                            ;
115:                        }
116:                    }
117:                    if (target != null) {
118:                        try {
119:                            target.close();
120:                        } catch (IOException e) {
121:                            ;
122:                        }
123:                    }
124:                    // RK: Now if I'm correct this should be called anytime.
125:                    if (tmp_file != null && tmp_file.exists()) {
126:                        tmp_file.delete();
127:                        tmp_file = null;
128:                    }
129:                }
130:            }
131:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.