package journal.reader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.text.ParseException; import java.util.zip.GZIPInputStream; public class JournalComparer { private String file1; private String file2; private DataProvider provider1; private DataProvider provider2; private int lineCounter; public JournalComparer(String file1, String file2, int lineCounter) { this.file1 = file1; this.file2 = file2; this.lineCounter = lineCounter; } static class DataProvider { private String filename; private JournalParser parser; private int lineCounter; private int line = 0; public DataProvider(String filename, int lineCounter) throws FileNotFoundException { this.filename = filename; this.lineCounter = lineCounter; InputStream in = new FileInputStream(filename); if (filename.endsWith(".gz")) { try { in = new GZIPInputStream(in); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } parser = new JournalParser(new InputStreamReader(in), new Options()); } public DataJournalEntry next() throws ParseException, IOException { JournalEntry entry = parser.parseNext(); line++; if (lineCounter > 0 && line > 0 && line % lineCounter == 0) { System.err.println(filename + " : " + line); } if (entry instanceof TransactionJournalEntry) { entry = parser.parseNext(); line++; if (entry == null) { return null; } } return (DataJournalEntry) entry; } } public void run() throws ParseException, IOException { provider1 = new DataProvider(file1, lineCounter); provider2 = new DataProvider(file2, lineCounter); DataJournalEntry data1 = provider1.next(); DataJournalEntry data2 = provider2.next(); while (data1 != null && data2 != null) { int compare = data1.compareTo(data2); if (compare == 0) { if (! data1.equals(data2)) { System.out.println("Entries differ : "); System.out.println("1 : " + data1.toJournalString()); System.out.println("2 : " + data2.toJournalString()); } data1 = provider1.next(); data2 = provider2.next(); continue; } else if (compare < 0) { System.out.println("1 : " + data1.toJournalString()); data1 = provider1.next(); } else { // compare > 0 System.out.println("2 : " + data2.toJournalString()); data2 = provider2.next(); } } while (data1 != null) { System.out.println("1 : " + data1.toJournalString()); data1 = provider1.next(); } while (data2 != null) { System.out.println("2 : " + data2.toJournalString()); data2 = provider2.next(); } } public static void main(String[] args) { if (args.length < 2) { System.out.println("Usage : journal.reader.JournalComparer <file1> <file2> [linecounter]"); System.exit(1); } String file1 = args[0]; String file2 = args[1]; int lineCounter = 0; if (args.length == 3) { lineCounter = Integer.parseInt(args[2]); } JournalComparer comp = new JournalComparer(file1, file2, lineCounter); try { comp.run(); } catch (Exception e) { e.printStackTrace(); } } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 8019 | Sven Erik Knop | Safety checks for case-sensitivity and Unicode, now provided in the checkpoint. | ||
#1 | 7527 | Sven Erik Knop |
JournalReader, now in its proper place. Documentation to follow. |