- java.lang.Object
-
- io.github.mmm.binary.BinaryType
-
- All Implemented Interfaces:
Binary
,Streamable
- Direct Known Subclasses:
CryptoBinary
public class BinaryType extends Object implements Binary
Datatype for binary data or BLOB (binary large object).- Since:
- 1.0.0
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]
data
-
Fields inherited from interface io.github.mmm.binary.Binary
EMPTY_BYTE_ARRAY
-
-
Constructor Summary
Constructors Constructor Description BinaryType(byte[] data)
The constructor.BinaryType(String base64)
The constructor (mainly for testing).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description InputStream
asStream()
boolean
equals(Object obj)
String
format(BinaryCodec codec)
This method avoids anarray-copy
ofBinary.getData()
and is therefore more efficient than usingBinaryCodec
.encode
(Binary.getData()
).
ATTENTION:
Sub-types often represent sensible data.static String
formatBase64(byte[] data)
static String
formatHex(byte[] data)
byte[]
getData()
ATTENTION:
Sub-types often represent sensible data.void
getData(byte[] buffer, int offset)
Copies thedata
into the given buffer.byte
getDataByte(int index)
int
getLength()
protected int
getMaxLength()
protected int
getMinLength()
int
hashCode()
boolean
isZeros()
static byte[]
parseBase64(String base64)
static byte[]
parseHex(String hex)
void
save(OutputStream out)
static byte[]
toBytes(int value)
static byte[]
toBytes(long value)
static long
toInt(byte[] data)
static long
toLong(byte[] data)
String
toString()
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.github.mmm.binary.Binary
formatBase64, formatHex
-
-
-
-
Field Detail
-
data
protected final byte[] data
- See Also:
getData()
-
-
Method Detail
-
getMinLength
protected int getMinLength()
- Returns:
- the minimum allowed length of the
data
. Implementation has to be static and return the same value for each class with each call.
-
getMaxLength
protected int getMaxLength()
- Returns:
- the maximum allowed length of the
data
. Implementation has to be static and return the same value for each class with each call.
-
getDataByte
public byte getDataByte(int index)
- Specified by:
getDataByte
in interfaceBinary
- Parameters:
index
- the index of the requested byte starting from1
.- Returns:
- the requested byte. If the given index exceeds the
data
(is greater or equal tolength
)0
is returned.
-
getData
public byte[] getData()
Description copied from interface:Binary
ATTENTION:
Sub-types often represent sensible data. Be careful to pass these data in raw form (unencrypted).
-
getData
public void getData(byte[] buffer, int offset)
Description copied from interface:Binary
Copies thedata
into the given buffer.
-
getLength
public int getLength()
-
format
public String format(BinaryCodec codec)
Description copied from interface:Binary
This method avoids anarray-copy
ofBinary.getData()
and is therefore more efficient than usingBinaryCodec
.encode
(Binary.getData()
).
ATTENTION:
Sub-types often represent sensible data. Be careful to pass the data in raw form (unencrypted).- Specified by:
format
in interfaceBinary
- Parameters:
codec
- theBinaryCodec
.- Returns:
- the
encoded
data
.
-
isZeros
public boolean isZeros()
-
asStream
public InputStream asStream()
- Specified by:
asStream
in interfaceStreamable
- Returns:
- a new
InputStream
to read the underlying data.
-
save
public void save(OutputStream out)
- Specified by:
save
in interfaceStreamable
- Parameters:
out
- theOutputStream
towrite
the data to.
-
parseHex
public static byte[] parseHex(String hex)
- Parameters:
hex
- ahex representation
ofBLOB data
.- Returns:
- the parsed
hex
String.
-
formatHex
public static String formatHex(byte[] data)
-
parseBase64
public static byte[] parseBase64(String base64)
-
formatBase64
public static String formatBase64(byte[] data)
- Parameters:
data
- thedata
to format.- Returns:
- the given
data
formatted asBase64
encodedString
representation. - See Also:
Binary.formatBase64()
-
toBytes
public static byte[] toBytes(long value)
- Parameters:
value
- thelong
value.- Returns:
- the value as array of 8
byte
s.
-
toBytes
public static byte[] toBytes(int value)
- Parameters:
value
- theint
value.- Returns:
- the value as array of 4
byte
s.
-
toLong
public static long toLong(byte[] data)
- Parameters:
data
- the long value asbyte[]
with a maximum length of 8 (seetoBytes(long)
).- Returns:
- the value
long
.
-
toInt
public static long toInt(byte[] data)
- Parameters:
data
- the long value asbyte[]
with a maximum length of 8 (seetoBytes(long)
).- Returns:
- the value
long
.
-
-