Fix DirCache's skip over null byte padding when reading a DIRC file
[egit.git] / org.spearce.jgit / src / org / spearce / jgit / util / NB.java
blobc65c6fa1896c80ee9b2c2bb3eed84f5dbcd6c692
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 * Skip an entire region of an input stream.
75 * <p>
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.
80 * @param fd
81 * the stream to skip bytes from.
82 * @param toSkip
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
86 * skipped.
87 * @throws IOException
88 * there was an error reading from the stream.
90 public static void skipFully(final InputStream fd, long toSkip)
91 throws IOException {
92 while (toSkip > 0) {
93 final long r = fd.skip(toSkip);
94 if (r <= 0)
95 throw new EOFException("Short skip of block");
96 toSkip -= r;
101 * Compare a 32 bit unsigned integer stored in a 32 bit signed integer.
102 * <p>
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.
107 * @param a
108 * the first value to compare.
109 * @param b
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);
115 if (cmp != 0)
116 return cmp;
117 return (a & 1) - (b & 1);
121 * Convert sequence of 2 bytes (network byte order) into unsigned value.
123 * @param intbuf
124 * buffer to acquire the 2 bytes of data from.
125 * @param offset
126 * position within the buffer to begin reading from. This
127 * position and the next byte after it (for a total of 2 bytes)
128 * will be read.
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.
139 * @param intbuf
140 * buffer to acquire the 4 bytes of data from.
141 * @param offset
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;
151 r <<= 8;
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.
160 * @param intbuf
161 * buffer to acquire the 4 bytes of data from.
162 * @param offset
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);
171 low <<= 8;
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.
180 * @param intbuf
181 * buffer to acquire the 8 bytes of data from.
182 * @param offset
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).
196 * @param intbuf
197 * buffer to write the 2 bytes of data into.
198 * @param offset
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
201 * replaced.
202 * @param v
203 * the value to write.
205 public static void encodeInt16(final byte[] intbuf, final int offset, int v) {
206 intbuf[offset + 1] = (byte) v;
207 v >>>= 8;
209 intbuf[offset] = (byte) v;
213 * Write a 32 bit integer as a sequence of 4 bytes (network byte order).
215 * @param intbuf
216 * buffer to write the 4 bytes of data into.
217 * @param offset
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
220 * replaced.
221 * @param v
222 * the value to write.
224 public static void encodeInt32(final byte[] intbuf, final int offset, int v) {
225 intbuf[offset + 3] = (byte) v;
226 v >>>= 8;
228 intbuf[offset + 2] = (byte) v;
229 v >>>= 8;
231 intbuf[offset + 1] = (byte) v;
232 v >>>= 8;
234 intbuf[offset] = (byte) v;
238 * Write a 64 bit integer as a sequence of 8 bytes (network byte order).
240 * @param intbuf
241 * buffer to write the 48bytes of data into.
242 * @param offset
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
245 * replaced.
246 * @param v
247 * the value to write.
249 public static void encodeInt64(final byte[] intbuf, final int offset, long v) {
250 intbuf[offset + 7] = (byte) v;
251 v >>>= 8;
253 intbuf[offset + 6] = (byte) v;
254 v >>>= 8;
256 intbuf[offset + 5] = (byte) v;
257 v >>>= 8;
259 intbuf[offset + 4] = (byte) v;
260 v >>>= 8;
262 intbuf[offset + 3] = (byte) v;
263 v >>>= 8;
265 intbuf[offset + 2] = (byte) v;
266 v >>>= 8;
268 intbuf[offset + 1] = (byte) v;
269 v >>>= 8;
271 intbuf[offset] = (byte) v;
274 private NB() {
275 // Don't create instances of a static only utility.