Fix NB.decodeUInt16 to correctly handle the high byte
[egit/graphgui.git] / org.spearce.jgit / src / org / spearce / jgit / util / NB.java
blobfa13354c9085640254f4f90f904cc82d51f0be68
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.util;
40 import java.io.EOFException;
41 import java.io.IOException;
42 import java.io.InputStream;
44 /** Conversion utilities for network byte order handling. */
45 public final class NB {
46 /**
47 * Read the entire byte array into memory, or throw an exception.
49 * @param fd
50 * input stream to read the data from.
51 * @param dst
52 * buffer that must be fully populated, [off, off+len).
53 * @param off
54 * position within the buffer to start writing to.
55 * @param len
56 * number of bytes that must be read.
57 * @throws EOFException
58 * the stream ended before dst was fully populated.
59 * @throws IOException
60 * there was an error reading from the stream.
62 public static void readFully(final InputStream fd, final byte[] dst,
63 int off, int len) throws IOException {
64 while (len > 0) {
65 final int r = fd.read(dst, off, len);
66 if (r <= 0)
67 throw new EOFException("Short read of block.");
68 off += r;
69 len -= r;
73 /**
74 * Compare a 32 bit unsigned integer stored in a 32 bit signed integer.
75 * <p>
76 * This function performs an unsigned compare operation, even though Java
77 * does not natively support unsigned integer values. Negative numbers are
78 * treated as larger than positive ones.
80 * @param a
81 * the first value to compare.
82 * @param b
83 * the second value to compare.
84 * @return < 0 if a < b; 0 if a == b; > 0 if a > b.
86 public static int compareUInt32(final int a, final int b) {
87 final int cmp = (a >>> 1) - (b >>> 1);
88 if (cmp != 0)
89 return cmp;
90 return (a & 1) - (b & 1);
93 /**
94 * Convert sequence of 2 bytes (network byte order) into unsigned value.
96 * @param intbuf
97 * buffer to acquire the 2 bytes of data from.
98 * @param offset
99 * position within the buffer to begin reading from. This
100 * position and the next byte after it (for a total of 2 bytes)
101 * will be read.
102 * @return unsigned integer value that matches the 16 bits read.
104 public static int decodeUInt16(final byte[] intbuf, final int offset) {
105 int r = (intbuf[offset] & 0xff) << 8;
106 return r | (intbuf[offset + 1] & 0xff);
110 * Convert sequence of 4 bytes (network byte order) into signed value.
112 * @param intbuf
113 * buffer to acquire the 4 bytes of data from.
114 * @param offset
115 * position within the buffer to begin reading from. This
116 * position and the next 3 bytes after it (for a total of 4
117 * bytes) will be read.
118 * @return signed integer value that matches the 32 bits read.
120 public static int decodeInt32(final byte[] intbuf, final int offset) {
121 int r = intbuf[offset] << 8;
123 r |= intbuf[offset + 1] & 0xff;
124 r <<= 8;
126 r |= intbuf[offset + 2] & 0xff;
127 return (r << 8) | (intbuf[offset + 3] & 0xff);
131 * Convert sequence of 4 bytes (network byte order) into unsigned value.
133 * @param intbuf
134 * buffer to acquire the 4 bytes of data from.
135 * @param offset
136 * position within the buffer to begin reading from. This
137 * position and the next 3 bytes after it (for a total of 4
138 * bytes) will be read.
139 * @return unsigned integer value that matches the 32 bits read.
141 public static long decodeUInt32(final byte[] intbuf, final int offset) {
142 int low = (intbuf[offset + 1] & 0xff) << 8;
143 low |= (intbuf[offset + 2] & 0xff);
144 low <<= 8;
146 low |= (intbuf[offset + 3] & 0xff);
147 return ((long) (intbuf[offset] & 0xff)) << 24 | low;
151 * Convert sequence of 8 bytes (network byte order) into unsigned value.
153 * @param intbuf
154 * buffer to acquire the 8 bytes of data from.
155 * @param offset
156 * position within the buffer to begin reading from. This
157 * position and the next 7 bytes after it (for a total of 8
158 * bytes) will be read.
159 * @return unsigned integer value that matches the 64 bits read.
161 public static long decodeUInt64(final byte[] intbuf, final int offset) {
162 return (decodeUInt32(intbuf, offset) << 32)
163 | decodeUInt32(intbuf, offset + 4);
167 * Write a 16 bit integer as a sequence of 2 bytes (network byte order).
169 * @param intbuf
170 * buffer to write the 2 bytes of data into.
171 * @param offset
172 * position within the buffer to begin writing to. This position
173 * and the next byte after it (for a total of 2 bytes) will be
174 * replaced.
175 * @param v
176 * the value to write.
178 public static void encodeInt16(final byte[] intbuf, final int offset, int v) {
179 intbuf[offset + 1] = (byte) v;
180 v >>>= 8;
182 intbuf[offset] = (byte) v;
186 * Write a 32 bit integer as a sequence of 4 bytes (network byte order).
188 * @param intbuf
189 * buffer to write the 4 bytes of data into.
190 * @param offset
191 * position within the buffer to begin writing to. This position
192 * and the next 3 bytes after it (for a total of 4 bytes) will be
193 * replaced.
194 * @param v
195 * the value to write.
197 public static void encodeInt32(final byte[] intbuf, final int offset, int v) {
198 intbuf[offset + 3] = (byte) v;
199 v >>>= 8;
201 intbuf[offset + 2] = (byte) v;
202 v >>>= 8;
204 intbuf[offset + 1] = (byte) v;
205 v >>>= 8;
207 intbuf[offset] = (byte) v;
211 * Write a 64 bit integer as a sequence of 8 bytes (network byte order).
213 * @param intbuf
214 * buffer to write the 48bytes of data into.
215 * @param offset
216 * position within the buffer to begin writing to. This position
217 * and the next 7 bytes after it (for a total of 8 bytes) will be
218 * replaced.
219 * @param v
220 * the value to write.
222 public static void encodeInt64(final byte[] intbuf, final int offset, long v) {
223 intbuf[offset + 7] = (byte) v;
224 v >>>= 8;
226 intbuf[offset + 6] = (byte) v;
227 v >>>= 8;
229 intbuf[offset + 5] = (byte) v;
230 v >>>= 8;
232 intbuf[offset + 4] = (byte) v;
233 v >>>= 8;
235 intbuf[offset + 3] = (byte) v;
236 v >>>= 8;
238 intbuf[offset + 2] = (byte) v;
239 v >>>= 8;
241 intbuf[offset + 1] = (byte) v;
242 v >>>= 8;
244 intbuf[offset] = (byte) v;
247 private NB() {
248 // Don't create instances of a static only utility.