1
2
3
4
5
6
7
8
9
10
11 package ch.qos.logback.core.util;
12
13 import java.io.BufferedReader;
14 import java.io.FileInputStream;
15 import java.io.FileNotFoundException;
16 import java.io.FileReader;
17 import java.io.IOException;
18 import java.io.InputStreamReader;
19 import java.util.zip.GZIPInputStream;
20
21 public class Compare {
22 static final int B1_NULL = -1;
23 static final int B2_NULL = -2;
24
25 public static boolean compare(String file1, String file2) throws FileNotFoundException, IOException {
26 if (file1.endsWith(".gz")) {
27
28 return gzFileCompare(file1, file2);
29 } else {
30 return regularFileCompare(file1, file2);
31 }
32 }
33
34 static BufferedReader gzFileToBufferedReader(String file) throws IOException {
35 FileInputStream fis = new FileInputStream(file);
36 GZIPInputStream gzis = new GZIPInputStream(fis);
37 BufferedReader br = new BufferedReader(new InputStreamReader(gzis));
38 return br;
39 }
40
41 public static boolean gzFileCompare(String file1, String file2) throws IOException {
42 BufferedReader in1 = gzFileToBufferedReader(file1);
43 BufferedReader in2 = gzFileToBufferedReader(file2);
44 return bufferCompare(in1, in2, file1, file2);
45 }
46
47 public static boolean regularFileCompare(String file1, String file2)
48 throws FileNotFoundException, IOException {
49 BufferedReader in1 = new BufferedReader(new FileReader(file1));
50 BufferedReader in2 = new BufferedReader(new FileReader(file2));
51 return bufferCompare(in1, in2, file1, file2);
52 }
53
54 public static boolean bufferCompare(BufferedReader in1, BufferedReader in2,
55 String file1, String file2) throws FileNotFoundException, IOException {
56
57 String s1;
58 int lineCounter = 0;
59
60 while ((s1 = in1.readLine()) != null) {
61 lineCounter++;
62
63 String s2 = in2.readLine();
64
65 if (!s1.equals(s2)) {
66 System.out.println("Files [" + file1 + "] and [" + file2
67 + "] differ on line " + lineCounter);
68 System.out.println("One reads: [" + s1 + "].");
69 System.out.println("Other reads:[" + s2 + "].");
70 outputFile(file1);
71 outputFile(file2);
72
73 return false;
74 }
75 }
76
77
78 if (in2.read() != -1) {
79 System.out.println("File [" + file2 + "] longer than file [" + file1
80 + "].");
81 outputFile(file1);
82 outputFile(file2);
83
84 return false;
85 }
86
87 return true;
88 }
89
90
91
92
93
94
95 private static void outputFile(String file) throws FileNotFoundException,
96 IOException {
97 BufferedReader in1 = new BufferedReader(new FileReader(file));
98
99 String s1;
100 int lineCounter = 0;
101 System.out.println("--------------------------------");
102 System.out.println("Contents of " + file + ":");
103
104 while ((s1 = in1.readLine()) != null) {
105 lineCounter++;
106 System.out.print(lineCounter);
107
108 if (lineCounter < 10) {
109 System.out.print(" : ");
110 } else if (lineCounter < 100) {
111 System.out.print(" : ");
112 } else if (lineCounter < 1000) {
113 System.out.print(" : ");
114 } else {
115 System.out.print(": ");
116 }
117
118 System.out.println(s1);
119 }
120 }
121
122 public static boolean gzCompare(String file1, String file2)
123 throws FileNotFoundException, IOException {
124 BufferedReader in1 = new BufferedReader(new InputStreamReader(
125 new GZIPInputStream(new FileInputStream(file1))));
126 BufferedReader in2 = new BufferedReader(new InputStreamReader(
127 new GZIPInputStream(new FileInputStream(file2))));
128
129 String s1;
130 int lineCounter = 0;
131
132 while ((s1 = in1.readLine()) != null) {
133 lineCounter++;
134
135 String s2 = in2.readLine();
136
137 if (!s1.equals(s2)) {
138 System.out.println("Files [" + file1 + "] and [" + file2
139 + "] differ on line " + lineCounter);
140 System.out.println("One reads: [" + s1 + "].");
141 System.out.println("Other reads:[" + s2 + "].");
142 outputFile(file1);
143 outputFile(file2);
144
145 return false;
146 }
147 }
148
149
150 if (in2.read() != -1) {
151 System.out.println("File [" + file2 + "] longer than file [" + file1
152 + "].");
153 outputFile(file1);
154 outputFile(file2);
155
156 return false;
157 }
158
159 return true;
160 }
161 }