public class Bytes extends Object implements Comparable<Bytes>
It supports byte ordering (little/big endianness).
This class is immutable as long as the internal array is not changed from outside (which can't be assured, when
using using wrap()). It is possible to create a mutable version (see MutableBytes).
Example:
Bytes b = Bytes.from(array);
b.negate();
System.out.println(b.encodeHex());
| Modifier and Type | Method and Description |
|---|---|
static Bytes |
allocate(int length)
Creates a new instance with an empty array filled zeros.
|
static Bytes |
allocate(int length,
byte defaultValue)
Creates a new instance with an empty array filled with given defaultValue
|
Bytes |
and(byte[] secondArray)
Bitwise AND operation on the whole internal byte array.
|
Bytes |
and(Bytes bytes)
Bitwise AND operation on the whole internal byte array.
|
Bytes |
append(byte singleByte)
Creates a new instance with the current array appended with the provided data (ie.
|
Bytes |
append(byte[] secondArray)
Creates a new instance with the current array appended with the provided data (ie.
|
Bytes |
append(Bytes bytes)
Creates a new instance with the current array appended with the provided data (ie.
|
Bytes |
append(char char2Bytes)
Creates a new instance with the current array appended with the provided data (ie.
|
Bytes |
append(int integer4Bytes)
Creates a new instance with the current array appended with the provided data (ie.
|
Bytes |
append(long long8Bytes)
Creates a new instance with the current array appended with the provided data (ie.
|
Bytes |
append(short short2Bytes)
Creates a new instance with the current array appended with the provided data (ie.
|
byte[] |
array()
The reference of te internal byte-array.
|
BigInteger |
bigInteger()
The internal byte array wrapped in a
BigInteger instance. |
ByteBuffer |
buffer()
The internal byte array wrapped in a
ByteBuffer instance. |
byte |
byteAt(int index)
Returns the
byte value at the specified index. |
ByteOrder |
byteOrder()
Get the set byte order or endianness.
|
Bytes |
byteOrder(ByteOrder byteOrder)
Set the byte order or endianness of this instance.
|
int |
compareTo(Bytes o)
Compares this bytes instance to another.
|
Bytes |
copy()
Creates a new instance with a copy of the internal byte array and all other attributes.
|
Bytes |
copy(int offset,
int length)
Creates a new instance with a copy of the internal byte array and all other attributes.
|
int |
count(byte target)
Traverses the internal byte array counts the occurrences of given byte.
|
Bytes |
duplicate()
Create a new instance which shares the same underlying array
|
String |
encode(BinaryToTextEncoding.Encoder encoder)
Encode the internal byte-array with given encoder.
|
String |
encodeBase36()
Base36 (aka Hexatrigesimal) representation.
|
String |
encodeBase64()
Base64 representation with padding.
|
String |
encodeBinary()
Binary (aka "1" and "0") representation.
|
String |
encodeCharset(Charset charset)
String representation with given charset encoding
|
String |
encodeDec()
Decimal (0-9) representation.
|
String |
encodeHex()
Base16 or Hex representation in lowercase.
|
String |
encodeHex(boolean upperCase)
Base16 or Hex representation.
|
String |
encodeOctal()
Octal (0-7) representation.
|
String |
encodeUtf8()
UTF-8 representation of this byte array
|
double |
entropy()
Calculates the entropy of the internal byte array.
|
boolean |
equals(Object o) |
boolean |
equalsContent(byte[] array)
Checks only for internal array content
|
boolean |
equalsContent(Bytes other)
Checks only for internal array content
|
static Bytes |
from(BitSet set)
Creates a new instance from given BitSet.
|
static Bytes |
from(byte singleByte)
Creates a new single array element array instance from given byte
|
static Bytes |
from(byte[]... moreArrays)
Creates a new instance from given array of byte arrays
|
static Bytes |
from(byte[] byteArrayToCopy)
Creates a new instance from given collections of single bytes.
|
static Bytes |
from(byte[] array,
int offset,
int length)
Creates a new instance from a slice of given array
|
static Bytes |
from(ByteBuffer buffer)
Creates a new instance from given ByteBuffer.
|
static Bytes |
from(byte firstByte,
byte... moreBytes)
Creates a new instance from given collections of single bytes.
|
static Bytes |
from(char char2Byte)
Creates a new instance from given unsigned 2 byte char.
|
static Bytes |
from(Collection<Byte> bytesCollection)
Creates a new instance from given collections.
|
static Bytes |
from(InputStream stream)
Reads given input stream and creates a new instance from read data
|
static Bytes |
from(int integer4byte)
Creates a new instance from given 4 byte integer.
|
static Bytes |
from(long long8byte)
Creates a new instance from given 8 byte long.
|
static Bytes |
from(short short2Byte)
Creates a new instance from given 2 byte short.
|
static Bytes |
from(String utf8String)
Creates a new instance from given utf-8 encoded string
|
static Bytes |
from(String string,
Charset charset)
Creates a new instance from given string
|
int |
hashCode() |
int |
indexOf(byte target)
Returns the index of the first appearance of the value
target in
array. |
int |
indexOf(byte[] subArray)
Returns the start position of the first occurrence of the specified
target within array, or -1 if there is no such occurrence. |
InputStream |
inputStream()
Creates an input stream with the same backing data as the intern array of this instance
|
boolean |
isEmpty()
Checks the internal array for emptiness.
|
boolean |
isReadOnly()
Check if this instance is read only
|
int |
lastIndexOf(byte target)
Returns the index of the last appearance of the value
target in
array. |
Bytes |
leftShift(int shiftCount)
Bitwise left shifting of internal byte array.
|
int |
length()
The byte length of the underlying byte array.
|
int |
lengthBit()
The bit length of the underlying byte array.
|
MutableBytes |
mutable()
Returns a mutable version of this instance with sharing the same underlying byte-array.
|
Bytes |
negate()
Bitwise negate operation on the whole internal byte array.
|
Bytes |
or(byte[] secondArray)
Bitwise OR operation on the whole internal byte array.
|
Bytes |
or(Bytes bytes)
Bitwise OR operation on the whole internal byte array.
|
static Bytes |
parse(String encoded,
BinaryToTextEncoding.Decoder decoder)
Parsing of arbitrary encoded format
|
static Bytes |
parseBase36(String base36String)
Parsing of base36 encoded byte arrays.
|
static Bytes |
parseBase64(String base64String)
Parsing of base64 encoded byte arrays.
|
static Bytes |
parseDec(String decString)
Parsing of decimal encoded byte arrays.
|
static Bytes |
parseHex(String hexString)
Parsing of base16/HEX encoded byte arrays.
|
static Bytes |
parseOctal(String octalString)
Parsing of octal encoded byte arrays.
|
static Bytes |
random(int length)
A new instance with random bytes.
|
static Bytes |
random(int length,
Random random)
A new instance with random bytes.
|
Bytes |
readOnly()
Returns a new read-only byte instance.
|
Bytes |
resize(int newByteLength)
Copies the specified array, truncating or padding with zeros (if necessary)
so the copy has the specified length.
|
Bytes |
reverse()
Reverses the internal byte array.
|
Bytes |
rightShift(int shiftCount)
Bitwise right shifting of internal byte array.
|
Bytes |
shuffle()
Shuffles the internal byte array with a new
SecureRandom instance. |
Bytes |
shuffle(Random random)
Shuffles the internal byte array
|
Bytes |
sort()
Sorts the internal byte array with it's natural ordering
|
Bytes |
sort(Comparator<Byte> comparator)
Sorts the internal byte array according to given comparator.
|
BitSet |
toBitSet()
Returns a copy of the internal byte-array as
BitSet type |
byte |
toByte()
If the underlying byte array is smaller than 1 byte / 8 bit returns unsigned two-complement
representation for a Java byte value.
|
char |
toChar()
If the underlying byte array is smaller than 2 byte / 16 bit returns unsigned two-complement
representation for a Java char integer value.
|
int |
toInt()
If the underlying byte array is smaller than 4 byte / 32 bit returns signed two-complement
representation for a Java signed integer value.
|
List<Byte> |
toList()
Returns a copy of the internal byte-array as
List collection type
This requires a time and space complexity of O(n). |
long |
toLong()
If the underlying byte array is smaller than 8 byte / 64 bit returns signed two-complement
representation for a Java signed long integer value.
|
Byte[] |
toObjectArray()
Returns a copy of the internal byte-array as boxed primitive array.
|
short |
toShort()
If the underlying byte array is smaller than 2 byte / 16 bit returns signed two-complement
representation for a Java short integer value.
|
String |
toString()
A memory safe toString implementation, which only shows the byte length and at most 8 bytes preview in hex
representation.
|
Bytes |
transform(BytesTransformer transformer)
Generic transformation of this instance.
|
static Bytes |
wrap(byte[] array)
Creates a new instance with given byte array.
|
static Bytes |
wrap(byte[] array,
ByteOrder byteOrder)
Creates a new instance with given byte array.
|
static Bytes |
wrap(Bytes bytes)
Creates a new reference backed by the same byte array.
|
Bytes |
xor(byte[] secondArray)
Bitwise XOR operation on the whole internal byte array.
|
Bytes |
xor(Bytes bytes)
Bitwise XOR operation on the whole internal byte array.
|
public static Bytes allocate(int length)
length - of the internal arraypublic static Bytes allocate(int length, byte defaultValue)
length - of the internal arraydefaultValue - to fill withpublic static Bytes wrap(Bytes bytes)
bytes - to use as templatepublic static Bytes wrap(byte[] array)
The new instance will be backed by the given byte array; that is, modifications to the bytes will cause the array to be modified and vice versa.
array - to use directlypublic static Bytes wrap(byte[] array, ByteOrder byteOrder)
The new instance will be backed by the given byte array; that is, modifications to the bytes will cause the array to be modified and vice versa.
array - to use directlybyteOrder - the byte order of passed arraypublic static Bytes from(byte[] array, int offset, int length)
array - to sliceoffset - stat positionlength - lengthpublic static Bytes from(byte[]... moreArrays)
moreArrays - must not be nullpublic static Bytes from(Collection<Byte> bytesCollection)
bytesCollection - to create frompublic static Bytes from(byte singleByte)
singleByte - to create frompublic static Bytes from(byte[] byteArrayToCopy)
byteArrayToCopy - must not be null and will not be used directly, but a copypublic static Bytes from(byte firstByte, byte... moreBytes)
firstByte - must not be null and will not be used directly, but a copymoreBytes - more bytes varargpublic static Bytes from(char char2Byte)
char2Byte - to create frompublic static Bytes from(short short2Byte)
short2Byte - to create frompublic static Bytes from(int integer4byte)
integer4byte - to create frompublic static Bytes from(long long8byte)
long8byte - to create frompublic static Bytes from(ByteBuffer buffer)
buffer - to get the byte array frompublic static Bytes from(BitSet set)
set - to get the byte array frompublic static Bytes from(InputStream stream)
stream - to read frompublic static Bytes from(String utf8String)
utf8String - to get the internal byte array frompublic static Bytes from(String string, Charset charset)
string - to get the internal byte array fromcharset - used to decode the stringpublic static Bytes parseOctal(String octalString)
octalString - the encoded stringpublic static Bytes parseDec(String decString)
decString - the encoded stringpublic static Bytes parseHex(String hexString)
hexString - the encoded stringpublic static Bytes parseBase36(String base36String)
base36String - the encoded stringpublic static Bytes parseBase64(String base64String)
base64String - the encoded stringpublic static Bytes parse(String encoded, BinaryToTextEncoding.Decoder decoder)
encoded - the encoded stringdecoder - the decoder used to decode the stringpublic static Bytes random(int length)
SecureRandom instance.length - desired array lengthpublic static Bytes random(int length, Random random)
length - desired array lengthrandom - to create the entropy for the random bytespublic Bytes append(Bytes bytes)
bytes - to appendpublic Bytes append(byte singleByte)
singleByte - to appendpublic Bytes append(char char2Bytes)
char2Bytes - to appendpublic Bytes append(short short2Bytes)
short2Bytes - to appendpublic Bytes append(int integer4Bytes)
integer4Bytes - to appendpublic Bytes append(long long8Bytes)
long8Bytes - to appendpublic Bytes append(byte[] secondArray)
secondArray - to appendpublic Bytes xor(Bytes bytes)
transform(BytesTransformer).bytes - must be of same length as this instancepublic Bytes xor(byte[] secondArray)
transform(BytesTransformer).secondArray - must be of same length as this instancepublic Bytes and(Bytes bytes)
transform(BytesTransformer).bytes - must be of same length as this instancepublic Bytes and(byte[] secondArray)
transform(BytesTransformer).secondArray - must be of same length as this instancepublic Bytes or(Bytes bytes)
transform(BytesTransformer).bytes - must be of same length as this instancepublic Bytes or(byte[] secondArray)
transform(BytesTransformer).secondArray - must be of same length as this instancepublic Bytes negate()
transform(BytesTransformer).public Bytes leftShift(int shiftCount)
See the considerations about possible in-place operation in transform(BytesTransformer).
shiftCount - how many bits (not bytes)public Bytes rightShift(int shiftCount)
See the considerations about possible in-place operation in transform(BytesTransformer).
shiftCount - how many bits (not bytes)public Bytes copy()
public Bytes copy(int offset, int length)
offset - starting position in the source arraylength - of the new instancepublic Bytes reverse()
See the considerations about possible in-place operation in transform(BytesTransformer).
public Bytes sort(Comparator<Byte> comparator)
See the considerations about possible in-place operation in transform(BytesTransformer).
comparator - to sort the bytespublic Bytes sort()
See the considerations about possible in-place operation in transform(BytesTransformer).
public Bytes shuffle(Random random)
See the considerations about possible in-place operation in transform(BytesTransformer).
random - used to create entropy for the shufflepublic Bytes shuffle()
SecureRandom instance.
See the considerations about possible in-place operation in transform(BytesTransformer).
public Bytes transform(BytesTransformer transformer)
This transformation might be done in-place (ie. without copying the internal array and overwriting its old state),
or on a copy of the internal data, depending on the type (e.g. MutableBytes) and if the operation can be done
in-place. Therefore the caller has to ensure that certain side-effects, which occur due to the changing of the internal
data, do not create bugs in his/her code. Usually immutability is prefered, but when handling many or big byte arrays,
mutability enables drastically better performance.
transformer - used to transform this instancepublic Bytes resize(int newByteLength)
(byte)0.
If if the internal array will be grown, zero bytes will be added on the left, keeping the value the same.
newByteLength - the length of the copy to be returnedpublic int length()
public int lengthBit()
public boolean isEmpty()
public ByteOrder byteOrder()
ByteOrder.BIG_ENDIAN.ByteOrder.BIG_ENDIAN or ByteOrder.LITTLE_ENDIANpublic boolean isReadOnly()
public int indexOf(byte target)
target in
array.target - a primitive byte valuei for which array[i] == target, or
-1 if no such index exists.public int indexOf(byte[] subArray)
target within array, or -1 if there is no such occurrence.
More formally, returns the lowest index i such that java.util.Arrays.copyOfRange(array, i, i + target.length) contains exactly
the same elements as target.
subArray - the array to search for as a sub-sequence of arrayi for which array[i] == target, or
-1 if no such index exists.public int lastIndexOf(byte target)
target in
array.target - a primitive byte valuei for which array[i] == target,
or -1 if no such index exists.public byte byteAt(int index)
byte value at the specified index.
An index ranges from 0 to length() - 1. The first char value of the sequence
is at index 0, the next at index 1, and so on, as for array indexing.index - the index of the byte value.byte value at the specified index of the underlying byte array.IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.public int count(byte target)
target - byte to countpublic double entropy()
This implementation requires O(n) time and space complexity.
public Bytes duplicate()
public Bytes byteOrder(ByteOrder byteOrder)
ByteOrder.BIG_ENDIAN.
This option is important for all encoding and conversation methods.
byteOrder - new byteOrderpublic Bytes readOnly()
public ByteBuffer buffer()
ByteBuffer instance.
Changes to it will be directly mirrored in this Bytes instance.
This will honor the set byteOrder().
ReadOnlyBufferException - if this is a read-only instancepublic BigInteger bigInteger()
BigInteger instance.
If the internal byte order is ByteOrder.LITTLE_ENDIAN, a copy of the internal
array will be reversed and used as backing array with the big integer. Otherwise the internal
array will be used directly.
ReadOnlyBufferException - if this is a read-only instancepublic MutableBytes mutable()
copy() first.MutableBytesReadOnlyBufferException - if this is a read-only instancepublic InputStream inputStream()
public byte[] array()
Modifications to this bytes's content will cause the returned array's content to be modified, and vice versa.
ReadOnlyBufferException - if this is a read-only instancepublic String encodeBinary()
Example: 10011100
public String encodeOctal()
Example: 1124517677707527755
public String encodeDec()
Example: 20992966904426477
public String encodeHex()
Example: 4a94fdff1eafed
public String encodeHex(boolean upperCase)
encodeHex().
Example: 4A94FDFF1EAFED
upperCase - if the output character should be in uppercasepublic String encodeBase36()
Example: 5qpdvuwjvu5
public String encodeBase64()
Example: SpT9/x6v7Q==
public String encodeUtf8()
public String encodeCharset(Charset charset)
charset - the charset the return will be encodedpublic String encode(BinaryToTextEncoding.Encoder encoder)
encoder - the encoder implementationpublic List<Byte> toList()
List collection type
This requires a time and space complexity of O(n).public Byte[] toObjectArray()
public BitSet toBitSet()
BitSet typepublic byte toByte()
UnsupportedOperationException - if byte array is longer than 1 bytepublic char toChar()
byteOrder().UnsupportedOperationException - if byte array is longer than 2 bytepublic short toShort()
byteOrder().UnsupportedOperationException - if byte array is longer than 2 bytepublic int toInt()
byteOrder().UnsupportedOperationException - if byte array is longer than 4 bytepublic long toLong()
byteOrder().UnsupportedOperationException - if byte array is longer than 8 bytepublic int compareTo(Bytes o)
Two byte bytes are compared by comparing their sequences of
remaining elements lexicographically, without regard to the starting
position of each sequence within its corresponding buffer.
Pairs of byte elements are compared as if by invoking
Byte.compare(byte, byte).
Uses ByteBuffer.compareTo(Object) internally.
compareTo in interface Comparable<Bytes>public boolean equalsContent(Bytes other)
other - to compare toArrays.equals(byte[], byte[]))public boolean equalsContent(byte[] array)
array - to compare toArrays.equals(byte[], byte[]))Copyright © 2017. All rights reserved.