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: }
|