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 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

      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

      public BinaryString(String string)
      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

      public BinaryString(String string, int base)
      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

      public BinaryString(BitSet bits)
      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

      public Iterator<Byte> createIterator()
      Specified by:
      createIterator in interface Sequential<Byte>
    • compareTo

      public int compareTo(BinaryString that)
      Specified by:
      compareTo in interface Comparable<BinaryString>
      Overrides:
      compareTo in class Primitive<BinaryString>
    • equals

      public boolean equals(Object object)
      Overrides:
      equals in class Primitive<BinaryString>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Primitive<BinaryString>
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      public String toString(String indentation)
      Specified by:
      toString in interface Composite<BinaryString>
    • toString

      public String toString(int base)
      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

      public String toString(int base, String indentation)
      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 interface Sequential<Byte>
    • toArray

      public Byte[] toArray()
      Specified by:
      toArray in interface Sequential<Byte>
    • copy

      public <T extends Composite<BinaryString>> T copy()
      Specified by:
      copy in interface Composite<BinaryString>