Class ImmutableBytes

java.lang.Object
com.inductiveautomation.ignition.common.ImmutableBytes
All Implemented Interfaces:
Serializable

public class ImmutableBytes extends Object implements Serializable
See Also:
  • Field Details

    • EMPTY

      public static final ImmutableBytes EMPTY
      Shared instance of an instance for byte[0]
  • Method Details

    • copyOf

      public static ImmutableBytes copyOf(@Nonnull byte[] bytes)
      Creates an ImmutableBytes instance by copying the given byte array.
    • fromFile

      public static ImmutableBytes fromFile(@Nonnull Path filePath) throws IOException
      Creates an ImmutableBytes instance by reading all available bytes from a file
      Throws:
      IOException
    • fromStream

      public static ImmutableBytes fromStream(@Nonnull InputStream stream) throws IOException
      Creates an ImmutableBytes instance by reading all available bytes from an input stream. Note that this method does not close the stream.
      Throws:
      IOException
    • ofString

      public static ImmutableBytes ofString(@Nonnull String string)
      Parameters:
      string - The string to represent as bytes
      Returns:
      An instance of ImmutableBytes representing the given string in UTF-8 encoding
    • fromHexString

      public static ImmutableBytes fromHexString(@Nonnull String hexString)
      Decodes the given hex string into bytes. Opposite of getAsHexString().
    • fromHexString

      public static ImmutableBytes fromHexString(@Nonnull HexFormat hexFormat, @Nonnull String hexString)
      Decodes the given hex string into bytes using the given HexFormat. Opposite of getAsHexString(HexFormat).
    • fromBase64String

      public static ImmutableBytes fromBase64String(@Nonnull Base64.Decoder decoder, @Nonnull String base64String)
      Decodes the given base64-encoded string into bytes using the given Base64.Decoder. Opposite of getAsBase64String(Encoder).
    • fromRandomGenerator

      public static ImmutableBytes fromRandomGenerator(RandomGenerator randomGenerator, int length)
      Generates a new ImmutableBytes instance by reading a configurable length of bytes from a given RandomGenerator.
      Parameters:
      randomGenerator - the RandomGenerator. must not be null.
      length - the length of random bytes to generate. must not be negative.
      Returns:
      a new ImmutableBytes instance backed by a byte array of the given length whose contents are provided by the given RandomGenerator. never null.
      Throws:
      NullPointerException - if the randomGenerator argument is null
    • getBytes

      public byte[] getBytes()
      Returns:
      a copy of the byte array held by this object
    • getBytesAsString

      public String getBytesAsString()
      Returns:
      the bytes as a string, using UTF-8 encoding
    • getBytesAsString

      public String getBytesAsString(Charset charset)
      Parameters:
      charset - The charset to use to interpret the bytes
      Returns:
      the bytes as a string, using the given charset
    • getAsHexString

      public String getAsHexString()
      Returns:
      the bytes as a hex string
      See Also:
    • getAsHexString

      public String getAsHexString(HexFormat hexFormat)
      Returns:
      the bytes as a hex string
      See Also:
    • getAsBase64String

      public String getAsBase64String(Base64.Encoder encoder)
      Parameters:
      encoder - a Base64.Encoder used to base64-encode the bytes
      Returns:
      the bytes as a base64-encoded string
      See Also:
    • getBytesAsStream

      public InputStream getBytesAsStream()
      Returns:
      the bytes as an input stream
    • getBytesAsReader

      public Reader getBytesAsReader()
      Returns:
      the bytes as a reader, using UTF-8 encoding
    • writeToStream

      public void writeToStream(OutputStream outputStream) throws IOException
      Writes the bytes held by this object to the given output stream
      Throws:
      IOException
    • updateDigest

      public void updateDigest(MessageDigest digest)
      Calls MessageDigest.update(byte[]) with the bytes held by this object
    • digest

      public ImmutableBytes digest(Supplier<DigestBuilder> digestBuilderSupplier)
      Creates a new ImmutableBytes instance by taking a digest of this instance's bytes using a supplied DigestBuilder.
      Parameters:
      digestBuilderSupplier - a Supplier of DigestBuilder. must not be null.
      Returns:
      a new ImmutableBytes instance backed by the digest of this instance's bytes
      Throws:
      NullPointerException - if the digestBuilderSupplier argument is null or the supplied DigestBuilder is null
    • getCrc32

      public long getCrc32()
      Returns:
      the CRC-32 checksum of the bytes
    • length

      public int length()
      Returns:
      the length of the byte array
    • hasBytes

      public boolean hasBytes()
      Returns:
      true if the byte array has a length greater than 0
    • isEmpty

      public boolean isEmpty()
      Returns:
      true if the byte array has a length of 0.
    • get

      public byte get(int index)
      Parameters:
      index - the index within the byte array to fetch
      Returns:
      the byte at the given index
    • get

      public byte[] get(int start, int end)
      Get a subsequence of the byte[] held by this object
      Parameters:
      start - the start index (inclusive)
      end - the end index (exclusive)
      Returns:
      a new byte[] containing the bytes from start to end
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object