Switch jgit library to the EDL (3-clause BSD)
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / util / NB.java
blob61877b87a1352445999f8721ef56447cc7d09a7d
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 4 bytes (network byte order) into signed value.
96 * @param intbuf
97 * buffer to acquire the 4 bytes of data from.
98 * @param offset
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;
108 r <<= 8;
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.
117 * @param intbuf
118 * buffer to acquire the 4 bytes of data from.
119 * @param offset
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);
128 low <<= 8;
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.
137 * @param intbuf
138 * buffer to acquire the 8 bytes of data from.
139 * @param offset
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).
153 * @param intbuf
154 * buffer to write the 4 bytes of data into.
155 * @param offset
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
158 * replaced.
159 * @param v
160 * the value to write.
162 public static void encodeInt32(final byte[] intbuf, final int offset, int v) {
163 intbuf[offset + 3] = (byte) v;
164 v >>>= 8;
166 intbuf[offset + 2] = (byte) v;
167 v >>>= 8;
169 intbuf[offset + 1] = (byte) v;
170 v >>>= 8;
172 intbuf[offset] = (byte) v;
175 private NB() {
176 // Don't create instances of a static only utility.