# HG changeset patch # User Artem Tikhomirov # Date 1305603789 -7200 # Node ID d63583b47bfa66e315cf00bce872f9a274755d59 # Parent 047b1dec7a04cddcf991fad1b87ef19083e7ea95 ArrayIndexOutOfBoundsException when file appended. Erroneous 'areTheSame' when trailing were deleted. diff -r 047b1dec7a04 -r d63583b47bfa src/org/tmatesoft/hg/repo/HgWorkingCopyStatusCollector.java --- a/src/org/tmatesoft/hg/repo/HgWorkingCopyStatusCollector.java Tue May 17 03:42:33 2011 +0200 +++ b/src/org/tmatesoft/hg/repo/HgWorkingCopyStatusCollector.java Tue May 17 05:43:09 2011 +0200 @@ -289,14 +289,15 @@ try { fis = new FileInputStream(f); FileChannel fc = fis.getChannel(); - ByteBuffer fb = ByteBuffer.allocate(min(data.length, 8192)); - final boolean[] checkValue = new boolean[] { true }; - ByteChannel check = new ByteChannel() { + ByteBuffer fb = ByteBuffer.allocate(min(data.length * 2 /*to fit couple of lines appended*/, 8192)); + class Check implements ByteChannel { + final boolean debug = false; // XXX may want to add global variable to allow clients to turn + boolean sameSoFar = true; int x = 0; - final boolean debug = false; // XXX may want to add global variable to allow clients to turn + public int write(ByteBuffer buffer) { for (int i = buffer.remaining(); i > 0; i--, x++) { - if (data[x] != buffer.get()) { + if (x >= data.length /*file has been appended*/ || data[x] != buffer.get()) { if (debug) { byte[] xx = new byte[15]; if (buffer.position() > 5) { @@ -306,21 +307,29 @@ System.out.print("expected >>" + new String(data, max(0, x - 4), 20) + "<< but got >>"); System.out.println(new String(xx) + "<<"); } - checkValue[0] = false; + sameSoFar = false; break; } } buffer.position(buffer.limit()); // mark as read return buffer.limit(); } + + public boolean sameSoFar() { + return sameSoFar; + } + public boolean ultimatelyTheSame() { + return sameSoFar && x == data.length; + } }; + Check check = new Check(); FilterByteChannel filters = new FilterByteChannel(check, repo.getFiltersFromWorkingDirToRepo(p)); - while (fc.read(fb) != -1 && checkValue[0]) { + while (fc.read(fb) != -1 && check.sameSoFar()) { fb.flip(); filters.write(fb); fb.compact(); } - return checkValue[0]; + return check.ultimatelyTheSame(); } catch (IOException ex) { if (fis != null) { fis.close();