- 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 InputStreamasStream()booleanequals(Object obj)Stringformat(BinaryCodec codec)This method avoids anarray-copyofBinary.getData()and is therefore more efficient than usingBinaryCodec.encode(Binary.getData()).
ATTENTION:
Sub-types often represent sensible data.static StringformatBase64(byte[] data)static StringformatHex(byte[] data)byte[]getData()ATTENTION:
Sub-types often represent sensible data.voidgetData(byte[] buffer, int offset)Copies thedatainto the given buffer.bytegetDataByte(int index)intgetLength()protected intgetMaxLength()protected intgetMinLength()inthashCode()booleanisZeros()static byte[]parseBase64(String base64)static byte[]parseHex(String hex)voidsave(OutputStream out)static byte[]toBytes(int value)static byte[]toBytes(long value)static longtoInt(byte[] data)static longtoLong(byte[] data)StringtoString()-
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:
getDataBytein 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)0is returned.
-
getData
public byte[] getData()
Description copied from interface:BinaryATTENTION:
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:BinaryCopies thedatainto the given buffer.
-
getLength
public int getLength()
-
format
public String format(BinaryCodec codec)
Description copied from interface:BinaryThis method avoids anarray-copyofBinary.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:
formatin interfaceBinary- Parameters:
codec- theBinaryCodec.- Returns:
- the
encodeddata.
-
isZeros
public boolean isZeros()
-
asStream
public InputStream asStream()
- Specified by:
asStreamin interfaceStreamable- Returns:
- a new
InputStreamto read the underlying data.
-
save
public void save(OutputStream out)
- Specified by:
savein interfaceStreamable- Parameters:
out- theOutputStreamtowritethe data to.
-
parseHex
public static byte[] parseHex(String hex)
- Parameters:
hex- ahex representationofBLOB data.- Returns:
- the parsed
hexString.
-
formatHex
public static String formatHex(byte[] data)
-
parseBase64
public static byte[] parseBase64(String base64)
-
formatBase64
public static String formatBase64(byte[] data)
- Parameters:
data- thedatato format.- Returns:
- the given
dataformatted asBase64encodedStringrepresentation. - See Also:
Binary.formatBase64()
-
toBytes
public static byte[] toBytes(long value)
- Parameters:
value- thelongvalue.- Returns:
- the value as array of 8
bytes.
-
toBytes
public static byte[] toBytes(int value)
- Parameters:
value- theintvalue.- Returns:
- the value as array of 4
bytes.
-
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.
-
-