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 * Compare a 32 bit unsigned integer stored in a 32 bit signed integer.
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.
81 * the first value to compare.
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);
90 return (a
& 1) - (b
& 1);
94 * Convert sequence of 4 bytes (network byte order) into signed value.
97 * buffer to acquire the 4 bytes of data from.
99 * position within the buffer to begin reading from. This
100 * position and the next 3 bytes after it (for a total of 4
101 * bytes) will be read.
102 * @return signed integer value that matches the 32 bits read.
104 public static int decodeInt32(final byte[] intbuf
, final int offset
) {
105 int r
= intbuf
[offset
] << 8;
107 r
|= intbuf
[offset
+ 1] & 0xff;
110 r
|= intbuf
[offset
+ 2] & 0xff;
111 return (r
<< 8) | (intbuf
[offset
+ 3] & 0xff);
115 * Convert sequence of 4 bytes (network byte order) into unsigned value.
118 * buffer to acquire the 4 bytes of data from.
120 * position within the buffer to begin reading from. This
121 * position and the next 3 bytes after it (for a total of 4
122 * bytes) will be read.
123 * @return unsigned integer value that matches the 32 bits read.
125 public static long decodeUInt32(final byte[] intbuf
, final int offset
) {
126 int low
= (intbuf
[offset
+ 1] & 0xff) << 8;
127 low
|= (intbuf
[offset
+ 2] & 0xff);
130 low
|= (intbuf
[offset
+ 3] & 0xff);
131 return ((long) (intbuf
[offset
] & 0xff)) << 24 | low
;
135 * Convert sequence of 8 bytes (network byte order) into unsigned value.
138 * buffer to acquire the 8 bytes of data from.
140 * position within the buffer to begin reading from. This
141 * position and the next 7 bytes after it (for a total of 8
142 * bytes) will be read.
143 * @return unsigned integer value that matches the 64 bits read.
145 public static long decodeUInt64(final byte[] intbuf
, final int offset
) {
146 return (decodeUInt32(intbuf
, offset
) << 32)
147 | decodeUInt32(intbuf
, offset
+ 4);
151 * Write a 32 bit integer as a sequence of 4 bytes (network byte order).
154 * buffer to write the 4 bytes of data into.
156 * position within the buffer to begin writing to. This position
157 * and the next 3 bytes after it (for a total of 4 bytes) will be
160 * the value to write.
162 public static void encodeInt32(final byte[] intbuf
, final int offset
, int v
) {
163 intbuf
[offset
+ 3] = (byte) v
;
166 intbuf
[offset
+ 2] = (byte) v
;
169 intbuf
[offset
+ 1] = (byte) v
;
172 intbuf
[offset
] = (byte) v
;
176 // Don't create instances of a static only utility.