Source Code Cross Referenced for CopyFile.java in  » IDE-Netbeans » etl.project » org » netbeans » modules » etlbulkloader » tools » 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 » etl.project » org.netbeans.modules.etlbulkloader.tools 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * To change this template, choose Tools | Templates
003:         * and open the template in the editor.
004:         */
005:        package org.netbeans.modules.etlbulkloader.tools;
006:
007:        /**
008:         *
009:         * @author Manish
010:         */
011:        import java.io.*;
012:        import java.util.zip.*;
013:        import net.java.hulp.i18n.Logger;
014:        import org.netbeans.modules.etl.logger.Localizer;
015:        import org.netbeans.modules.etl.logger.LogUtil;
016:
017:        /**
018:         * Command line program to copy a file to another directory.
019:         */
020:        public class CopyFile {
021:            // constant values for the override option
022:            public static final int OVERWRITE_ALWAYS = 1;
023:            public static final int OVERWRITE_NEVER = 2;
024:            public static final int OVERWRITE_ASK = 3;
025:
026:            // program options initialized to default values
027:            private static int bufferSize = 4 * 1024;
028:            private static boolean clock = true;
029:            private static boolean copyOriginalTimestamp = true;
030:            private static boolean verify = true;
031:            private static int override = OVERWRITE_ALWAYS;
032:
033:            private static transient final Logger mLogger = LogUtil
034:                    .getLogger(CopyFile.class.getName());
035:            private static transient final Localizer mLoc = Localizer.get();
036:
037:            public static Long copyFile(File srcFile, File destFile)
038:                    throws IOException {
039:                InputStream in = new FileInputStream(srcFile);
040:                OutputStream out = new FileOutputStream(destFile);
041:                long millis = System.currentTimeMillis();
042:                CRC32 checksum = null;
043:                if (verify) {
044:                    checksum = new CRC32();
045:                    checksum.reset();
046:                }
047:                byte[] buffer = new byte[bufferSize];
048:                int bytesRead;
049:                while ((bytesRead = in.read(buffer)) >= 0) {
050:                    if (verify) {
051:                        checksum.update(buffer, 0, bytesRead);
052:                    }
053:                    out.write(buffer, 0, bytesRead);
054:                }
055:                out.close();
056:                in.close();
057:                if (clock) {
058:                    millis = System.currentTimeMillis() - millis;
059:                    mLogger.infoNoloc(mLoc.t("Second(s): " + (millis / 1000L)));
060:                }
061:                if (verify) {
062:                    return new Long(checksum.getValue());
063:                } else {
064:                    return null;
065:                }
066:            }
067:
068:            public static Long createChecksum(File file) throws IOException {
069:                long millis = System.currentTimeMillis();
070:                InputStream in = new FileInputStream(file);
071:                CRC32 checksum = new CRC32();
072:                checksum.reset();
073:                byte[] buffer = new byte[bufferSize];
074:                int bytesRead;
075:                while ((bytesRead = in.read(buffer)) >= 0) {
076:                    checksum.update(buffer, 0, bytesRead);
077:                }
078:                in.close();
079:                if (clock) {
080:                    millis = System.currentTimeMillis() - millis;
081:                    mLogger.infoNoloc(mLoc.t("Second(s): " + (millis / 1000L)));
082:                }
083:                return new Long(checksum.getValue());
084:            }
085:
086:            /**
087:             * Determine if data is to be copied to given file.
088:             * Take into consideration override option and 
089:             * ask user in case file exists and override option is ask.
090:             * @param file File object for potential destination file
091:             * @return true if data is to be copied to file, false if not
092:             */
093:            public static boolean doCopy(File file) {
094:                boolean exists = file.exists();
095:                if (override == OVERWRITE_ALWAYS || !exists) {
096:                    return true;
097:                } else if (override == OVERWRITE_NEVER) {
098:                    return false;
099:                } else if (override == OVERWRITE_ASK) {
100:                    return readYesNoFromStandardInput("File exists. "
101:                            + "Overwrite (y/n)?");
102:                } else {
103:                    throw new InternalError("Program error. Invalid "
104:                            + "value for override: " + override);
105:                }
106:            }
107:
108:            public static void main(String[] args) throws IOException {
109:                // make sure there are exactly two arguments
110:                if (args.length != 2) {
111:                    System.err
112:                            .println("Usage: CopyFile SRC-FILE-NAME DEST-DIR-NAME");
113:                    System.exit(1);
114:                }
115:                // make sure the source file is indeed a readable file
116:                File srcFile = new File(args[0]);
117:                if (!srcFile.isFile() || !srcFile.canRead()) {
118:                    System.err.println("Not a readable file: "
119:                            + srcFile.getName());
120:                    System.exit(1);
121:                }
122:                // make sure the second argument is a directory
123:                File destDir = new File(args[1]);
124:                if (!destDir.isDirectory()) {
125:                    System.err.println("Not a directory: " + destDir.getName());
126:                    System.exit(1);
127:                }
128:                // create File object for destination file
129:                File destFile = new File(destDir, srcFile.getName());
130:
131:                // check if copying is desired given overwrite option
132:                if (!doCopy(destFile)) {
133:                    return;
134:                }
135:
136:                // copy file, optionally creating a checksum
137:                Long checksumSrc = copyFile(srcFile, destFile);
138:
139:                // copy timestamp of last modification
140:                if (copyOriginalTimestamp) {
141:                    if (!destFile.setLastModified(srcFile.lastModified())) {
142:                        System.err.println("Error: Could not set "
143:                                + "timestamp of copied file.");
144:                    }
145:                }
146:
147:                // optionally verify file
148:                if (verify) {
149:                    System.out.print("Verifying destination file...");
150:                    Long checksumDest = createChecksum(destFile);
151:                    if (checksumSrc.equals(checksumDest)) {
152:                        mLogger.infoNoloc(mLoc.t(" OK, files are equal."));
153:                    } else {
154:                        mLogger.infoNoloc(mLoc.t(" Error: Checksums differ."));
155:                    }
156:                }
157:            }
158:
159:            /**
160:             * Print a message to standard output and read lines from
161:             * standard input until yes or no (y or n) is entered.
162:             * @param message informative text to be answered by user
163:             * @return user answer, true for yes, false for no.
164:             */
165:            public static boolean readYesNoFromStandardInput(String message) {
166:                String line;
167:                BufferedReader in = new BufferedReader(new InputStreamReader(
168:                        System.in));
169:                Boolean answer = null;
170:                try {
171:                    while ((line = in.readLine()) != null) {
172:                        line = line.toLowerCase();
173:                        if ("y".equals(line) || "yes".equals(line)) {
174:                            answer = Boolean.TRUE;
175:                            break;
176:                        } else if ("n".equals(line) || "no".equals(line)) {
177:                            answer = Boolean.FALSE;
178:                            break;
179:                        } else {
180:                            mLogger
181:                                    .infoNoloc(mLoc
182:                                            .t("Could not understand answer (\""
183:                                                    + line
184:                                                    + "\"). Please use y for yes or n for no."));
185:                        }
186:                    }
187:                    if (answer == null) {
188:                        throw new IOException(
189:                                "Unexpected end of input from stdin.");
190:                    }
191:                    in.close();
192:                    return answer.booleanValue();
193:                } catch (IOException ioe) {
194:                    throw new InternalError(
195:                            "Cannot read from stdin or write to stdout.");
196:                }
197:            }
198:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.