Package net.morimekta.util
Class Binary
- java.lang.Object
-
- net.morimekta.util.Binary
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<Binary>
,Stringable
@Immutable public class Binary extends java.lang.Object implements java.lang.Comparable<Binary>, Stringable, java.io.Serializable
Simplistic byte sequence wrapper with lots of convenience methods. Used to wrap byte arrays for the binary data type.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Binary(byte[] bytes)
Create a binary instance that wraps a created byte array.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
asString()
Make a string representation of the instance value.int
compareTo(Binary other)
static Binary
copy(byte[] bytes)
Convenience method to copy a byte array into a byte sequence.static Binary
copy(byte[] bytes, int off, int len)
Convenience method to copy a part of a byte array into a byte sequence.static Binary
empty()
Method to create a Binary with 0 bytes.boolean
equals(java.lang.Object o)
static Binary
fromBase64(java.lang.String base64)
Decode base64 string and wrap the result in a byte sequence.static Binary
fromHexString(java.lang.String hex)
Parse a hex string as bytes.static Binary
fromUUID(java.util.UUID uuid)
Get a binary representation of a UUID.byte[]
get()
Get a copy of the backing array.int
get(byte[] into)
Get a copy of the backing array.java.nio.ByteBuffer
getByteBuffer()
Get a byte buffer wrapping the binary data.java.io.InputStream
getInputStream()
Get an input stream that reads from the stored bytes.int
hashCode()
int
length()
Get the length of the backing array.static Binary
read(java.io.InputStream in)
Read a binary buffer from input stream.static Binary
read(java.io.InputStream in, int len)
Read a binary buffer from input stream.java.lang.String
toBase64()
Get the sequence encoded as base64.java.lang.String
toHexString()
Make a hex string from a byte array.java.lang.String
toString()
java.util.UUID
toUUID()
Get a UUID from the binary data.The UUID binary representation is equivalent to the hexadecimal representation of the UUID (sans dashes).static Binary
wrap(byte[] bytes)
Convenience method to wrap a byte array into a byte sequence.int
write(java.io.OutputStream out)
Write bytes to output stream.
-
-
-
Method Detail
-
wrap
public static Binary wrap(byte[] bytes)
Convenience method to wrap a byte array into a byte sequence.- Parameters:
bytes
- Bytes to wrap.- Returns:
- The wrapped byte sequence.
-
copy
public static Binary copy(byte[] bytes)
Convenience method to copy a byte array into a byte sequence.- Parameters:
bytes
- Bytes to wrap.- Returns:
- The wrapped byte sequence.
-
copy
public static Binary copy(byte[] bytes, int off, int len)
Convenience method to copy a part of a byte array into a byte sequence.- Parameters:
bytes
- Bytes to wrap.off
- Offset in source bytes to start reading from.len
- Number of bytes to copy.- Returns:
- The wrapped byte sequence.
-
empty
public static Binary empty()
Method to create a Binary with 0 bytes.- Returns:
- Empty Binary object.
-
length
public int length()
Get the length of the backing array.- Returns:
- Byte count.
-
get
public byte[] get()
Get a copy of the backing array.- Returns:
- The copy.
-
get
public int get(byte[] into)
Get a copy of the backing array.- Parameters:
into
- Target ro copy into.- Returns:
- Number of bytes written.
-
fromBase64
public static Binary fromBase64(java.lang.String base64)
Decode base64 string and wrap the result in a byte sequence.- Parameters:
base64
- The string to decode.- Returns:
- The resulting sequence.
-
toBase64
public java.lang.String toBase64()
Get the sequence encoded as base64.- Returns:
- The encoded string.
-
fromUUID
public static Binary fromUUID(java.util.UUID uuid)
Get a binary representation of a UUID. The UUID binary representation is equivalent to the hexadecimal representation of the UUID (sans dashes). SeeUUID.toString()
andUUID.fromString(String)
.- Parameters:
uuid
- The UUID to make binary representation of.- Returns:
- The Binary representation.
- Throws:
java.lang.IllegalArgumentException
- If a null UUID is given.
-
toUUID
public java.util.UUID toUUID()
Get a UUID from the binary data.The UUID binary representation is equivalent to the hexadecimal representation of the UUID (sans dashes). SeeUUID.toString()
andUUID.fromString(String)
.- Returns:
- The UUID representation of the 16 bytes.
- Throws:
java.lang.IllegalStateException
- If the binary does not have the correct size for holding a UUID, 16 bytes.
-
fromHexString
public static Binary fromHexString(java.lang.String hex)
Parse a hex string as bytes.- Parameters:
hex
- The hex string.- Returns:
- The corresponding bytes.
-
toHexString
public java.lang.String toHexString()
Make a hex string from a byte array.- Returns:
- The hex string.
-
getByteBuffer
public java.nio.ByteBuffer getByteBuffer()
Get a byte buffer wrapping the binary data.- Returns:
- A byte buffer.
-
getInputStream
public java.io.InputStream getInputStream()
Get an input stream that reads from the stored bytes.- Returns:
- An input stream.
-
read
public static Binary read(java.io.InputStream in) throws java.io.IOException
Read a binary buffer from input stream. This will read all of the content of the stream, and put in the binary.- Parameters:
in
- Input stream to read.- Returns:
- The binary instance.
- Throws:
java.io.IOException
- If unable to read completely what's expected.
-
read
public static Binary read(java.io.InputStream in, int len) throws java.io.IOException
Read a binary buffer from input stream. This will read a specific number of bytes form the stream, and wrap in the binary instance.- Parameters:
in
- Input stream to read.len
- Number of bytes to read.- Returns:
- The binary instance.
- Throws:
java.io.IOException
- If unable to read completely what's expected.
-
write
public int write(java.io.OutputStream out) throws java.io.IOException
Write bytes to output stream.- Parameters:
out
- Stream to write to.- Returns:
- Number of bytes written.
- Throws:
java.io.IOException
- When unable to write to stream.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
compareTo
public int compareTo(@Nonnull Binary other)
- Specified by:
compareTo
in interfacejava.lang.Comparable<Binary>
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
asString
public java.lang.String asString()
Description copied from interface:Stringable
Make a string representation of the instance value.- Specified by:
asString
in interfaceStringable
- Returns:
- The string representation.
-
-