1 26 27 29 package de.nava.informa.utils; 30 31 import java.io.BufferedReader ; 32 import java.io.File ; 33 import java.io.FileInputStream ; 34 import java.io.FileOutputStream ; 35 import java.io.FileReader ; 36 import java.io.IOException ; 37 import java.io.InputStream ; 38 import java.io.OutputStream ; 39 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 43 46 public final class FileUtils { 47 48 private static Log logger = LogFactory.getLog(FileUtils.class); 49 50 private FileUtils() { 51 } 52 53 public static boolean compare(String nameExpected, String nameActual) 54 throws IOException { 55 56 return compare(new File (nameExpected), new File (nameActual)); 57 } 58 59 public static boolean compare(File fileExpected, File fileActual) 60 throws IOException { 61 62 BufferedReader readExpected; 63 try { 64 logger.debug("Comparing golden file " + fileExpected + 65 " to " + fileActual); 66 readExpected = new BufferedReader (new FileReader (fileExpected)); 67 } catch (IOException e) { 68 logger.error("Could not read baseline: " + e); 69 return false; 70 } 71 BufferedReader readActual = 72 new BufferedReader (new FileReader (fileActual)); 73 return compare(readExpected, readActual); 74 } 75 76 private static boolean compare(BufferedReader readerExpected, 77 BufferedReader readerActual) 78 throws IOException { 79 80 String lineExpected = readerExpected.readLine(); 81 String lineActual = readerActual.readLine(); 82 while (lineExpected != null && lineActual != null) { 83 if (lineExpected == null || lineActual == null) { 84 return false; 85 } 86 if (!lineExpected.equals(lineActual)) { 87 return false; 88 } 89 lineExpected = readerExpected.readLine(); 90 lineActual = readerActual.readLine(); 91 } 92 readerExpected.close(); 93 readerActual.close(); 94 return lineExpected == null && lineActual == null; 95 } 96 97 100 public static void copyFile(File inFile, File outFile) { 101 try { 102 logger.debug("Copying file " + inFile + " to " + outFile); 103 InputStream in = new FileInputStream (inFile); 104 OutputStream out = new FileOutputStream (outFile); 105 byte[] buf = new byte[8 * 1024]; 106 int n; 107 while ((n = in.read(buf)) >= 0) { 108 out.write(buf, 0, n); 109 out.flush(); 110 } 111 in.close(); 112 out.close(); 113 } catch (Exception e) { 114 logger.warn("Error occurred while copying file " + inFile + " to " + outFile); 115 e.printStackTrace(); 116 } 117 } 118 119 } 120 | Popular Tags |