001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * SimpleDiff.java
044: *
045: * Created on February 8, 2001, 2:59 PM
046: */
047:
048: package org.netbeans.junit.diff;
049:
050: import java.io.*;
051:
052: /**
053: *
054: * @author vstejskal, Jan Becicka
055: * @version 2.0
056: */
057: public class SimpleDiff extends Object implements Diff {
058:
059: private static final int BUFSIZE = 1024;
060: private final LineDiff lineDiff;
061:
062: /** Creates new SimpleDiff */
063: public SimpleDiff() {
064: lineDiff = new LineDiff(false);
065: }
066:
067: /**
068: * @param first first file to compare
069: * @param second second file to compare
070: * @param diff difference file
071: * @return true iff files differ
072: */
073: public boolean diff(final java.io.File first,
074: final java.io.File second, java.io.File diff)
075: throws java.io.IOException {
076: if (isBinaryFile(first) || isBinaryFile(second)) {
077: return binaryCompare(first, second, diff);
078: } else {
079: return textualCompare(first, second, diff);
080: }
081: }
082:
083: /**
084: * @param first first file to compare
085: * @param second second file to compare
086: * @param diff difference file
087: * @return true iff files differ
088: */
089: public boolean diff(final String first, final String second,
090: String diff) throws java.io.IOException {
091: File fFirst = new File(first);
092: File fSecond = new File(second);
093: File fDiff = null != diff ? new File(diff) : null;
094: return diff(fFirst, fSecond, fDiff);
095: }
096:
097: protected boolean isBinaryFile(File file)
098: throws java.io.IOException {
099: byte[] buffer = new byte[BUFSIZE];
100: FileInputStream is = new FileInputStream(file);
101: try {
102: int bytesRead = is.read(buffer, 0, BUFSIZE);
103: if (bytesRead == -1)
104: return false;
105: for (int i = 0; i != bytesRead; i++) {
106: if (buffer[i] < 0)
107: return true;
108: }
109: return false;
110: } finally {
111: is.close();
112: }
113: }
114:
115: protected boolean binaryCompare(final File first,
116: final File second, File diff) throws java.io.IOException {
117: if (first.length() != second.length())
118: return true;
119:
120: InputStream fs1 = new BufferedInputStream(new FileInputStream(
121: first));
122: InputStream fs2 = new BufferedInputStream(new FileInputStream(
123: second));
124: byte[] b1 = new byte[BUFSIZE];
125: byte[] b2 = new byte[BUFSIZE];
126:
127: try {
128: while (true) {
129: int l1 = fs1.read(b1);
130: int l2 = fs2.read(b2);
131: if (l1 == -1) {
132: // files differ in length if l2 != -1; otherwise are equal
133: return l2 != -1;
134: }
135: if (l1 != l2)
136: return true; // files differ in length
137: if (!java.util.Arrays.equals(b1, b2))
138: return true; // files differ in content
139: }
140: } finally {
141: fs1.close();
142: fs2.close();
143: }
144: }
145:
146: protected boolean textualCompare(final File first,
147: final File second, File diff) throws java.io.IOException {
148: return lineDiff.diff(first, second, diff);
149: }
150: }
|