Package com.helger.commons.io
Class ByteArrayWrapper
- java.lang.Object
-
- com.helger.commons.io.ByteArrayWrapper
-
- All Implemented Interfaces:
IHasByteArray,IHasInputStream,IHasInputStreamAndReader,IHasSize
@MustImplementEqualsAndHashcode public final class ByteArrayWrapper extends Object implements IHasByteArray
A straight forward implementation ofIHasByteArray- Since:
- 9.1.3
- Author:
- Philip Helger
-
-
Constructor Summary
Constructors Constructor Description ByteArrayWrapper(byte[] aBytes, boolean bCopyNeeded)Wrap the whole byte array.ByteArrayWrapper(byte[] aBytes, int nOfs, int nLength, boolean bCopyNeeded)Wrap the passed byte array or just parts of it.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]bytes()static ByteArrayWrappercreate(NonBlockingByteArrayOutputStream aBAOS, boolean bCopyNeeded)Wrap the content of aNonBlockingByteArrayOutputStream.static ByteArrayWrappercreate(String sText, Charset aCharset)Wrap the content of a String in a certain charset.booleanequals(Object o)intgetOffset()inthashCode()booleanisCopy()booleanisEmpty()booleanisNotEmpty()intsize()StringtoString()-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.helger.commons.io.IHasByteArray
getAllBytes, getBytesAsString, getHexEncoded, getInputStream, hasOffset, isReadMultiple, startsWith, writeTo
-
Methods inherited from interface com.helger.commons.io.IHasInputStream
getBufferedInputStream
-
Methods inherited from interface com.helger.commons.io.IHasInputStreamAndReader
getBufferedReader, getReader
-
-
-
-
Constructor Detail
-
ByteArrayWrapper
public ByteArrayWrapper(@Nonnull byte[] aBytes, boolean bCopyNeeded)
Wrap the whole byte array.- Parameters:
aBytes- The byte array to be wrapped. May not benull.bCopyNeeded-trueto copy it,falseto reuse the instance.
-
ByteArrayWrapper
public ByteArrayWrapper(@Nonnull byte[] 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 benull.nOfs- Offset. Must be ≥ 0.nLength- Length. Must be ≥ 0.bCopyNeeded-trueto copy it,falseto reuse the instance.
-
-
Method Detail
-
isCopy
public boolean isCopy()
- Specified by:
isCopyin interfaceIHasByteArray- Returns:
trueif the contained byte array was copied in the constructor or not.
-
bytes
@Nonnull @ReturnsMutableObject public byte[] bytes()
- Specified by:
bytesin interfaceIHasByteArray- Returns:
- A reference to the contained byte array. Gives write access to the
payload! Don't forget to apply
IHasByteArray.getOffset()andIHasSize.size(). Nevernull.
-
getOffset
@Nonnegative public int getOffset()
- Specified by:
getOffsetin interfaceIHasByteArray- Returns:
- The offset into the byte array to start reading. This is always 0 when copied. Must be ge; 0.
- See Also:
IHasByteArray.bytes(),IHasSize.size()
-
size
@Nonnegative public int size()
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfaceIHasByteArray- Specified by:
isEmptyin interfaceIHasSize- Returns:
trueif no items are present,falseif at least a single item is present.- See Also:
IHasSize.size(),IHasSize.isNotEmpty()
-
isNotEmpty
public boolean isNotEmpty()
- Specified by:
isNotEmptyin interfaceIHasByteArray- Specified by:
isNotEmptyin interfaceIHasSize- Returns:
trueif at least one item is present,falseif no item is present.- See Also:
IHasSize.size(),IHasSize.isEmpty()
-
create
@Nonnull @ReturnsMutableCopy public static ByteArrayWrapper create(@Nonnull NonBlockingByteArrayOutputStream aBAOS, boolean bCopyNeeded)
Wrap the content of aNonBlockingByteArrayOutputStream.- Parameters:
aBAOS- The output stream to be wrapped. May not benull.bCopyNeeded-trueto copy it (needed if the output stream will be modified afterwards),falseto reuse the data (if the output stream will not be modified afterwards).- Returns:
- ByteArrayWrapper The created instance. Never
null. - Since:
- 9.2.1
-
create
@Nonnull @ReturnsMutableCopy public static 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 benull.aCharset- The character set to be used for retrieving the bytes from the string. May not benull.- Returns:
- ByteArrayWrapper The created instance. Never
null. - Since:
- 9.2.1
-
-