Class ByteArrayView
- All Implemented Interfaces:
com.streambase.sb.internal.CloneableData
,Serializable
,Cloneable
,Comparable<ByteArrayView>
- Direct Known Subclasses:
DirectByteArrayView
ByteArrayView provides an immutable window onto a byte[] or a part of one, specified with an offset and length. It provides convenience routines to create new windows on the current view as either ByteArrayViews or ByteBuffers.
Only the copy() methods copy the underlying data, so changes in the parent byte[] will be reflected in the view's results
Note: The ByteArrayView and its underlying Blob object should not be modified in-place
by a StreamBase application. Modifying a Blob may introduce race conditions. If you do need to
modify a Blob's data, create a new Blob or ByteArrayView object to receive the result. Use
copy methods such as copyBytes()
, and create a new ByteArrayView with makeView(byte[])
and return it when needed.
Note: Serializations of instances of this class that are created (e.g., by using
ObjectOutputStream
) in one version of StreamBase in general will not be
deserializable in any other version of StreamBase.
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract byte[]
array()
Returns a reference to the backing array for this view.final String
asString()
Convert byte array to a stringclone()
Implement Object.clone, which just delegates to ByteArrayView.copy().int
compareTo
(ByteArrayView rhs) compare this ByteArrayView to another lexigraphically NB: no data is copied when doing thisabstract ByteArrayView
copy()
Create a new ByteArrayView which wraps a new byte[] containing a copy of this view's data NB: this method copies the underlying dataabstract ByteArrayView
copy
(int offset, int length) Create a new ByteArrayView which wraps a new byte[] containing a copy of this view's data NB: this method copies the underlying dataabstract byte[]
Create a new byte[] containing a copy of this view's data.abstract byte[]
copyBytes
(int offset, int length) Create a new byte[] containing a copy of this view's data within the provided window.boolean
Check if two ByteArrayViews are byte-for-byte identical NB: this may devolve to a byte by byte comparisonbyte
get
(int pos) Get byte as specified positionint
hashCode()
abstract int
length()
return the length of this view NB: no data is copied when doing thisstatic ByteArrayView
makeCopiedView
(byte[] bytes) Create a new ByteArrayView which wraps the entire provided byte[] (which is copied) NB: The bytes are copiedstatic ByteArrayView
makeCopiedView
(byte[] bytes, int offset, int length) Create a new ByteArrayView which wraps the provided byte[] (which is copied) between offset and length from offset NB: The bytes are copiedstatic ByteArrayView
makeView
(byte[] bytes) Create a new ByteArrayView which wraps the entire provided byte[] NB: no data is copied when doing thisstatic ByteArrayView
makeView
(byte[] bytes, int offset, int length) Create a new ByteArrayView which wraps the provided byte[] between offset and length from offset NB: no data is copied when doing thisstatic ByteArrayView
Create a new ByteArrayView which converts the provided string to bytes and wraps it.abstract int
offset()
return the offset into the backing array of this view NB: no data is copied when doing thisabstract ByteArrayView
slice
(int offset, int length) Create a new ByteArrayView which wraps a portion of this one NB: no data is copied when doing thistoString()
Formats a ByteArrayView by printing out every byte as a char.abstract ByteBuffer
view()
Create a new ByteBuffer which wraps all of this one NB: no data is copied when doing thisabstract ByteBuffer
view
(int offset, int length) Create a new ByteBuffer which wraps a portion of this one NB: no data is copied when doing this
-
Constructor Details
-
ByteArrayView
public ByteArrayView()
-
-
Method Details
-
makeView
Create a new ByteArrayView which wraps the entire provided byte[] NB: no data is copied when doing this- Parameters:
bytes
- the data to be viewed- Returns:
- the new ByteArrayView,
null
if bytes isnull
-
makeView
Create a new ByteArrayView which wraps the provided byte[] between offset and length from offset NB: no data is copied when doing this- Parameters:
bytes
- the data to be viewedoffset
- the initial offset of the windowlength
- the length of the window- Returns:
- the new ByteArrayView,
null
if bytes isnull
- Throws:
IllegalArgumentException
- if offset or length are negativeIndexOutOfBoundsException
- if offset+length > bytes.length
-
makeCopiedView
Create a new ByteArrayView which wraps the entire provided byte[] (which is copied) NB: The bytes are copied- Parameters:
bytes
- the data to be copied and viewed- Returns:
- the new ByteArrayView,
null
if bytes isnull
- Since:
- 6.3.13
-
makeCopiedView
Create a new ByteArrayView which wraps the provided byte[] (which is copied) between offset and length from offset NB: The bytes are copied- Parameters:
bytes
- the data to be copied and viewedoffset
- the initial offset of the windowlength
- the length of the window- Returns:
- the new ByteArrayView,
null
if bytes isnull
- Throws:
IllegalArgumentException
- if offset or length are negativeIndexOutOfBoundsException
- if offset+length > bytes.length- Since:
- 6.3.13
-
makeView
Create a new ByteArrayView which converts the provided string to bytes and wraps it. NB: no data is copied when doing this- Parameters:
s
- the String to be viewed- Returns:
- the new ByteArrayView,
null
if s isnull
- Since:
- 6.3.13
-
slice
Create a new ByteArrayView which wraps a portion of this one NB: no data is copied when doing this- Parameters:
offset
- the initial offset of the window (relative to the current window)length
- the length of the window- Returns:
- the new ByteArrayView
- Throws:
IllegalArgumentException
- if offset or length are negativeIndexOutOfBoundsException
- if offset+length > length()
-
view
Create a new ByteBuffer which wraps a portion of this one NB: no data is copied when doing this- Parameters:
offset
- the initial offset of the window (relative to the current window)length
- the length of the window- Returns:
- a ByteBuffer whose arrayOffset() and limit() are the offset and length provided
- Throws:
IllegalArgumentException
- if offset or length are negativeIndexOutOfBoundsException
- if offset+length > length()
-
view
Create a new ByteBuffer which wraps all of this one NB: no data is copied when doing this- Returns:
- a ByteBuffer whose arrayOffset() and limit() are the offset() and length() of this view
-
array
public abstract byte[] array()Returns a reference to the backing array for this view. Note that the view might only be of a part of the returned array: this means you should only access the array between offsetsoffset()
and offset() +length()
. NB: no data is copied when doing this- Returns:
- the backing array
-
offset
public abstract int offset()return the offset into the backing array of this view NB: no data is copied when doing this- Returns:
- the offset in bytes
-
length
public abstract int length()return the length of this view NB: no data is copied when doing this- Returns:
- the length in bytes
-
copy
Create a new ByteArrayView which wraps a new byte[] containing a copy of this view's data NB: this method copies the underlying data- Parameters:
offset
- the initial offset of the window (relative to the current window)length
- the length of the window- Returns:
- the new ByteArrayView
- Throws:
IllegalArgumentException
- if offset or length are negativeIndexOutOfBoundsException
- if offset+length > length()- See Also:
-
copy
Create a new ByteArrayView which wraps a new byte[] containing a copy of this view's data NB: this method copies the underlying data- Returns:
- the new ByteArrayView
- See Also:
-
clone
Implement Object.clone, which just delegates to ByteArrayView.copy(). -
copyBytes
public abstract byte[] copyBytes(int offset, int length) Create a new byte[] containing a copy of this view's data within the provided window. The returned array may be indexed from 0, unlikearray()
. NB: this method copies the underlying data- Parameters:
offset
- the initial offset of the window (relative to the current window)length
- the length of the window- Returns:
- the new byte[]
- Throws:
IllegalArgumentException
- if offset or length are negativeIndexOutOfBoundsException
- if offset+length > length()
-
copyBytes
public abstract byte[] copyBytes()Create a new byte[] containing a copy of this view's data. The returned array may be indexed from 0, unlikearray()
. NB: this method copies the underlying data- Returns:
- a new byte[] containing a copy of this view's data
-
compareTo
compare this ByteArrayView to another lexigraphically NB: no data is copied when doing this- Specified by:
compareTo
in interfaceComparable<ByteArrayView>
- Parameters:
rhs
- The right hand side of this comparison- Returns:
- < 0 if this is less than
-
equals
Check if two ByteArrayViews are byte-for-byte identical NB: this may devolve to a byte by byte comparison -
hashCode
public int hashCode() -
get
public byte get(int pos) Get byte as specified position- Parameters:
pos
- Position to access- Returns:
- Byte
-
toString
Formats a ByteArrayView by printing out every byte as a char. If this ByteArrayView is larger than 16 bytes, only 13 characters will be printed an an ellipsis ("...") will be appended. -
asString
Convert byte array to a string- Returns:
- String
-