From 43bad60ed812e5a5a4c706c6c58d62eaa06cd24c Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sun, 10 Aug 2008 01:46:26 -0700 Subject: [PATCH] Teach NB how to encode/decode an unsigned 16 bit integer 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 Signed-off-by: Robin Rosenberg --- org.spearce.jgit/src/org/spearce/jgit/util/NB.java | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) 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 3af293a8..c6176f80 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 -- 2.11.4.GIT