org.eclipse.jgit.util
Class NB

java.lang.Object
  extended by org.eclipse.jgit.util.NB

public final class NB
extends java.lang.Object

Conversion utilities for network byte order handling.


Method Summary
static int compareUInt32(int a, int b)
          Compare a 32 bit unsigned integer stored in a 32 bit signed integer.
static int decodeInt32(byte[] intbuf, int offset)
          Convert sequence of 4 bytes (network byte order) into signed value.
static int decodeUInt16(byte[] intbuf, int offset)
          Convert sequence of 2 bytes (network byte order) into unsigned value.
static long decodeUInt32(byte[] intbuf, int offset)
          Convert sequence of 4 bytes (network byte order) into unsigned value.
static long decodeUInt64(byte[] intbuf, int offset)
          Convert sequence of 8 bytes (network byte order) into unsigned value.
static void encodeInt16(byte[] intbuf, int offset, int v)
          Write a 16 bit integer as a sequence of 2 bytes (network byte order).
static void encodeInt32(byte[] intbuf, int offset, int v)
          Write a 32 bit integer as a sequence of 4 bytes (network byte order).
static void encodeInt64(byte[] intbuf, int offset, long v)
          Write a 64 bit integer as a sequence of 8 bytes (network byte order).
static byte[] readFully(java.io.File path)
          Read an entire local file into memory as a byte array.
static void readFully(java.nio.channels.FileChannel fd, long pos, byte[] dst, int off, int len)
          Read the entire byte array into memory, or throw an exception.
static byte[] readFully(java.io.File path, int max)
          Read an entire local file into memory as a byte array.
static void readFully(java.io.InputStream fd, byte[] dst, int off, int len)
          Read the entire byte array into memory, or throw an exception.
static void skipFully(java.io.InputStream fd, long toSkip)
          Skip an entire region of an input stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

readFully

public static final byte[] readFully(java.io.File path)
                              throws java.io.FileNotFoundException,
                                     java.io.IOException
Read an entire local file into memory as a byte array.

Parameters:
path - location of the file to read.
Returns:
complete contents of the requested local file.
Throws:
java.io.FileNotFoundException - the file does not exist.
java.io.IOException - the file exists, but its contents cannot be read.

readFully

public static final byte[] readFully(java.io.File path,
                                     int max)
                              throws java.io.FileNotFoundException,
                                     java.io.IOException
Read an entire local file into memory as a byte array.

Parameters:
path - location of the file to read.
max - maximum number of bytes to read, if the file is larger than this limit an IOException is thrown.
Returns:
complete contents of the requested local file.
Throws:
java.io.FileNotFoundException - the file does not exist.
java.io.IOException - the file exists, but its contents cannot be read.

readFully

public static void readFully(java.io.InputStream fd,
                             byte[] dst,
                             int off,
                             int len)
                      throws java.io.IOException
Read the entire byte array into memory, or throw an exception.

Parameters:
fd - input stream to read the data from.
dst - buffer that must be fully populated, [off, off+len).
off - position within the buffer to start writing to.
len - number of bytes that must be read.
Throws:
java.io.EOFException - the stream ended before dst was fully populated.
java.io.IOException - there was an error reading from the stream.

readFully

public static void readFully(java.nio.channels.FileChannel fd,
                             long pos,
                             byte[] dst,
                             int off,
                             int len)
                      throws java.io.IOException
Read the entire byte array into memory, or throw an exception.

Parameters:
fd - file to read the data from.
pos - position to read from the file at.
dst - buffer that must be fully populated, [off, off+len).
off - position within the buffer to start writing to.
len - number of bytes that must be read.
Throws:
java.io.EOFException - the stream ended before dst was fully populated.
java.io.IOException - there was an error reading from the stream.

skipFully

public static void skipFully(java.io.InputStream fd,
                             long toSkip)
                      throws java.io.IOException
Skip an entire region of an input stream.

The input stream's position is moved forward by the number of requested bytes, discarding them from the input. This method does not return until the exact number of bytes requested has been skipped.

Parameters:
fd - the stream to skip bytes from.
toSkip - total number of bytes to be discarded. Must be >= 0.
Throws:
java.io.EOFException - the stream ended before the requested number of bytes were skipped.
java.io.IOException - there was an error reading from the stream.

compareUInt32

public static int compareUInt32(int a,
                                int b)
Compare a 32 bit unsigned integer stored in a 32 bit signed integer.

This function performs an unsigned compare operation, even though Java does not natively support unsigned integer values. Negative numbers are treated as larger than positive ones.

Parameters:
a - the first value to compare.
b - the second value to compare.
Returns:
< 0 if a < b; 0 if a == b; > 0 if a > b.

decodeUInt16

public static int decodeUInt16(byte[] intbuf,
                               int offset)
Convert sequence of 2 bytes (network byte order) into unsigned value.

Parameters:
intbuf - buffer to acquire the 2 bytes of data from.
offset - position within the buffer to begin reading from. This position and the next byte after it (for a total of 2 bytes) will be read.
Returns:
unsigned integer value that matches the 16 bits read.

decodeInt32

public static int decodeInt32(byte[] intbuf,
                              int offset)
Convert sequence of 4 bytes (network byte order) into signed value.

Parameters:
intbuf - buffer to acquire the 4 bytes of data from.
offset - position within the buffer to begin reading from. This position and the next 3 bytes after it (for a total of 4 bytes) will be read.
Returns:
signed integer value that matches the 32 bits read.

decodeUInt32

public static long decodeUInt32(byte[] intbuf,
                                int offset)
Convert sequence of 4 bytes (network byte order) into unsigned value.

Parameters:
intbuf - buffer to acquire the 4 bytes of data from.
offset - position within the buffer to begin reading from. This position and the next 3 bytes after it (for a total of 4 bytes) will be read.
Returns:
unsigned integer value that matches the 32 bits read.

decodeUInt64

public static long decodeUInt64(byte[] intbuf,
                                int offset)
Convert sequence of 8 bytes (network byte order) into unsigned value.

Parameters:
intbuf - buffer to acquire the 8 bytes of data from.
offset - position within the buffer to begin reading from. This position and the next 7 bytes after it (for a total of 8 bytes) will be read.
Returns:
unsigned integer value that matches the 64 bits read.

encodeInt16

public static void encodeInt16(byte[] intbuf,
                               int offset,
                               int v)
Write a 16 bit integer as a sequence of 2 bytes (network byte order).

Parameters:
intbuf - buffer to write the 2 bytes of data into.
offset - position within the buffer to begin writing to. This position and the next byte after it (for a total of 2 bytes) will be replaced.
v - the value to write.

encodeInt32

public static void encodeInt32(byte[] intbuf,
                               int offset,
                               int v)
Write a 32 bit integer as a sequence of 4 bytes (network byte order).

Parameters:
intbuf - buffer to write the 4 bytes of data into.
offset - position within the buffer to begin writing to. This position and the next 3 bytes after it (for a total of 4 bytes) will be replaced.
v - the value to write.

encodeInt64

public static void encodeInt64(byte[] intbuf,
                               int offset,
                               long v)
Write a 64 bit integer as a sequence of 8 bytes (network byte order).

Parameters:
intbuf - buffer to write the 48bytes of data into.
offset - position within the buffer to begin writing to. This position and the next 7 bytes after it (for a total of 8 bytes) will be replaced.
v - the value to write.