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 of
IHasByteArray- Since:
- 9.1.3
- Author:
- Philip Helger
-
Constructor Summary
ConstructorsConstructorDescriptionByteArrayWrapper(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
Modifier and TypeMethodDescriptionbyte[]bytes()static ByteArrayWrappercreate(NonBlockingByteArrayOutputStream aBAOS, boolean bCopyNeeded) Wrap the content of aNonBlockingByteArrayOutputStream.static ByteArrayWrapperWrap the content of a String in a certain charset.booleanintinthashCode()booleanisCopy()booleanisEmpty()booleanintsize()toString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.helger.commons.io.IHasByteArray
getAllBytes, getBytesAsString, getHexEncoded, getInputStream, hasOffset, isPartialArray, isReadMultiple, startsWith, writeToMethods inherited from interface com.helger.commons.io.IHasInputStream
getBufferedInputStream, withBufferedInputStreamDo, withInputStreamDoMethods inherited from interface com.helger.commons.io.IHasInputStreamAndReader
getBufferedReader, getReader
-
Constructor Details
-
ByteArrayWrapper
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 Details
-
isCopy
public boolean isCopy()- Specified by:
isCopyin interfaceIHasByteArray- Returns:
trueif the contained byte array was copied in the constructor or not.
-
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
- 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:
-
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:
-
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:
-
equals
-
hashCode
public int hashCode() -
toString
-
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
-