Class ByteArrayWrapper

java.lang.Object
com.helger.base.array.bytes.ByteArrayWrapper
All Implemented Interfaces:
IHasSize, IHasByteArray, IHasInputStream, IHasInputStreamAndReader, IWriteToStream

@MustImplementEqualsAndHashcode public final class ByteArrayWrapper extends Object implements IHasByteArray
A straight forward implementation of IHasByteArray
Since:
9.1.3
Author:
Philip Helger
  • Constructor Details

    • ByteArrayWrapper

      public ByteArrayWrapper(byte @NonNull [] aBytes, boolean bCopyNeeded)
      Wrap the whole byte array.
      Parameters:
      aBytes - The byte array to be wrapped. May not be null.
      bCopyNeeded - true to copy it, false to reuse the instance.
    • ByteArrayWrapper

      public ByteArrayWrapper(byte @NonNull [] aBytes, @Nonnegative int nOfs, @Nonnegative int nLength, boolean bCopyNeeded)
      Wrap the passed byte array or just parts of it.
      Parameters:
      aBytes - The byte array to be wrapped. May not be null.
      nOfs - Offset. Must be ≥ 0.
      nLength - Length. Must be ≥ 0.
      bCopyNeeded - true to copy it, false to reuse the instance.
  • Method Details

    • isCopy

      public boolean isCopy()
      Specified by:
      isCopy in interface IHasByteArray
      Returns:
      true if the internal byte array is a copy of the original, false if it references the original array.
    • bytes

      @ReturnsMutableObject public byte @NonNull [] bytes()
      Specified by:
      bytes in interface IHasByteArray
      Returns:
      A reference to the contained byte array. Gives write access to the payload! Don't forget to apply IHasByteArray.getOffset() and IHasSize.size(). Never null.
    • getOffset

      @Nonnegative public int getOffset()
      Specified by:
      getOffset in interface IHasByteArray
      Returns:
      The offset into the byte array to start reading. This is always 0 when copied. Must be ge; 0.
      See Also:
    • size

      @Nonnegative public int size()
      Specified by:
      size in interface IHasSize
      Returns:
      The number of contained elements. Always ≥ 0.
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: IHasByteArray
      Specified by:
      isEmpty in interface IHasByteArray
      Specified by:
      isEmpty in interface IHasSize
      Returns:
      true if no items are present, false if at least a single item is present.
      See Also:
    • isNotEmpty

      public boolean isNotEmpty()
      Specified by:
      isNotEmpty in interface IHasByteArray
      Specified by:
      isNotEmpty in interface IHasSize
      Returns:
      true if at least one item is present, false if no item is present.
      See Also:
    • equals

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

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

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

      @ReturnsMutableCopy public static @NonNull ByteArrayWrapper create(@NonNull NonBlockingByteArrayOutputStream aBAOS, boolean bCopyNeeded)
      Wrap the content of a NonBlockingByteArrayOutputStream.
      Parameters:
      aBAOS - The output stream to be wrapped. May not be null.
      bCopyNeeded - true to copy it (needed if the output stream will be modified afterwards), false to reuse the data (if the output stream will not be modified afterwards).
      Returns:
      The created instance. Never null.
      Since:
      9.2.1
    • create

      @ReturnsMutableCopy public static @NonNull ByteArrayWrapper create(@NonNull String sText, @NonNull Charset aCharset)
      Wrap the content of a String in a certain charset.
      Parameters:
      sText - The String to be wrapped. May not be null.
      aCharset - The character set to be used for retrieving the bytes from the string. May not be null.
      Returns:
      The created instance. Never null.
      Since:
      9.2.1
    • create

      public static @NonNull ByteArrayWrapper create(@NonNull ByteBuffer aBuffer, boolean bCopyNeeded)
      Wrap the content of a ByteBuffer.
      Parameters:
      aBuffer - The buffer to be wrapped. May not be null.
      bCopyNeeded - true if bytes should be copied, false otherwise.
      Returns:
      The created instance. Never null.