[EGIT PATCH 11/11] Teach NB how to encode/decode an unsigned 16 bit integer

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The DIRC (aka index) file format in Git uses a 16 bit unsigned int
field in at least one of the members for a file record.

Signed-off-by: Shawn O. Pearce <spearce@xxxxxxxxxxx>
---
 org.spearce.jgit/src/org/spearce/jgit/util/NB.java |   35 ++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/NB.java b/org.spearce.jgit/src/org/spearce/jgit/util/NB.java
index 3af293a..ef21a4b 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/util/NB.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/util/NB.java
@@ -91,6 +91,22 @@ public final class NB {
 	}
 
 	/**
+	 * Convert sequence of 2 bytes (network byte order) into unsigned value.
+	 * 
+	 * @param intbuf
+	 *            buffer to acquire the 2 bytes of data from.
+	 * @param offset
+	 *            position within the buffer to begin reading from. This
+	 *            position and the next byte after it (for a total of 2 bytes)
+	 *            will be read.
+	 * @return unsigned integer value that matches the 16 bits read.
+	 */
+	public static int decodeUInt16(final byte[] intbuf, final int offset) {
+		int r = (intbuf[offset] << 8) & 0xff;
+		return r | (intbuf[offset + 1] & 0xff);
+	}
+
+	/**
 	 * Convert sequence of 4 bytes (network byte order) into signed value.
 	 * 
 	 * @param intbuf
@@ -148,6 +164,25 @@ public final class NB {
 	}
 
 	/**
+	 * Write a 16 bit integer as a sequence of 2 bytes (network byte order).
+	 * 
+	 * @param intbuf
+	 *            buffer to write the 2 bytes of data into.
+	 * @param offset
+	 *            position within the buffer to begin writing to. This position
+	 *            and the next byte after it (for a total of 2 bytes) will be
+	 *            replaced.
+	 * @param v
+	 *            the value to write.
+	 */
+	public static void encodeInt16(final byte[] intbuf, final int offset, int v) {
+		intbuf[offset + 1] = (byte) v;
+		v >>>= 8;
+
+		intbuf[offset] = (byte) v;
+	}
+
+	/**
 	 * Write a 32 bit integer as a sequence of 4 bytes (network byte order).
 	 * 
 	 * @param intbuf
-- 
1.6.0.rc2.219.g1250ab

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux