Package craterdog.primitives
Class BinaryString
- All Implemented Interfaces:
Atomic<BinaryString>
,Composite<BinaryString>
,Sequential<Byte>
,Comparable<BinaryString>
,Iterable<Byte>
public final class BinaryString
extends Primitive<BinaryString>
implements Sequential<Byte>, Composite<BinaryString>
This class implements a binary string in a way that makes it easy to encode and decode it
in various numeric bases (2, 16, 32, 64). The class also supports the
java.lang.Iterable
interface so that it can be used by the java language
for each loops. Binary strings are immutable.- Author:
- Derk Norton
-
Constructor Summary
ConstructorsConstructorDescriptionThis default constructor creates an empty binary string.BinaryString(byte[] bytes)
This constructor creates a binary string containing the specified bytes.BinaryString(Byte[] bytes)
This constructor creates a binary string containing the specified bytes.BinaryString(String string)
This constructor decodes the specified string using base 64 to extract the corresponding binary string.BinaryString(String string, int base)
This constructor decodes the specified string using the specified base to extract the corresponding binary string.BinaryString(BitSet bits)
This constructor creates a binary string containing the specified bit set. -
Method Summary
Modifier and TypeMethodDescriptionint
compareTo(BinaryString that)
<T extends Composite<BinaryString>>
Tcopy()
boolean
int
getSize()
int
hashCode()
Byte[]
toArray()
byte[]
toBytes()
This method returns a byte array containing the binary string.toString()
toString(int base)
This method converts the binary string into the specified base encoded string.This method converts the binary string into the specified base encoded string.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Methods inherited from interface craterdog.core.Sequential
isEmpty, iterator
-
Constructor Details
-
BinaryString
public BinaryString()This default constructor creates an empty binary string. -
BinaryString
public BinaryString(byte[] bytes)This constructor creates a binary string containing the specified bytes.- Parameters:
bytes
- The bytes to be used to create the binary string.
-
BinaryString
This constructor creates a binary string containing the specified bytes.- Parameters:
bytes
- The bytes to be used to create the binary string.
-
BinaryString
This constructor decodes the specified string using base 64 to extract the corresponding binary string.- Parameters:
string
- The encoded string containing the bytes to be used to create the binary string.
-
BinaryString
This constructor decodes the specified string using the specified base to extract the corresponding binary string.- Parameters:
string
- The encoded string containing the bytes to be used to create the binary string.base
- The numeric base (2, 16, 32, or 64) that was used to encode the string.
-
BinaryString
This constructor creates a binary string containing the specified bit set.- Parameters:
bits
- The bit set to be used to create the binary string.
-
-
Method Details
-
createIterator
- Specified by:
createIterator
in interfaceSequential<Byte>
-
compareTo
- Specified by:
compareTo
in interfaceComparable<BinaryString>
- Overrides:
compareTo
in classPrimitive<BinaryString>
-
equals
- Overrides:
equals
in classPrimitive<BinaryString>
-
hashCode
public int hashCode()- Overrides:
hashCode
in classPrimitive<BinaryString>
-
toString
-
toString
- Specified by:
toString
in interfaceComposite<BinaryString>
-
toString
This method converts the binary string into the specified base encoded string. If the encoded string is longer than 80 characters, it is broken up into separate lines with a maximum of 80 characters per line.- Parameters:
base
- The base to be used for encoding the bytes (2, 16, 32, 64).- Returns:
- The base 64 encoded string for the binary string.
-
toString
This method converts the binary string into the specified base encoded string. If the encoded string is longer than 80 characters, it is broken up into separate lines with a maximum of 80 characters per line. The specified whitespace indentation string is used to prefix each line.- Parameters:
base
- The base to be used for encoding the bytes (2, 16, 32, 64).indentation
- The (whitespace only) string that should be used to prefix each line.- Returns:
- The base 64 encoded string for the binary string.
-
toBytes
public byte[] toBytes()This method returns a byte array containing the binary string.- Returns:
- A byte array containing the binary string.
-
getSize
public int getSize()- Specified by:
getSize
in interfaceSequential<Byte>
-
toArray
- Specified by:
toArray
in interfaceSequential<Byte>
-
copy
- Specified by:
copy
in interfaceComposite<BinaryString>
-