View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.util;
15  
16  import java.io.BufferedReader;
17  import java.io.FileInputStream;
18  import java.io.FileNotFoundException;
19  import java.io.FileReader;
20  import java.io.IOException;
21  import java.io.InputStreamReader;
22  import java.io.Reader;
23  import java.util.zip.GZIPInputStream;
24  import java.util.zip.ZipInputStream;
25  
26  public class Compare {
27      static final int B1_NULL = -1;
28      static final int B2_NULL = -2;
29  
30      public static boolean compare(String file1, String file2) throws FileNotFoundException, IOException {
31          if (file1.endsWith(".gz")) {
32              return gzFileCompare(file1, file2);
33          } else if (file1.endsWith(".zip")) {
34              return zipFileCompare(file1, file2);
35          } else {
36              return regularFileCompare(file1, file2);
37          }
38      }
39  
40      static BufferedReader gzFileToBufferedReader(String file) throws IOException {
41          FileInputStream fis = new FileInputStream(file);
42          GZIPInputStream gzis = new GZIPInputStream(fis);
43          return new BufferedReader(new InputStreamReader(gzis));
44      }
45  
46      static BufferedReader zipFileToBufferedReader(String file) throws IOException {
47          FileInputStream fis = new FileInputStream(file);
48          ZipInputStream zis = new ZipInputStream(fis);
49          zis.getNextEntry();
50          return new BufferedReader(new InputStreamReader(zis));
51      }
52  
53      public static boolean gzFileCompare(String file1, String file2) throws IOException {
54          BufferedReader in1 = gzFileToBufferedReader(file1);
55          BufferedReader in2 = gzFileToBufferedReader(file2);
56          return bufferCompare(in1, in2, file1, file2);
57      }
58  
59      public static boolean zipFileCompare(String file1, String file2) throws IOException {
60          BufferedReader in1 = zipFileToBufferedReader(file1);
61          BufferedReader in2 = zipFileToBufferedReader(file2);
62          return bufferCompare(in1, in2, file1, file2);
63      }
64  
65      public static boolean regularFileCompare(String file1, String file2) throws FileNotFoundException, IOException {
66          BufferedReader in1 = new BufferedReader(new FileReader(file1));
67          BufferedReader in2 = new BufferedReader(new FileReader(file2));
68          return bufferCompare(in1, in2, file1, file2);
69      }
70  
71      public static boolean bufferCompare(BufferedReader in1, BufferedReader in2, String file1, String file2) throws FileNotFoundException, IOException {
72  
73          String s1;
74          int lineCounter = 0;
75  
76          while ((s1 = in1.readLine()) != null) {
77              lineCounter++;
78  
79              String s2 = in2.readLine();
80  
81              if (!s1.equals(s2)) {
82                  System.out.println("Files [" + file1 + "] and [" + file2 + "] differ on line " + lineCounter);
83                  System.out.println("One reads:  [" + s1 + "].");
84                  System.out.println("Other reads:[" + s2 + "].");
85                  outputFile(file1);
86                  outputFile(file2);
87  
88                  return false;
89              }
90          }
91  
92          // the second file is longer
93          if (in2.read() != -1) {
94              System.out.println("File [" + file2 + "] longer than file [" + file1 + "].");
95              outputFile(file1);
96              outputFile(file2);
97  
98              return false;
99          }
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 }