Package com.helger.base.array.bytes
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 Summary
ConstructorsConstructorDescriptionByteArrayWrapper(byte @NonNull [] aBytes, boolean bCopyNeeded) Wrap the whole byte array.ByteArrayWrapper(byte @NonNull [] aBytes, int nOfs, int nLength, boolean bCopyNeeded) Wrap the passed byte array or just parts of it. -
Method Summary
Modifier and TypeMethodDescriptionbyte @NonNull []bytes()static @NonNull ByteArrayWrappercreate(@NonNull NonBlockingByteArrayOutputStream aBAOS, boolean bCopyNeeded) Wrap the content of aNonBlockingByteArrayOutputStream.static @NonNull ByteArrayWrapperWrap the content of a String in a certain charset.static @NonNull ByteArrayWrappercreate(@NonNull ByteBuffer aBuffer, boolean bCopyNeeded) Wrap the content of aByteBuffer.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.base.io.iface.IHasByteArray
getAllBytes, getBytesAsString, getHexEncoded, getInputStream, hasOffset, isPartialArray, isReadMultiple, startsWith, writeToMethods inherited from interface com.helger.base.io.iface.IHasInputStream
getBufferedInputStream, withBufferedInputStreamDo, withInputStreamDoMethods inherited from interface com.helger.base.io.iface.IHasInputStreamAndReader
getBufferedReader, getReaderMethods inherited from interface com.helger.base.io.iface.IWriteToStream
writeToAndClose
-
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 benull.bCopyNeeded-trueto copy it,falseto 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 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 internal byte array is a copy of the original,falseif it references the original array.
-
bytes
@ReturnsMutableObject public byte @NonNull [] 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:
-
size
@Nonnegative public int size() -
isEmpty
public boolean isEmpty()Description copied from interface:IHasByteArray- 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
@ReturnsMutableCopy public static @NonNull 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:
- 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 benull.aCharset- The character set to be used for retrieving the bytes from the string. May not benull.- Returns:
- The created instance. Never
null. - Since:
- 9.2.1
-
create
Wrap the content of aByteBuffer.- Parameters:
aBuffer- The buffer to be wrapped. May not benull.bCopyNeeded-trueif bytes should be copied,falseotherwise.- Returns:
- The created instance. Never
null.
-