Package com.ibm.icu.util
Class ByteArrayWrapper
- java.lang.Object
-
- com.ibm.icu.util.ByteArrayWrapper
-
- All Implemented Interfaces:
java.lang.Comparable<ByteArrayWrapper>
- Direct Known Subclasses:
RawCollationKey
public class ByteArrayWrapper extends java.lang.Object implements java.lang.Comparable<ByteArrayWrapper>
A simple utility class to wrap a byte array.Generally passed as an argument object into a method. The method takes responsibility of writing into the internal byte array and increasing its size when necessary.
-
-
Constructor Summary
Constructors Constructor Description ByteArrayWrapper()
Construct a new ByteArrayWrapper with no data.ByteArrayWrapper(byte[] bytesToAdopt, int size)
Construct a new ByteArrayWrapper from a byte array and sizeByteArrayWrapper(java.nio.ByteBuffer source)
Construct a new ByteArrayWrapper from the contents of a ByteBuffer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ByteArrayWrapper
append(byte[] src, int start, int limit)
Appends the internal byte array from offset size with the contents of src from offset start to limit.int
compareTo(ByteArrayWrapper other)
Compare this object to another ByteArrayWrapper, which must not be null.private static void
copyBytes(byte[] src, int srcoff, byte[] tgt, int tgtoff, int length)
Copies the contents of src byte array from offset srcoff to the target of tgt byte array at the offset tgtoff.ByteArrayWrapper
ensureCapacity(int capacity)
Ensure that the internal byte array is at least of length capacity.boolean
equals(java.lang.Object other)
Return true if the bytes in each wrapper are equal.int
hashCode()
Return the hashcode.byte[]
releaseBytes()
Releases the internal byte array to the caller, resets the internal byte array to null and its size to 0.ByteArrayWrapper
set(byte[] src, int start, int limit)
Set the internal byte array from offset 0 to (limit - start) with the contents of src from offset start to limit.java.lang.String
toString()
Returns string value for debugging
-
-
-
Constructor Detail
-
ByteArrayWrapper
public ByteArrayWrapper()
Construct a new ByteArrayWrapper with no data.
-
ByteArrayWrapper
public ByteArrayWrapper(byte[] bytesToAdopt, int size)
Construct a new ByteArrayWrapper from a byte array and size- Parameters:
bytesToAdopt
- the byte array to adoptsize
- the length of valid data in the byte array- Throws:
java.lang.IndexOutOfBoundsException
- if bytesToAdopt == null and size != 0, or size < 0, or size > bytesToAdopt.length.
-
ByteArrayWrapper
public ByteArrayWrapper(java.nio.ByteBuffer source)
Construct a new ByteArrayWrapper from the contents of a ByteBuffer.- Parameters:
source
- the ByteBuffer from which to get the data.
-
-
Method Detail
-
ensureCapacity
public ByteArrayWrapper ensureCapacity(int capacity)
Ensure that the internal byte array is at least of length capacity. If the byte array is null or its length is less than capacity, a new byte array of length capacity will be allocated. The contents of the array (between 0 and size) remain unchanged.- Parameters:
capacity
- minimum length of internal byte array.- Returns:
- this ByteArrayWrapper
-
set
public final ByteArrayWrapper set(byte[] src, int start, int limit)
Set the internal byte array from offset 0 to (limit - start) with the contents of src from offset start to limit. If the byte array is null or its length is less than capacity, a new byte array of length (limit - start) will be allocated. This resets the size of the internal byte array to (limit - start).- Parameters:
src
- source byte array to copy fromstart
- start offset of src to copy fromlimit
- end + 1 offset of src to copy from- Returns:
- this ByteArrayWrapper
-
append
public final ByteArrayWrapper append(byte[] src, int start, int limit)
Appends the internal byte array from offset size with the contents of src from offset start to limit. This increases the size of the internal byte array to (size + limit - start).- Parameters:
src
- source byte array to copy fromstart
- start offset of src to copy fromlimit
- end + 1 offset of src to copy from- Returns:
- this ByteArrayWrapper
-
releaseBytes
public final byte[] releaseBytes()
Releases the internal byte array to the caller, resets the internal byte array to null and its size to 0.- Returns:
- internal byte array.
-
toString
public java.lang.String toString()
Returns string value for debugging- Overrides:
toString
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object other)
Return true if the bytes in each wrapper are equal.- Overrides:
equals
in classjava.lang.Object
- Parameters:
other
- the object to compare to.- Returns:
- true if the two objects are equal.
-
hashCode
public int hashCode()
Return the hashcode.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- the hashcode.
-
compareTo
public int compareTo(ByteArrayWrapper other)
Compare this object to another ByteArrayWrapper, which must not be null.- Specified by:
compareTo
in interfacejava.lang.Comparable<ByteArrayWrapper>
- Parameters:
other
- the object to compare to.- Returns:
- a value <0, 0, or >0 as this compares less than, equal to, or greater than other.
- Throws:
java.lang.ClassCastException
- if the other object is not a ByteArrayWrapper
-
copyBytes
private static final void copyBytes(byte[] src, int srcoff, byte[] tgt, int tgtoff, int length)
Copies the contents of src byte array from offset srcoff to the target of tgt byte array at the offset tgtoff.- Parameters:
src
- source byte array to copy fromsrcoff
- start offset of src to copy fromtgt
- target byte array to copy totgtoff
- start offset of tgt to copy tolength
- size of contents to copy
-
-