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)
72              throws FileNotFoundException, IOException {
73  
74          String s1;
75          int lineCounter = 0;
76  
77          while ((s1 = in1.readLine()) != null) {
78              lineCounter++;
79  
80              String s2 = in2.readLine();
81  
82              if (!s1.equals(s2)) {
83                  System.out.println("Files [" + file1 + "] and [" + file2 + "] differ on line " + lineCounter);
84                  System.out.println("One reads:  [" + s1 + "].");
85                  System.out.println("Other reads:[" + s2 + "].");
86                  outputFile(file1);
87                  outputFile(file2);
88  
89                  return false;
90              }
91          }
92  
93          // the second file is longer
94          if (in2.read() != -1) {
95              System.out.println("File [" + file2 + "] longer than file [" + file1 + "].");
96              outputFile(file1);
97              outputFile(file2);
98  
99              return false;
100         }
101 
102         return true;
103     }
104 
105     /**
106      * 
107      * Prints file on the console.
108      * 
109      */
110     private static void outputFile(String file) throws FileNotFoundException, IOException {
111         BufferedReader in1 = null;
112 
113         try {
114             in1 = new BufferedReader(new FileReader(file));
115             String s1;
116             int lineCounter = 0;
117             System.out.println("--------------------------------");
118             System.out.println("Contents of " + file + ":");
119 
120             while ((s1 = in1.readLine()) != null) {
121                 lineCounter++;
122                 System.out.print(lineCounter);
123 
124                 if (lineCounter < 10) {
125                     System.out.print("   : ");
126                 } else if (lineCounter < 100) {
127                     System.out.print("  : ");
128                 } else if (lineCounter < 1000) {
129                     System.out.print(" : ");
130                 } else {
131                     System.out.print(": ");
132                 }
133 
134                 System.out.println(s1);
135             }
136         } finally {
137             close(in1);
138         }
139     }
140 
141     public static boolean gzCompare(String file1, String file2) throws FileNotFoundException, IOException {
142         BufferedReader in1 = null;
143         BufferedReader in2 = null;
144         try {
145             in1 = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file1))));
146             in2 = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file2))));
147 
148             String s1;
149             int lineCounter = 0;
150 
151             while ((s1 = in1.readLine()) != null) {
152                 lineCounter++;
153 
154                 String s2 = in2.readLine();
155 
156                 if (!s1.equals(s2)) {
157                     System.out.println("Files [" + file1 + "] and [" + file2 + "] differ on line " + lineCounter);
158                     System.out.println("One reads:  [" + s1 + "].");
159                     System.out.println("Other reads:[" + s2 + "].");
160                     outputFile(file1);
161                     outputFile(file2);
162 
163                     return false;
164                 }
165             }
166 
167             // the second file is longer
168             if (in2.read() != -1) {
169                 System.out.println("File [" + file2 + "] longer than file [" + file1 + "].");
170                 outputFile(file1);
171                 outputFile(file2);
172 
173                 return false;
174             }
175 
176             return true;
177         } finally {
178             close(in1);
179             close(in2);
180         }
181     }
182 
183     static void close(Reader r) {
184         if (r != null)
185             try {
186                 r.close();
187             } catch (IOException e) {
188             }
189     }
190 }