changeset 34:51bc56c0addd

Static Nodeid creation methods to reuse NULL id
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 13 Jan 2011 04:03:13 +0100
parents 565ce0835674
children 6061aa826a9e
files src/com/tmate/hgkit/console/Bundle.java src/com/tmate/hgkit/ll/Nodeid.java
diffstat 2 files changed, 49 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/tmate/hgkit/console/Bundle.java	Thu Jan 13 03:42:14 2011 +0100
+++ b/src/com/tmate/hgkit/console/Bundle.java	Thu Jan 13 04:03:13 2011 +0100
@@ -20,23 +20,24 @@
 		File bundleFile = new File("/temp/hg/hg-bundle-a78c980749e3.tmp");
 		DataAccessProvider dap = new DataAccessProvider();
 		DataAccess da = dap.create(bundleFile);
-		while (!da.isEmpty()) {
-			int len = da.readInt();
-			while (len > 4) {
-				byte[] nb = new byte[20];
-				da.readBytes(nb, 0, 20);
-				Nodeid node = new Nodeid(nb, true);
-				da.readBytes(nb, 0, 20);
-				Nodeid p1 = new Nodeid(nb, true);
-				da.readBytes(nb, 0, 20);
-				Nodeid p2 = new Nodeid(nb, true);
-				da.readBytes(nb, 0, 20);
-				Nodeid cs = new Nodeid(nb, true);
-				da.skip(len - 84);
-				System.out.printf("%6d %s %s %s %s\n", len, node, p1, p2, cs);
-				len = da.isEmpty() ? 0 : da.readInt();
+		try {
+			while (!da.isEmpty()) {
+				int len = da.readInt();
+				while (len > 4) {
+					byte[] nb = new byte[80];
+					da.readBytes(nb, 0, 80);
+					Nodeid node = Nodeid.fromBinary(nb, 0);
+					Nodeid p1 = Nodeid.fromBinary(nb, 20);
+					Nodeid p2 = Nodeid.fromBinary(nb, 40);
+					Nodeid cs = Nodeid.fromBinary(nb, 60);
+					da.skip(len - 84);
+					System.out.printf("%6d %s %s %s %s\n", len, node, p1, p2, cs);
+					len = da.isEmpty() ? 0 : da.readInt();
+				}
+				System.out.println("Group done");
 			}
-			System.out.println("Group done");
+		} finally {
+			da.done();
 		}
 	}
 }
--- a/src/com/tmate/hgkit/ll/Nodeid.java	Thu Jan 13 03:42:14 2011 +0100
+++ b/src/com/tmate/hgkit/ll/Nodeid.java	Thu Jan 13 04:03:13 2011 +0100
@@ -55,12 +55,44 @@
 	
 	@Override
 	public String toString() {
+		// XXX may want to output just single 0 for the NULL id?
 		return toHexString(binaryData, 0, binaryData.length);
 	}
 
 	public String shortNotation() {
 		return toHexString(binaryData, 0, 6);
 	}
+	
+	public boolean isNull() {
+		if (this == NULL) {
+			return true;
+		}
+		for (int i = 0; i < 20; i++) {
+			if (this.binaryData[i] != 0) {
+				return false;
+			}
+		}
+		return true;
+	}
+
+	// primary difference with cons is handling of NULL id (this method returns constant)
+	// always makes a copy of an array passed
+	public static Nodeid fromBinary(byte[] binaryRepresentation, int offset) {
+		if (binaryRepresentation == null || binaryRepresentation.length - offset < 20) {
+			throw new IllegalArgumentException();
+		}
+		int i = 0;
+		while (i < 20 && binaryRepresentation[offset+i] == 0) i++;
+		if (i == 20) {
+			return NULL;
+		}
+		if (offset == 0 && binaryRepresentation.length == 20) {
+			return new Nodeid(binaryRepresentation, true);
+		}
+		byte[] b = new byte[20]; // create new instance if no other reasonable guesses possible
+		System.arraycopy(binaryRepresentation, offset, b, 0, 20);
+		return new Nodeid(b, false);
+	}
 
 	// binascii.unhexlify()
 	public static Nodeid fromAscii(byte[] asciiRepresentation, int offset, int length) {