git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Shawn O. Pearce <spearce@spearce.org>
Date
Aug 10, 2008, 08:46 UTC
Message-ID
<1218357986-19671-12-git-send-email-spearce@spearce.org>
In-Reply-To
<1218357986-19671-11-git-send-email-spearce@spearce.org>

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@spearce.org>
---
 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
Previous: Shawn O. PearceNext: Robin Rosenberg
Message 12 of 15 in “Misc. cleanups and improvements”
  1. 00/11 Misc. cleanups and improvementsShawn O. Pearce, Aug 10, 2008
  2. 01/11 Fix RawParseUtils.endOfParagraph to work on all corner casesShawn O. Pearce, Aug 10, 2008
  3. 02/11 Add test case for the RevCommit parsing codeShawn O. Pearce, Aug 10, 2008
  4. 03/11 Notify AbstractTreeIterator implementations of skipped tree entriesShawn O. Pearce, Aug 10, 2008
  5. 04/11 Allow AbstractTreeIterator subclasses to supply their own path arrayShawn O. Pearce, Aug 10, 2008
  6. 05/11 Allow WorkingTreeIterators to define their prefix path when createdShawn O. Pearce, Aug 10, 2008
  7. 06/11 Add getTree to TreeWalk for locating the current iterator instanceShawn O. Pearce, Aug 10, 2008
  8. 07/11 Allow WorkingTreeIterator to track last modified time for entriesShawn O. Pearce, Aug 10, 2008
  9. 08/11 Expose the current entry's length, last modified in WorkingTreeIteratorShawn O. Pearce, Aug 10, 2008
  10. 09/11 Expose idBuffer,idOffset in AbstractTreeIterator to applicationsShawn O. Pearce, Aug 10, 2008
  11. 10/11 Add a TreeWalk iterator implementation for IContainerShawn O. Pearce, Aug 10, 2008
  12. 11/11 Teach NB how to encode/decode an unsigned 16 bit integerShawn O. Pearce, Aug 10, 2008
  13. Robin RosenbergAug 13, 2008
  14. Shawn O. PearceAug 14, 2008
  15. Robin RosenbergAug 14, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.