Package dev.enola.common
Class ByteSeq
java.lang.Object
dev.enola.common.ByteSeq
- All Implemented Interfaces:
Comparable<ByteSeq>
Immutable Sequence of Bytes, of variable (but obviously fixed) length.
Typically intended to be used for "small"(-ish) size, like binary IDs, hashes, cryptographic keys, and such things; do not use this for "very large BLOBs". The hashCode is cached.
The com.google.protobuf.ByteString is very similar - but we don't want to depend
on the ProtoBuf library JUST for having a type like this. Likewise,
com.google.crypto.tink.util.Bytes is similar.
This intentionally does not implement Iterable<Byte> to avoid boxing
overhead.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionasString()Return String from bytes decoded as UTF-8.static ByteSeq.Builderbuilder(int size) intbooleanstatic ByteSeqfrom(byte[] bytes) Create a ByteSeq from an array of bytes.static ByteSeqNew ByteSeq from a String, in UTF-8.static ByteSeqstatic ByteSeqbyteget(int index) inthashCode()booleanisEmpty()Returnstrueif the size is0,falseotherwise.static ByteSeqrandom(int size) intsize()byte[]toBytes()toString()Return debug information about this object.toUUID()
-
Field Details
-
EMPTY
-
-
Method Details
-
builder
-
from
Create a ByteSeq from an array of bytes. Prefer using the Builder instead of this, to avoid the implementation have to copy the array.- Parameters:
bytes- Bytes (which will be copied)- Returns:
- the ByteSeq
-
from
New ByteSeq from a String, in UTF-8. Inverse ofasString(). More efficient than (avoids 2nd re-copy) usingfrom(byte[])onString.getBytes(), and safer because it avoids accidentally using the (non-fixed) platform default charset. -
from
-
from
-
random
-
toBytes
-
size
-
get
-
toUUID
-
asString
Return String from bytes decoded as UTF-8. Inverse offrom(String). -
isEmpty
Returnstrueif the size is0,falseotherwise.- Returns:
- true if this is zero bytes long
-
toString
Return debug information about this object. -
hashCode
-
equals
-
compareTo
- Specified by:
compareToin interfaceComparable<ByteSeq>
-