org.apache.java.lang
Class Bytes

java.lang.Object
  |
  +--org.apache.java.lang.Bytes

public class Bytes
extends java.lang.Object

Static methods for managing byte arrays (all methods follow Big Endian order where most significant bits are in front).

Version:
$Id$
Author:
Stefano Mazzocchi

Field Summary
private static char[] hexDigits
           
 
Constructor Summary
Bytes()
           
 
Method Summary
static byte[] append(byte[] a, byte[] b)
          Appends two bytes array into one.
static byte[] append(byte[] a, byte[] b, byte[] c)
          Appends three bytes array into one.
static boolean areEqual(byte[] a, byte[] b)
          Compares two byte arrays for equality.
static byte[] copy(byte[] b, int pos)
          Gets the end of the byte array given.
static byte[] copy(byte[] b, int pos, int length)
          Gets a sub-set of the byte array given.
static void merge(byte[] src, byte[] dest)
          Merges a bytes array into another.
static void merge(byte[] src, byte[] dest, int pos)
          Merges a bytes array into another starting from the given position.
static void merge(byte[] src, byte[] dest, int pos, int length)
          Merges a bytes array into another starting from the given position.
static void merge(byte[] src, byte[] dest, int srcpos, int destpos, int length)
          Merges a bytes array into another starting from the given positions.
static byte[] toBytes(int n)
          Returns a 4-byte array built from an int.
static byte[] toBytes(int n, byte[] b)
          Build a 4-byte array from an int.
static byte[] toBytes(long n)
          Returns a 8-byte array built from a long.
static byte[] toBytes(long n, byte[] b)
          Build a 8-byte array from a long.
static int toInt(byte[] b)
          Build an int from first 4 bytes of the array.
static long toLong(byte[] b)
          Build a long from first 8 bytes of the array.
static java.lang.String toString(byte[] b)
          Returns a string of hexadecimal digits from a byte array.
static java.lang.String toString(byte[] b, int offset, int length)
          Returns a string of hexadecimal digits from a byte array, starting at offset and continuing for length bytes.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

hexDigits

private static final char[] hexDigits
Constructor Detail

Bytes

public Bytes()
Method Detail

append

public static byte[] append(byte[] a,
                            byte[] b)
Appends two bytes array into one.
Parameters:
a - A byte[].
b - A byte[].
Returns:
A byte[].

append

public static byte[] append(byte[] a,
                            byte[] b,
                            byte[] c)
Appends three bytes array into one.
Parameters:
a - A byte[].
b - A byte[].
c - A byte[].
Returns:
A byte[].

areEqual

public static boolean areEqual(byte[] a,
                               byte[] b)
Compares two byte arrays for equality.
Parameters:
a - A byte[].
b - A byte[].
Returns:
True if the arrays have identical contents.

copy

public static byte[] copy(byte[] b,
                          int pos)
Gets the end of the byte array given.
Parameters:
b - A byte[].
pos - The position from which to start.
Returns:
A byte[] consisting of the portion of b between pos and the end of b.

copy

public static byte[] copy(byte[] b,
                          int pos,
                          int length)
Gets a sub-set of the byte array given.
Parameters:
b - A byte[].
pos - The position from which to start.
length - The number of bytes to copy from the original byte array to the new one.
Returns:
A byte[] consisting of the portion of b starting at pos and continuing for length bytes, or until the end of b is reached, which ever occurs first.

merge

public static void merge(byte[] src,
                         byte[] dest)
Merges a bytes array into another.
Parameters:
src - A byte[].
dest - A byte[].

merge

public static void merge(byte[] src,
                         byte[] dest,
                         int pos)
Merges a bytes array into another starting from the given position.
Parameters:
src - A byte[].
dest - A byte[].
pos - The position from which to start.

merge

public static void merge(byte[] src,
                         byte[] dest,
                         int pos,
                         int length)
Merges a bytes array into another starting from the given position.
Parameters:
src - A byte[].
dest - A byte[].
pos - The position from which to start.
length - The number of bytes to merge.

merge

public static void merge(byte[] src,
                         byte[] dest,
                         int srcpos,
                         int destpos,
                         int length)
Merges a bytes array into another starting from the given positions.
Parameters:
src - A byte[].
dest - A byte[].
srcpos - The position from which to start in src.
destpos - The position from which to start in dest.
length - The number of bytes to merge.

toBytes

public static byte[] toBytes(int n)
Returns a 4-byte array built from an int.
Parameters:
n - The number to convert.
Returns:
A byte[].

toBytes

public static byte[] toBytes(int n,
                             byte[] b)
Build a 4-byte array from an int. No check is performed on the array length.
Parameters:
n - The number to convert.
b - The array to fill.
Returns:
A byte[].

toBytes

public static byte[] toBytes(long n)
Returns a 8-byte array built from a long.
Parameters:
n - The number to convert.
Returns:
A byte[].

toBytes

public static byte[] toBytes(long n,
                             byte[] b)
Build a 8-byte array from a long. No check is performed on the array length.
Parameters:
n - The number to convert.
b - The array to fill.
Returns:
A byte[].

toInt

public static int toInt(byte[] b)
Build an int from first 4 bytes of the array.
Parameters:
b - The byte[] to convert.
Returns:
An int.

toLong

public static long toLong(byte[] b)
Build a long from first 8 bytes of the array.
Parameters:
b - The byte[] to convert.
Returns:
A long.

toString

public static java.lang.String toString(byte[] b)
Returns a string of hexadecimal digits from a byte array.
Parameters:
b - The byte[] to convert.
Returns:
A String.

toString

public static java.lang.String toString(byte[] b,
                                        int offset,
                                        int length)
Returns a string of hexadecimal digits from a byte array, starting at offset and continuing for length bytes.
Parameters:
b - The byte[] to convert.
offset - An int.
length - An int.
Returns:
A String.


Copyright © 1999-2001 Apache Software Foundation. All Rights Reserved.