001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.util; 015 016import java.io.BufferedReader; 017import java.io.FileInputStream; 018import java.io.FileNotFoundException; 019import java.io.FileReader; 020import java.io.IOException; 021import java.io.InputStreamReader; 022import java.io.Reader; 023import java.util.zip.GZIPInputStream; 024import java.util.zip.ZipInputStream; 025 026public class Compare { 027 static final int B1_NULL = -1; 028 static final int B2_NULL = -2; 029 030 public static boolean compare(String file1, String file2) throws FileNotFoundException, IOException { 031 if (file1.endsWith(".gz")) { 032 return gzFileCompare(file1, file2); 033 } else if (file1.endsWith(".zip")) { 034 return zipFileCompare(file1, file2); 035 } else { 036 return regularFileCompare(file1, file2); 037 } 038 } 039 040 static BufferedReader gzFileToBufferedReader(String file) throws IOException { 041 FileInputStream fis = new FileInputStream(file); 042 GZIPInputStream gzis = new GZIPInputStream(fis); 043 return new BufferedReader(new InputStreamReader(gzis)); 044 } 045 046 static BufferedReader zipFileToBufferedReader(String file) throws IOException { 047 FileInputStream fis = new FileInputStream(file); 048 ZipInputStream zis = new ZipInputStream(fis); 049 zis.getNextEntry(); 050 return new BufferedReader(new InputStreamReader(zis)); 051 } 052 053 public static boolean gzFileCompare(String file1, String file2) throws IOException { 054 BufferedReader in1 = gzFileToBufferedReader(file1); 055 BufferedReader in2 = gzFileToBufferedReader(file2); 056 return bufferCompare(in1, in2, file1, file2); 057 } 058 059 public static boolean zipFileCompare(String file1, String file2) throws IOException { 060 BufferedReader in1 = zipFileToBufferedReader(file1); 061 BufferedReader in2 = zipFileToBufferedReader(file2); 062 return bufferCompare(in1, in2, file1, file2); 063 } 064 065 public static boolean regularFileCompare(String file1, String file2) throws FileNotFoundException, IOException { 066 BufferedReader in1 = new BufferedReader(new FileReader(file1)); 067 BufferedReader in2 = new BufferedReader(new FileReader(file2)); 068 return bufferCompare(in1, in2, file1, file2); 069 } 070 071 public static boolean bufferCompare(BufferedReader in1, BufferedReader in2, String file1, String file2) throws FileNotFoundException, IOException { 072 073 String s1; 074 int lineCounter = 0; 075 076 while ((s1 = in1.readLine()) != null) { 077 lineCounter++; 078 079 String s2 = in2.readLine(); 080 081 if (!s1.equals(s2)) { 082 System.out.println("Files [" + file1 + "] and [" + file2 + "] differ on line " + lineCounter); 083 System.out.println("One reads: [" + s1 + "]."); 084 System.out.println("Other reads:[" + s2 + "]."); 085 outputFile(file1); 086 outputFile(file2); 087 088 return false; 089 } 090 } 091 092 // the second file is longer 093 if (in2.read() != -1) { 094 System.out.println("File [" + file2 + "] longer than file [" + file1 + "]."); 095 outputFile(file1); 096 outputFile(file2); 097 098 return false; 099 } 100 101 return true; 102 } 103 104 /** 105 * 106 * Prints file on the console. 107 * 108 */ 109 private static void outputFile(String file) throws FileNotFoundException, IOException { 110 BufferedReader in1 = null; 111 112 try { 113 in1 = new BufferedReader(new FileReader(file)); 114 String s1; 115 int lineCounter = 0; 116 System.out.println("--------------------------------"); 117 System.out.println("Contents of " + file + ":"); 118 119 while ((s1 = in1.readLine()) != null) { 120 lineCounter++; 121 System.out.print(lineCounter); 122 123 if (lineCounter < 10) { 124 System.out.print(" : "); 125 } else if (lineCounter < 100) { 126 System.out.print(" : "); 127 } else if (lineCounter < 1000) { 128 System.out.print(" : "); 129 } else { 130 System.out.print(": "); 131 } 132 133 System.out.println(s1); 134 } 135 } finally { 136 close(in1); 137 } 138 } 139 140 public static boolean gzCompare(String file1, String file2) throws FileNotFoundException, IOException { 141 BufferedReader in1 = null; 142 BufferedReader in2 = null; 143 try { 144 in1 = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file1)))); 145 in2 = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file2)))); 146 147 String s1; 148 int lineCounter = 0; 149 150 while ((s1 = in1.readLine()) != null) { 151 lineCounter++; 152 153 String s2 = in2.readLine(); 154 155 if (!s1.equals(s2)) { 156 System.out.println("Files [" + file1 + "] and [" + file2 + "] differ on line " + lineCounter); 157 System.out.println("One reads: [" + s1 + "]."); 158 System.out.println("Other reads:[" + s2 + "]."); 159 outputFile(file1); 160 outputFile(file2); 161 162 return false; 163 } 164 } 165 166 // the second file is longer 167 if (in2.read() != -1) { 168 System.out.println("File [" + file2 + "] longer than file [" + file1 + "]."); 169 outputFile(file1); 170 outputFile(file2); 171 172 return false; 173 } 174 175 return true; 176 } finally { 177 close(in1); 178 close(in2); 179 } 180 } 181 182 static void close(Reader r) { 183 if (r != null) 184 try { 185 r.close(); 186 } catch (IOException e) { 187 } 188 } 189}