Source Code Cross Referenced for PreservingFileWriter.java in  » Database-ORM » toplink » persistence » 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 » Database ORM » toplink » persistence.antlr 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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