001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package util;
028:
029: import java.io.*;
030:
031: public class FileCompare {
032:
033: public static boolean debug = false;
034:
035: private static void debugPrint(File a, File b, String msg) {
036: System.err.print(a.getPath());
037: System.err.print(", ");
038: System.err.print(b.getPath());
039: System.err.print(" ");
040: System.err.println(msg);
041: }
042:
043: public static boolean cmp(File a, File b) {
044: if (!a.isFile() || !b.isFile()) {
045: if (debug) {
046: debugPrint(a, b, "not both plain files");
047: }
048: return false; // NotAFile compares unequal.
049: }
050: if (a.length() != b.length()) {
051: if (debug) {
052: debugPrint(a, b, "different lengths");
053: }
054: return false; // different lengthes cannot be the same.
055: }
056: try {
057: FileInputStream astream = null;
058: FileInputStream bstream = null;
059: try {
060: astream = new FileInputStream(a);
061: bstream = new FileInputStream(b);
062: long flength = a.length(); // == b.length(), remember?
063: int bufsize = (int) Math.min(flength, 1024);
064: byte abuf[] = new byte[bufsize];
065: byte bbuf[] = new byte[bufsize];
066: long n = 0;
067: while (n < flength) {
068: int naread = astream.read(abuf);
069: int nbread = bstream.read(bbuf);
070: if (naread != nbread)
071: return false; // oops.
072: for (int i = 0; i < naread; i++) {
073: if (abuf[i] != bbuf[i]) {
074: if (debug) {
075: debugPrint(a, b, "differ at byte "
076: + (n + i));
077: }
078: return false;
079: }
080: }
081: n += naread;
082: }
083: } finally {
084: if (astream != null)
085: astream.close();
086: if (bstream != null)
087: bstream.close();
088: }
089: } catch (IOException e) {
090: e.printStackTrace();
091: return false;
092: }
093: if (debug) {
094: debugPrint(a, b, "are the same");
095: }
096: return true;
097: }
098:
099: public static boolean cpy(File a, File b) {
100: try {
101: FileInputStream astream = null;
102: FileOutputStream bstream = null;
103: try {
104: astream = new FileInputStream(a);
105: bstream = new FileOutputStream(b);
106: long flength = a.length();
107: int bufsize = (int) Math.min(flength, 1024);
108: byte buf[] = new byte[bufsize];
109: long n = 0;
110: while (n < flength) {
111: int naread = astream.read(buf);
112: bstream.write(buf, 0, naread);
113: n += naread;
114: }
115: } finally {
116: if (astream != null)
117: astream.close();
118: if (bstream != null)
119: bstream.close();
120: }
121: } catch (IOException e) {
122: e.printStackTrace();
123: return false;
124: }
125: return true;
126: }
127:
128: public static void conditionalCopy(File fromFile, File toFile) {
129: if (!cmp(fromFile, toFile))
130: cpy(fromFile, toFile);
131: }
132:
133: public static void main(String args[]) {
134: debug = true;
135: conditionalCopy(new File(args[0]), new File(args[1]));
136: }
137:
138: }
|