diff src/org/tmatesoft/hg/repo/HgWorkingCopyStatusCollector.java @ 219:d63583b47bfa

ArrayIndexOutOfBoundsException when file appended. Erroneous 'areTheSame' when trailing were deleted.
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 17 May 2011 05:43:09 +0200
parents 047b1dec7a04
children 26ad7827a62d
line wrap: on
line diff
--- 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();