Mercurial > jhg
comparison src/org/tmatesoft/hg/repo/HgWorkingCopyStatusCollector.java @ 295:981f9f50bb6c
Issue 11: Error log facility. SessionContext to share common facilities
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Fri, 16 Sep 2011 05:35:32 +0200 |
| parents | 32890bab7209 |
| children | fb74133d2025 |
comparison
equal
deleted
inserted
replaced
| 294:32890bab7209 | 295:981f9f50bb6c |
|---|---|
| 395 return !ioFailed && areTheSame(f, bac.toArray(), dataFile.getPath()); | 395 return !ioFailed && areTheSame(f, bac.toArray(), dataFile.getPath()); |
| 396 } | 396 } |
| 397 | 397 |
| 398 private boolean areTheSame(FileInfo f, final byte[] data, Path p) { | 398 private boolean areTheSame(FileInfo f, final byte[] data, Path p) { |
| 399 ReadableByteChannel is = null; | 399 ReadableByteChannel is = null; |
| 400 class Check implements ByteChannel { | |
| 401 final boolean debug = repo.getContext().getLog().isDebug(); | |
| 402 boolean sameSoFar = true; | |
| 403 int x = 0; | |
| 404 | |
| 405 public int write(ByteBuffer buffer) { | |
| 406 for (int i = buffer.remaining(); i > 0; i--, x++) { | |
| 407 if (x >= data.length /*file has been appended*/ || data[x] != buffer.get()) { | |
| 408 if (debug) { | |
| 409 byte[] xx = new byte[15]; | |
| 410 if (buffer.position() > 5) { | |
| 411 buffer.position(buffer.position() - 5); | |
| 412 } | |
| 413 buffer.get(xx, 0, min(xx.length, i)); | |
| 414 repo.getContext().getLog().debug(getClass(), "expected >>%s<< but got >>%s<<", new String(data, max(0, x - 4), min(data.length - x, 20)), new String(xx)); | |
| 415 } | |
| 416 sameSoFar = false; | |
| 417 break; | |
| 418 } | |
| 419 } | |
| 420 buffer.position(buffer.limit()); // mark as read | |
| 421 return buffer.limit(); | |
| 422 } | |
| 423 | |
| 424 public boolean sameSoFar() { | |
| 425 return sameSoFar; | |
| 426 } | |
| 427 public boolean ultimatelyTheSame() { | |
| 428 return sameSoFar && x == data.length; | |
| 429 } | |
| 430 }; | |
| 431 Check check = new Check(); | |
| 400 try { | 432 try { |
| 401 try { | 433 is = f.newInputChannel(); |
| 402 is = f.newInputChannel(); | 434 ByteBuffer fb = ByteBuffer.allocate(min(1 + data.length * 2 /*to fit couple of lines appended; never zero*/, 8192)); |
| 403 ByteBuffer fb = ByteBuffer.allocate(min(1 + data.length * 2 /*to fit couple of lines appended; never zero*/, 8192)); | 435 FilterByteChannel filters = new FilterByteChannel(check, repo.getFiltersFromWorkingDirToRepo(p)); |
| 404 class Check implements ByteChannel { | 436 while (is.read(fb) != -1 && check.sameSoFar()) { |
| 405 final boolean debug = false; // XXX may want to add global variable to allow clients to turn | 437 fb.flip(); |
| 406 boolean sameSoFar = true; | 438 filters.write(fb); |
| 407 int x = 0; | 439 fb.compact(); |
| 408 | 440 } |
| 409 public int write(ByteBuffer buffer) { | 441 return check.ultimatelyTheSame(); |
| 410 for (int i = buffer.remaining(); i > 0; i--, x++) { | 442 } catch (CancelledException ex) { |
| 411 if (x >= data.length /*file has been appended*/ || data[x] != buffer.get()) { | 443 repo.getContext().getLog().warn(getClass(), ex, "Unexpected cancellation"); |
| 412 if (debug) { | 444 return check.ultimatelyTheSame(); |
| 413 byte[] xx = new byte[15]; | 445 } catch (IOException ex) { |
| 414 if (buffer.position() > 5) { | 446 repo.getContext().getLog().warn(getClass(), ex, null); |
| 415 buffer.position(buffer.position() - 5); | 447 } finally { |
| 416 } | 448 if (is != null) { |
| 417 buffer.get(xx); | 449 try { |
| 418 System.out.print("expected >>" + new String(data, max(0, x - 4), 20) + "<< but got >>"); | |
| 419 System.out.println(new String(xx) + "<<"); | |
| 420 } | |
| 421 sameSoFar = false; | |
| 422 break; | |
| 423 } | |
| 424 } | |
| 425 buffer.position(buffer.limit()); // mark as read | |
| 426 return buffer.limit(); | |
| 427 } | |
| 428 | |
| 429 public boolean sameSoFar() { | |
| 430 return sameSoFar; | |
| 431 } | |
| 432 public boolean ultimatelyTheSame() { | |
| 433 return sameSoFar && x == data.length; | |
| 434 } | |
| 435 }; | |
| 436 Check check = new Check(); | |
| 437 FilterByteChannel filters = new FilterByteChannel(check, repo.getFiltersFromWorkingDirToRepo(p)); | |
| 438 while (is.read(fb) != -1 && check.sameSoFar()) { | |
| 439 fb.flip(); | |
| 440 filters.write(fb); | |
| 441 fb.compact(); | |
| 442 } | |
| 443 return check.ultimatelyTheSame(); | |
| 444 } catch (IOException ex) { | |
| 445 ex.printStackTrace(); // log warn | |
| 446 } finally { | |
| 447 if (is != null) { | |
| 448 is.close(); | 450 is.close(); |
| 449 } | 451 } catch (IOException ex) { |
| 450 } | 452 repo.getContext().getLog().info(getClass(), ex, null); |
| 451 } catch (/*TODO typed*/Exception ex) { | 453 } |
| 452 ex.printStackTrace(); | 454 } |
| 453 } | 455 } |
| 454 return false; | 456 return false; |
| 455 } | 457 } |
| 456 | 458 |
| 457 private static boolean todoCheckFlagsEqual(FileInfo f, HgManifest.Flags originalManifestFlags) { | 459 private static boolean todoCheckFlagsEqual(FileInfo f, HgManifest.Flags originalManifestFlags) { |
