}
return areTheSame(f, bac.toArray(), dataFile.getPath());
}
private boolean areTheSame(FileInfo f, final byte[] data, Path p) throws HgInvalidFileException {
ReadableByteChannel is = null;
class Check implements ByteChannel {
final boolean debug = repo.getSessionContext().getLog().isDebug();
boolean sameSoFar = true;
int x = 0;
public int write(ByteBuffer buffer) {
for (int i = buffer.remaining(); i > 0; i--, x++) {
if (x >= data.length /*file has been appended*/ || data[x] != buffer.get()) {
if (debug) {
byte[] xx = new byte[15];
if (buffer.position() > 5) {
buffer.position(buffer.position() - 5);
}
buffer.get(xx, 0, min(xx.length, i-1 /*-1 for the one potentially read at buffer.get in if() */));
String exp;
if (x < data.length) {
exp = new String(data, max(0, x - 4), min(data.length - x, 20));
} else {
int offset = max(0, x - 4);
exp = new String(data, offset, min(data.length - offset, 20));
}
repo.getSessionContext().getLog().dump(getClass(), Debug, "expected >>%s<< but got >>%s<<", exp, new String(xx));
}
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();
try {
is = f.newInputChannel();
// ByteBuffer fb = ByteBuffer.allocate(min(1 + data.length * 2 /*to fit couple of lines appended; never zero*/, 8192));
ByteBuffer fb = ByteBuffer.allocate(8192); // FIXME temp fix to ensure big enough buffer for KeywordFilter
FilterByteChannel filters = new FilterByteChannel(check, repo.getFiltersFromWorkingDirToRepo(p));
Preview preview = Adaptable.Factory.getAdapter(filters, Preview.class, null);
if (preview != null) {
while (is.read(fb) != -1) {
fb.flip();
preview.preview(fb);
fb.clear();
}
// reset channel to read once again
try {
is.close();
} catch (IOException ex) {
repo.getSessionContext().getLog().dump(getClass(), Info, ex, null);
}
is = f.newInputChannel();
fb.clear();
}
while (is.read(fb) != -1 && check.sameSoFar()) {
fb.flip();
filters.write(fb);
fb.compact();
}
return check.ultimatelyTheSame();