2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
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
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
{
47 * Read the entire byte array into memory, or throw an exception.
50 * input stream to read the data from.
52 * buffer that must be fully populated, [off, off+len).
54 * position within the buffer to start writing to.
56 * number of bytes that must be read.
57 * @throws EOFException
58 * the stream ended before dst was fully populated.
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
{
65 final int r
= fd
.read(dst
, off
, len
);
67 throw new EOFException("Short read of block.");
74 * Skip an entire region of an input stream.
76 * The input stream's position is moved forward by the number of requested
77 * bytes, discarding them from the input. This method does not return until
78 * the exact number of bytes requested has been skipped.
81 * the stream to skip bytes from.
83 * total number of bytes to be discarded. Must be >= 0.
84 * @throws EOFException
85 * the stream ended before the requested number of bytes were
88 * there was an error reading from the stream.
90 public static void skipFully(final InputStream fd
, long toSkip
)
93 final long r
= fd
.skip(toSkip
);
95 throw new EOFException("Short skip of block");
101 * Compare a 32 bit unsigned integer stored in a 32 bit signed integer.
103 * This function performs an unsigned compare operation, even though Java
104 * does not natively support unsigned integer values. Negative numbers are
105 * treated as larger than positive ones.
108 * the first value to compare.
110 * the second value to compare.
111 * @return < 0 if a < b; 0 if a == b; > 0 if a > b.
113 public static int compareUInt32(final int a
, final int b
) {
114 final int cmp
= (a
>>> 1) - (b
>>> 1);
117 return (a
& 1) - (b
& 1);
121 * Convert sequence of 2 bytes (network byte order) into unsigned value.
124 * buffer to acquire the 2 bytes of data from.
126 * position within the buffer to begin reading from. This
127 * position and the next byte after it (for a total of 2 bytes)
129 * @return unsigned integer value that matches the 16 bits read.
131 public static int decodeUInt16(final byte[] intbuf
, final int offset
) {
132 int r
= (intbuf
[offset
] & 0xff) << 8;
133 return r
| (intbuf
[offset
+ 1] & 0xff);
137 * Convert sequence of 4 bytes (network byte order) into signed value.
140 * buffer to acquire the 4 bytes of data from.
142 * position within the buffer to begin reading from. This
143 * position and the next 3 bytes after it (for a total of 4
144 * bytes) will be read.
145 * @return signed integer value that matches the 32 bits read.
147 public static int decodeInt32(final byte[] intbuf
, final int offset
) {
148 int r
= intbuf
[offset
] << 8;
150 r
|= intbuf
[offset
+ 1] & 0xff;
153 r
|= intbuf
[offset
+ 2] & 0xff;
154 return (r
<< 8) | (intbuf
[offset
+ 3] & 0xff);
158 * Convert sequence of 4 bytes (network byte order) into unsigned value.
161 * buffer to acquire the 4 bytes of data from.
163 * position within the buffer to begin reading from. This
164 * position and the next 3 bytes after it (for a total of 4
165 * bytes) will be read.
166 * @return unsigned integer value that matches the 32 bits read.
168 public static long decodeUInt32(final byte[] intbuf
, final int offset
) {
169 int low
= (intbuf
[offset
+ 1] & 0xff) << 8;
170 low
|= (intbuf
[offset
+ 2] & 0xff);
173 low
|= (intbuf
[offset
+ 3] & 0xff);
174 return ((long) (intbuf
[offset
] & 0xff)) << 24 | low
;
178 * Convert sequence of 8 bytes (network byte order) into unsigned value.
181 * buffer to acquire the 8 bytes of data from.
183 * position within the buffer to begin reading from. This
184 * position and the next 7 bytes after it (for a total of 8
185 * bytes) will be read.
186 * @return unsigned integer value that matches the 64 bits read.
188 public static long decodeUInt64(final byte[] intbuf
, final int offset
) {
189 return (decodeUInt32(intbuf
, offset
) << 32)
190 | decodeUInt32(intbuf
, offset
+ 4);
194 * Write a 16 bit integer as a sequence of 2 bytes (network byte order).
197 * buffer to write the 2 bytes of data into.
199 * position within the buffer to begin writing to. This position
200 * and the next byte after it (for a total of 2 bytes) will be
203 * the value to write.
205 public static void encodeInt16(final byte[] intbuf
, final int offset
, int v
) {
206 intbuf
[offset
+ 1] = (byte) v
;
209 intbuf
[offset
] = (byte) v
;
213 * Write a 32 bit integer as a sequence of 4 bytes (network byte order).
216 * buffer to write the 4 bytes of data into.
218 * position within the buffer to begin writing to. This position
219 * and the next 3 bytes after it (for a total of 4 bytes) will be
222 * the value to write.
224 public static void encodeInt32(final byte[] intbuf
, final int offset
, int v
) {
225 intbuf
[offset
+ 3] = (byte) v
;
228 intbuf
[offset
+ 2] = (byte) v
;
231 intbuf
[offset
+ 1] = (byte) v
;
234 intbuf
[offset
] = (byte) v
;
238 * Write a 64 bit integer as a sequence of 8 bytes (network byte order).
241 * buffer to write the 48bytes of data into.
243 * position within the buffer to begin writing to. This position
244 * and the next 7 bytes after it (for a total of 8 bytes) will be
247 * the value to write.
249 public static void encodeInt64(final byte[] intbuf
, final int offset
, long v
) {
250 intbuf
[offset
+ 7] = (byte) v
;
253 intbuf
[offset
+ 6] = (byte) v
;
256 intbuf
[offset
+ 5] = (byte) v
;
259 intbuf
[offset
+ 4] = (byte) v
;
262 intbuf
[offset
+ 3] = (byte) v
;
265 intbuf
[offset
+ 2] = (byte) v
;
268 intbuf
[offset
+ 1] = (byte) v
;
271 intbuf
[offset
] = (byte) v
;
275 // Don't create instances of a static only utility.