001: //
002: // Informa -- RSS Library for Java
003: // Copyright (c) 2002-2003 by Niko Schmuck
004: //
005: // Niko Schmuck
006: // http://sourceforge.net/projects/informa
007: // mailto:niko_schmuck@users.sourceforge.net
008: //
009: // This library is free software.
010: //
011: // You may redistribute it and/or modify it under the terms of the GNU
012: // Lesser General Public License as published by the Free Software Foundation.
013: //
014: // Version 2.1 of the license should be included with this distribution in
015: // the file LICENSE. If the license is not included with this distribution,
016: // you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
017: // or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
018: // MA 02139 USA.
019: //
020: // This library is distributed in the hope that it will be useful,
021: // but WITHOUT ANY WARRANTY; without even the implied waranty of
022: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
023: // Lesser General Public License for more details.
024: //
025:
026: // $Id: FileUtils.java,v 1.4 2003/07/26 10:04:38 niko_schmuck Exp $
027:
028: package de.nava.informa.utils;
029:
030: import java.io.BufferedReader;
031: import java.io.File;
032: import java.io.FileInputStream;
033: import java.io.FileOutputStream;
034: import java.io.FileReader;
035: import java.io.IOException;
036: import java.io.InputStream;
037: import java.io.OutputStream;
038:
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041:
042: /**
043: * Utility class providing some convenience methods when handling files.
044: */
045: public final class FileUtils {
046:
047: private static Log logger = LogFactory.getLog(FileUtils.class);
048:
049: private FileUtils() {
050: }
051:
052: public static boolean compare(String nameExpected, String nameActual)
053: throws IOException {
054:
055: return compare(new File(nameExpected), new File(nameActual));
056: }
057:
058: public static boolean compare(File fileExpected, File fileActual)
059: throws IOException {
060:
061: BufferedReader readExpected;
062: try {
063: logger.debug("Comparing golden file " + fileExpected
064: + " to " + fileActual);
065: readExpected = new BufferedReader(new FileReader(
066: fileExpected));
067: } catch (IOException e) {
068: logger.error("Could not read baseline: " + e);
069: return false;
070: }
071: BufferedReader readActual = new BufferedReader(new FileReader(
072: fileActual));
073: return compare(readExpected, readActual);
074: }
075:
076: private static boolean compare(BufferedReader readerExpected,
077: BufferedReader readerActual) throws IOException {
078:
079: String lineExpected = readerExpected.readLine();
080: String lineActual = readerActual.readLine();
081: while (lineExpected != null && lineActual != null) {
082: if (lineExpected == null || lineActual == null) {
083: return false;
084: }
085: if (!lineExpected.equals(lineActual)) {
086: return false;
087: }
088: lineExpected = readerExpected.readLine();
089: lineActual = readerActual.readLine();
090: }
091: readerExpected.close();
092: readerActual.close();
093: return lineExpected == null && lineActual == null;
094: }
095:
096: /**
097: * Copies a file from <code>inFile</copy> to <code>outFile</code>.
098: */
099: public static void copyFile(File inFile, File outFile) {
100: try {
101: logger.debug("Copying file " + inFile + " to " + outFile);
102: InputStream in = new FileInputStream(inFile);
103: OutputStream out = new FileOutputStream(outFile);
104: byte[] buf = new byte[8 * 1024];
105: int n;
106: while ((n = in.read(buf)) >= 0) {
107: out.write(buf, 0, n);
108: out.flush();
109: }
110: in.close();
111: out.close();
112: } catch (Exception e) {
113: logger.warn("Error occurred while copying file " + inFile
114: + " to " + outFile);
115: e.printStackTrace();
116: }
117: }
118:
119: }
|