1 21 22 package org.apache.derbyTesting.functionTests.harness; 24 25 import java.io.IOException ; 26 import java.io.BufferedInputStream ; 27 import java.util.Vector ; 28 import java.io.File ; 29 import java.io.PrintWriter ; 30 import java.io.BufferedReader ; 31 import java.io.FileReader ; 32 33 public class SimpleDiff 34 { 35 36 PrintWriter pw; 37 38 boolean debugOn = Boolean.getBoolean("simplediff.debug"); 39 int debugLevel = 1; 40 41 boolean diffsFound = true; 42 43 int lookAhead = 20; 44 45 public void debug(int level, String msg) 46 { 47 if (debugLevel >= level) 48 { 49 debug(msg); 50 } 51 } 52 public void debug(String msg) 53 { 54 if (debugOn) 55 { 56 System.out.println("DEBUG: " + msg); 57 } 58 } 59 60 int lineCount(String file) throws IOException 61 { 62 BufferedReader input = new BufferedReader (new FileReader (file)); 63 int count = 0; 64 String aLine = input.readLine(); 65 while (aLine != null) 66 { 67 count++; 68 aLine = input.readLine(); 69 } 70 input.close(); 71 return count; 72 } 73 74 75 public String [] readFile(BufferedReader input) throws IOException 76 { 77 78 Vector vec = new Vector (); 79 80 String aLine = ""; 81 aLine = input.readLine(); 83 while (aLine != null) 84 { 85 vec.addElement(aLine); 86 aLine = input.readLine(); 88 } 89 input.close(); 90 91 String rV[] = new String [vec.size()]; 92 debug(2, ""+vec.size() + " lines in input"); 94 vec.copyInto(rV); 95 96 return rV; 97 } 98 99 public void printFile(String file[]) 100 { 101 for (int i = 0; i < file.length; i++) 102 { 103 pw.println(i + ": " + file[i]); 104 System.out.println(i + ": " + file[i]); 105 } 106 } 107 108 public static String usage = "java SimpleDiff <file1> <file2>"; 109 110 116 117 public String [] diffFiles( DiffBuffer file1, 118 DiffBuffer file2) 119 throws IOException 120 { 121 122 int currentLine1 = 0; 123 int currentLine2 = 0; 124 Vector returnVec = new Vector (); 125 126 while ( file1.isValidOffset(currentLine1) && 127 file2.isValidOffset(currentLine2)) 128 { 129 String f1 = file1.lineAt(currentLine1); 130 String f2 = file2.lineAt(currentLine2); 131 132 if (f1.equals(f2)) 133 { 134 debug(1, currentLine1 + ": match"); 135 currentLine1++; 136 currentLine2++; 137 file1.setLowWater(currentLine1); 138 file2.setLowWater(currentLine2); 139 } 140 else 141 { 142 boolean foundMatch = false; 143 int checkLine2 = currentLine2; 144 int checkCount = 1; 145 while ( file2.isValidOffset(currentLine2 + checkCount) && 147 checkCount < lookAhead) 148 { 149 debug(1, "currentLine1 " + currentLine1 + " currentLine2 " + (currentLine2 +checkCount)); 150 debug(1, "about to reference file2[" + (currentLine2 + checkCount) + "]"); 151 f2 = file2.lineAt(currentLine2 + checkCount); 152 debug(2, "did"); 153 if (f1.equals(f2)) 154 { 155 foundMatch = true; 156 if (checkCount > 1) 157 { 158 returnVec.addElement(currentLine1 + "a" + (currentLine2 + 1) + "," + (currentLine2 + checkCount)); 159 } 160 else 161 { 162 returnVec.addElement(currentLine1 + "a" + (currentLine2 + 1)); 163 } 164 165 for (int j = 0; j < checkCount; j++) 166 { 167 returnVec.addElement("> " + 168 file2.lineAt(currentLine2 + j) ); 169 } 170 currentLine2 = currentLine2 + checkCount; 171 checkCount = 0; 172 173 break; 181 } 182 checkCount ++; 183 } 184 if (!foundMatch && file2.isValidOffset(currentLine2)) 185 { 186 int checkLine1 = currentLine1; 187 checkCount = 1; 188 f2 = file2.lineAt(currentLine2); 189 while ( file1.isValidOffset(currentLine1 + checkCount) && 190 checkCount < lookAhead) 191 { 192 debug(1, "currentLine2 " + currentLine2 + " currentLine1 " + (currentLine1 + checkCount)); 193 f1 = file1.lineAt(currentLine1 + checkCount); 194 if ( f2.equals(f1)) 195 { 196 foundMatch = true; 197 if (checkCount > 1) 198 { 199 returnVec.addElement((currentLine1 + 1) + "," + (currentLine1 + checkCount) + "d" + currentLine2); 200 } 201 else 202 { 203 returnVec.addElement((currentLine1 + 1) + "d" + currentLine2); 204 } 205 206 for (int j = 0; j < checkCount; j++) 207 { 208 returnVec.addElement("< " + 209 file1.lineAt(currentLine1 + j) ); 210 211 } 212 currentLine1 = currentLine1 + checkCount; 213 checkCount = 0; 214 debug(1, "continuing"); 215 break; 216 } 217 checkCount ++; 218 } 219 220 } 221 if (!foundMatch) 222 { 223 debug(1, currentLine1 + ": NOMATCH"); 224 returnVec.addElement((currentLine1 + 1) +" del"); returnVec.addElement("< " + file1.lineAt(currentLine1)); 226 currentLine1++; 227 } 228 else 229 { 230 currentLine1++; 231 currentLine2++; 232 file1.setLowWater(currentLine1); 233 file2.setLowWater(currentLine2); 234 } 235 } 236 } 237 238 if (file1.isValidOffset(currentLine1)) 239 { 240 returnVec.addElement((currentLine2) + " del"); 241 for (int i = currentLine1; file1.isValidOffset(i); i++) 242 { 243 returnVec.addElement("< " + file1.lineAt(i)); 244 } 245 } 246 if (file2.isValidOffset(currentLine2)) 247 { 248 returnVec.addElement((currentLine1) + " add"); 249 for (int i = currentLine2; file2.isValidOffset(i); i++) 250 { 251 returnVec.addElement("> " + file2.lineAt(i)); 252 } 253 } 254 255 file1.close(); 256 file2.close(); 257 258 if (returnVec.size() == 0) 259 { 260 return null; 261 } 262 263 264 String [] returnArray = new String [returnVec.size()]; 265 returnVec.copyInto(returnArray); 266 return returnArray; 267 268 269 } 270 271 private void reportMemory() 272 { 273 reportMemory(null); 274 } 275 276 private void reportMemory(String header) 277 { 278 if (header != null) 279 { 280 System.out.println(header); 281 } 282 long free = Runtime.getRuntime().freeMemory(); 283 long total = Runtime.getRuntime().totalMemory(); 284 285 System.out.println("total: " + total ); 286 System.out.println("free: " + free ); 287 System.out.println("used: " + (total - free)); 288 System.gc(); 289 System.out.println("used: <postgc> " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())); 290 System.out.println(" "); 291 } 292 293 public boolean doWork(BufferedReader in1, BufferedReader in2, PrintWriter localPW) throws IOException 294 { 295 this.pw=(localPW==null?new PrintWriter (System.out):localPW); 296 try 297 { 298 DiffBuffer db1 = new DiffBuffer(in1, "1"); 299 DiffBuffer db2 = new DiffBuffer(in2, "2"); 300 301 String diffs[] = diffFiles(db1, db2); 302 303 if (diffs == null) 304 { 305 debug(1, "no diff"); 306 return false; 307 } 308 else 309 { 310 for (int i = 0; i < diffs.length; i++) 311 { 312 this.pw.println(diffs[i]); 313 System.out.println(diffs[i]); 314 } 315 } 316 317 } 318 catch (IOException ioe) 319 { 320 System.err.println("IOException comparing <" + in1 + 321 "> and <" + in2 + ">"); 322 System.err.println(ioe); 323 324 this.pw.println("IOException comparing <" + in1 + 325 "> and <" + in2 + ">"); 326 this.pw.println(ioe); 327 } 328 return true; 329 } 330 public static void main(String args[]) throws IOException 331 { 332 333 if (args.length < 2) 334 { 335 System.err.println("Invalid number of arguments"); 336 System.err.println("Usage: " + usage); 337 System.exit(1); 338 } 339 340 SimpleDiff me = new SimpleDiff(); 341 342 try 343 { 344 BufferedReader br1 = new BufferedReader (new FileReader (args[0])); 345 BufferedReader br2 = new BufferedReader (new FileReader (args[1])); 346 me.doWork(br1, br2, null); 347 } 348 catch (IOException ioe) 349 { 350 System.out.println("IOExeption: " + ioe); 351 } 352 } 353 354 public void pause() 355 { 356 BufferedInputStream bis = new BufferedInputStream (System.in); 357 try 358 { 359 bis.read(); 360 } 361 catch (IOException ioe) 362 { 363 pw.println("Error trying to pause..."); 364 System.out.println("Error trying to pause..."); 365 } 366 } 367 368 class DiffBuffer extends Vector 369 { 370 371 372 public boolean atEOF() 373 { 374 return atEnd; 375 } 376 377 public DiffBuffer(BufferedReader rb) 378 { 379 this(rb, ""); 380 } 381 382 public DiffBuffer(BufferedReader rb, String name) 383 { 384 this (rb, name, 1024); 385 } 386 387 public DiffBuffer(BufferedReader rb, String name, int size) 388 { 389 super(size); 390 readBuffer = rb; 391 currentLowWater = 0; 392 currentHighWater = -1; 393 oldLow = 0; 394 myName = name; 395 atEnd = false; 396 } 397 398 public boolean isValidOffset(int lineNumber) throws IOException 399 { 400 if (atEnd) 401 { 402 return lineNumber <= actualEndOfFile; 403 } 404 405 if (lineAt(lineNumber) == null) 406 { 407 return false; 408 } 409 return true; 410 } 411 412 public String lineAt(int offset) throws IOException 413 { 414 419 if (offset > currentHighWater) 420 { 421 for (int i = 0; i < offset - currentHighWater; i++) 422 { 423 String aLine = readBuffer.readLine(); 424 addElement(aLine); 425 428 if (aLine == null) 429 { 430 if (!atEnd) 431 { 432 actualEndOfFile = currentHighWater + i; 434 atEnd = true; 436 } 437 } 438 } 439 currentHighWater = offset; 440 } 441 return (String ) elementAt(offset); 442 } 443 444 445 public final String EMPTY = null; 446 protected BufferedReader readBuffer; 447 protected int currentLowWater; 448 protected int currentHighWater; 449 private int oldLow; 450 protected String myName; 451 protected boolean atEnd; 452 protected int actualEndOfFile; 453 456 public void setLowWater(int newLow) 457 { 458 459 for (int i = oldLow; i < newLow; i++) 460 { 461 setElementAt(EMPTY, i); 462 } 463 currentLowWater = newLow; 464 oldLow = newLow -1; 465 } 466 467 468 public void iterate(boolean verbose) 469 { 470 int nulls = 0; 471 int nonnulls = 0; 472 473 474 for (int i = 0; i < this.size(); i++) 475 { 476 if (elementAt(i) == null) 477 { 478 if (verbose) 479 { 480 System.out.print("[" + i + "] "); 481 System.out.println("null"); 482 } 483 nulls++; 484 485 } 486 else 487 { 488 if (verbose) 489 { 490 System.out.print("[" + i + "] "); 491 System.out.println("NotNULL"); 492 } 493 nonnulls++; 494 } 495 } 496 System.out.println("nulls: " + nulls + " nonNull: " + nonnulls); 497 } 498 499 public void close() throws IOException 500 { 501 readBuffer.close(); 503 readBuffer = null; 504 } 505 } 506 507 } 508 | Popular Tags |