/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun * Microsystems, Inc. All Rights Reserved. */ /* * SimpleDiff.java * * Created on February 8, 2001, 2:59 PM */ package org.netbeans.junit.diff; import java.io.*; /** * * @author vstejskal, Jan Becicka * @version 2.0 */ public class SimpleDiff extends Object implements Diff { /** Creates new SimpleDiff */ public SimpleDiff() { } /** * @param first first file to compare * @param second second file to compare * @param diff difference file * @return true iff files differ */ public boolean diff(final java.io.File first, final java.io.File second, java.io.File diff) throws java.io.IOException { if (isBinaryFile(first) || isBinaryFile(second)) { return binaryCompare(first, second, diff); } else { return textualCompare(first, second, diff); } } /** * @param first first file to compare * @param second second file to compare * @param diff difference file * @return true iff files differ */ public boolean diff(final String first, final String second, String diff) throws java.io.IOException { File fFirst = new File(first); File fSecond = new File(second); File fDiff = null != diff ? new File(diff) : null; return diff(fFirst, fSecond, fDiff); } protected boolean compareBuffers(char [] buffer1, char [] buffer2) { int i, len; len = buffer1.length; if (len != buffer2.length) return true; for( i = 0; i < len; i++) if (buffer1[i] != buffer2[i]) return true; return false; } protected boolean isBinaryFile(File file) throws java.io.IOException { FileInputStream is = null; byte[] buffer = new byte[1024]; is = new FileInputStream(file); int bytesRead = is.read(buffer, 0, 1024); if (bytesRead == -1) return false; for (int i = 0; i != bytesRead; i++) { if (buffer[i] < 0) return true; } is.close(); return false; } protected boolean binaryCompare(final File first, final File second, File diff) throws java.io.IOException { boolean different = false; if (first.length() != second.length()) return true; FileReader fFirst = new FileReader(first); FileReader fSecond = new FileReader(second); char [] buffer1 = new char [256]; char [] buffer2 = new char [256]; int size1, size2; do { size1 = fFirst.read(buffer1); size2 = fSecond.read(buffer2); if (size1 != size2 || compareBuffers(buffer1, buffer2)) { different = true; break; } } while (-1 == size1 || -1 == size2); return different; } protected boolean textualCompare(final File first, final File second, File diff) throws java.io.IOException { BufferedReader r1 = new BufferedReader(new FileReader(first)); BufferedReader r2 = new BufferedReader(new FileReader(second)); while (true) { String s1 = r1.readLine(); String s2 = r2.readLine(); if (s1 == null) { if (s2 == null) return false; // files equal else return true; // files differ in length } if (!s1.equals(s2)) { return true; // files differ } } } }