Package io.pravega.common.util
Class ByteArraySegment
- java.lang.Object
-
- io.pravega.common.util.AbstractBufferView
-
- io.pravega.common.util.ByteArraySegment
-
- All Implemented Interfaces:
ArrayView,BufferView,StructuredReadableBuffer,StructuredWritableBuffer
public class ByteArraySegment extends AbstractBufferView implements ArrayView
Allows segmenting a byte array and operating only on that segment.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class io.pravega.common.util.AbstractBufferView
AbstractBufferView.AbstractReader
-
Nested classes/interfaces inherited from interface io.pravega.common.util.BufferView
BufferView.Collector<ExceptionT extends java.lang.Exception>
-
-
Constructor Summary
Constructors Constructor Description ByteArraySegment(byte[] array)Creates a new instance of the ByteArraySegment class that wraps the entire given array.ByteArraySegment(byte[] array, int startOffset, int length)Creates a new instance of the ByteArraySegment class that wraps the given array range.ByteArraySegment(java.nio.ByteBuffer buff)Creates a new instance of the ByteArraySegment class that wraps an array backed ByteBuffer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]array()Gets a reference to the backing array for this ArrayView.intarrayOffset()Gets a value indicating the offset in the backing array where this ArrayView starts at.java.nio.ByteBufferasByteBuffer()Returns a newByteBufferthat wraps the contents of thisArrayView.<ExceptionT extends java.lang.Exception>
voidcollect(BufferView.Collector<ExceptionT> bufferCollector)Iterates through each of the buffers that make up thisBufferView, in order, and invokes the givenBufferView.Collectoron each.voidcopyFrom(ArrayView source, int targetOffset, int length)Copies a specified number of bytes from the givenArrayViewinto this ByteArraySegment.voidcopyFrom(ArrayView source, int sourceOffset, int targetOffset, int length)Copies a specified number of bytes from the givenArrayViewinto this ByteArraySegment.voidcopyTo(byte[] target, int targetOffset, int length)Copies a specified number of bytes from this ArrayView into the given target array.voidcopyTo(java.io.OutputStream stream)Writes the entire contents of this ByteArraySegment to the given OutputStream.intcopyTo(java.nio.ByteBuffer target)Copies the contents of thisBufferViewto the givenByteBuffer.booleanequals(BufferView other)Checks for equality against anotherBufferViewinstance.byteget(int index)Gets the value at the specified index.intgetAllocatedLength()Gets a value indicating the amount of memory (in bytes) allocated for thisBufferView.io.pravega.common.util.ByteArraySegment.ReadergetBufferViewReader()Creates a newBufferView.Readerthat can be used to read thisBufferView.byte[]getCopy()Returns a copy of the contents of thisBufferView.intgetInt(int index)Gets the 32 bit Int value at the specified index.intgetLength()Gets a value representing the length of thisBufferView.longgetLong(int index)Gets the 64 bit Long value at the specified index.java.io.InputStreamgetReader()Creates an InputStream that can be used to read the contents of thisBufferView.java.io.InputStreamgetReader(int offset, int length)Creates an InputStream that can be used to read the contents of thisBufferView.shortgetShort(int index)Gets the 16 bit Short value at the specified index.java.util.Iterator<java.nio.ByteBuffer>iterateBuffers()voidset(int index, byte value)Sets the value at the specified index.voidsetInt(int index, int value)Sets a 32 bit Integer value at the specified index.voidsetLong(int index, long value)Sets a 64 bit Long value at the specified index.voidsetShort(int index, short value)Sets a Short value at the specified index.ByteArraySegmentslice(int offset, int length)java.lang.StringtoString()-
Methods inherited from class io.pravega.common.util.AbstractBufferView
equals, hashCode
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.pravega.common.util.BufferView
release, retain, slice
-
Methods inherited from interface io.pravega.common.util.StructuredReadableBuffer
getUnsignedLong
-
Methods inherited from interface io.pravega.common.util.StructuredWritableBuffer
setUnsignedLong
-
-
-
-
Constructor Detail
-
ByteArraySegment
public ByteArraySegment(byte[] array)
Creates a new instance of the ByteArraySegment class that wraps the entire given array.- Parameters:
array- The array to wrap.- Throws:
java.lang.NullPointerException- If the array is null.
-
ByteArraySegment
public ByteArraySegment(java.nio.ByteBuffer buff)
Creates a new instance of the ByteArraySegment class that wraps an array backed ByteBuffer.- Parameters:
buff- The ByteBuffer to wrap.- Throws:
java.lang.NullPointerException- If the array is null.java.lang.UnsupportedOperationException- If buff is not backed by an array.
-
ByteArraySegment
public ByteArraySegment(byte[] array, int startOffset, int length)Creates a new instance of the ByteArraySegment class that wraps the given array range.- Parameters:
array- The array to wrap.startOffset- The offset within the array to start the segment at.length- The length of the segment.- Throws:
java.lang.NullPointerException- If the array is null.java.lang.ArrayIndexOutOfBoundsException- If StartOffset or Length have invalid values.
-
-
Method Detail
-
getAllocatedLength
public int getAllocatedLength()
Description copied from interface:BufferViewGets a value indicating the amount of memory (in bytes) allocated for thisBufferView.- Specified by:
getAllocatedLengthin interfaceBufferView- Returns:
- The allocated memory size.
-
get
public byte get(int index)
Description copied from interface:StructuredReadableBufferGets the value at the specified index.- Specified by:
getin interfaceStructuredReadableBuffer- Parameters:
index- The index to get the value at.- Returns:
- The byte at the specified index.
-
getShort
public short getShort(int index)
Description copied from interface:StructuredReadableBufferGets the 16 bit Short value at the specified index.- Specified by:
getShortin interfaceStructuredReadableBuffer- Parameters:
index- The index to get the value at.- Returns:
- The Short at the specified index.
-
getInt
public int getInt(int index)
Description copied from interface:StructuredReadableBufferGets the 32 bit Int value at the specified index.- Specified by:
getIntin interfaceStructuredReadableBuffer- Parameters:
index- The index to get the value at.- Returns:
- The Integer at the specified index.
-
getLong
public long getLong(int index)
Description copied from interface:StructuredReadableBufferGets the 64 bit Long value at the specified index.- Specified by:
getLongin interfaceStructuredReadableBuffer- Parameters:
index- The index to get the value at.- Returns:
- The Long at the specified index.
-
array
public byte[] array()
Description copied from interface:ArrayViewGets a reference to the backing array for this ArrayView. This should be used in conjunction with arrayOffset() in order to determine where in the array this ArrayView starts at. NOTE: Care must be taken when using this array. Just like any other array in Java, it is modifiable and changes to it will be reflected in this ArrayView.
-
arrayOffset
public int arrayOffset()
Description copied from interface:ArrayViewGets a value indicating the offset in the backing array where this ArrayView starts at.- Specified by:
arrayOffsetin interfaceArrayView- Returns:
- The offset in the backing array.
-
getBufferViewReader
public io.pravega.common.util.ByteArraySegment.Reader getBufferViewReader()
Description copied from interface:BufferViewCreates a newBufferView.Readerthat can be used to read thisBufferView. This reader is preferable toBufferView.getReader()that returns anInputStreamas it contains optimized methods for copying directly into otherBufferViewinstances, such asByteArraySegments.- Specified by:
getBufferViewReaderin interfaceBufferView- Returns:
- A new
BufferView.Reader.
-
getReader
public java.io.InputStream getReader()
Description copied from interface:BufferViewCreates an InputStream that can be used to read the contents of thisBufferView. The InputStream returned spans the entireBufferView.- Specified by:
getReaderin interfaceBufferView- Returns:
- The InputStream.
-
getReader
public java.io.InputStream getReader(int offset, int length)Description copied from interface:BufferViewCreates an InputStream that can be used to read the contents of thisBufferView.- Specified by:
getReaderin interfaceBufferView- Parameters:
offset- The starting offset of the section to read.length- The length of the section to read.- Returns:
- The InputStream.
-
slice
public ByteArraySegment slice(int offset, int length)
Description copied from interface:ArrayViewCreates a newArrayViewthat represents a sub-range of thisArrayViewinstance. The new instance will share the same backing array as this one, so a change to one will be reflected in the other.- Specified by:
slicein interfaceArrayView- Specified by:
slicein interfaceBufferView- Parameters:
offset- The starting offset to begin the slice at.length- The sliced length.- Returns:
- A new
ArrayView.
-
asByteBuffer
public java.nio.ByteBuffer asByteBuffer()
Description copied from interface:ArrayViewReturns a newByteBufferthat wraps the contents of thisArrayView.- Specified by:
asByteBufferin interfaceArrayView- Returns:
- A
ByteBufferthat shares the same backing array as thisArrayView. Any changes made to theByteBufferwill be reflected in thisArrayViewand viceversa.
-
getCopy
public byte[] getCopy()
Description copied from interface:BufferViewReturns a copy of the contents of thisBufferView.- Specified by:
getCopyin interfaceBufferView- Returns:
- A byte array with the same length as this
BufferView, containing a copy of the data within it.
-
copyTo
public void copyTo(byte[] target, int targetOffset, int length)Description copied from interface:ArrayViewCopies a specified number of bytes from this ArrayView into the given target array.
-
copyTo
public int copyTo(java.nio.ByteBuffer target)
Description copied from interface:BufferViewCopies the contents of thisBufferViewto the givenByteBuffer.- Specified by:
copyToin interfaceBufferView- Parameters:
target- TheByteBufferto copy to. This buffer must have sufficient capacity to allow the entire contents of theBufferViewto be written. If less needs to be copied, consider usingBufferView.slice()to select a sub-range of thisBufferView.- Returns:
- The number of bytes copied.
-
copyTo
public void copyTo(java.io.OutputStream stream) throws java.io.IOExceptionWrites the entire contents of this ByteArraySegment to the given OutputStream. Only copies the contents of the ByteArraySegment, and writes no other data (such as the length of the Segment or any other info).- Specified by:
copyToin interfaceBufferView- Parameters:
stream- The OutputStream to write to.- Throws:
java.io.IOException- If the OutputStream threw one.
-
equals
public boolean equals(BufferView other)
Description copied from class:AbstractBufferViewChecks for equality against anotherBufferViewinstance. Note: this method provides a general implementation of equality. Derived classes should override this method with more efficient implementations if available.- Overrides:
equalsin classAbstractBufferView- Parameters:
other- The otherBufferViewinstance.- Returns:
- True if this instance and the other instance have the same contents.
-
collect
public <ExceptionT extends java.lang.Exception> void collect(BufferView.Collector<ExceptionT> bufferCollector) throws ExceptionT extends java.lang.Exception
Description copied from interface:BufferViewIterates through each of the buffers that make up thisBufferView, in order, and invokes the givenBufferView.Collectoron each.- Specified by:
collectin interfaceBufferView- Type Parameters:
ExceptionT- Type of exception that theBufferView.Collectorfunction throws, if any.- Parameters:
bufferCollector- ABufferView.Collectorfunction that will be invoked for each component. EachByteBufferpassed as an argument to this function is a direct pointer to the data contained within theBufferView(i.e., they are not copies of the data).- Throws:
ExceptionT- If theBufferView.Collectorfunction throws an exception of this type, the iteration will end and the exception will be bubbled up.ExceptionT extends java.lang.Exception
-
iterateBuffers
public java.util.Iterator<java.nio.ByteBuffer> iterateBuffers()
Description copied from interface:BufferView- Specified by:
iterateBuffersin interfaceBufferView- Returns:
- A
Iterator.
-
set
public void set(int index, byte value)Description copied from interface:StructuredWritableBufferSets the value at the specified index.- Specified by:
setin interfaceStructuredWritableBuffer- Parameters:
index- The index to set the value at.value- The Byte value to set.
-
setShort
public void setShort(int index, short value)Description copied from interface:StructuredWritableBufferSets a Short value at the specified index.- Specified by:
setShortin interfaceStructuredWritableBuffer- Parameters:
index- The index to set the value at.value- The Short value to set.
-
setInt
public void setInt(int index, int value)Description copied from interface:StructuredWritableBufferSets a 32 bit Integer value at the specified index.- Specified by:
setIntin interfaceStructuredWritableBuffer- Parameters:
index- The index to set the value at.value- The Integer value to set.
-
setLong
public void setLong(int index, long value)Description copied from interface:StructuredWritableBufferSets a 64 bit Long value at the specified index.- Specified by:
setLongin interfaceStructuredWritableBuffer- Parameters:
index- The index to set the value at.value- The Long value to set.
-
copyFrom
public void copyFrom(ArrayView source, int targetOffset, int length)
Copies a specified number of bytes from the givenArrayViewinto this ByteArraySegment.- Parameters:
source- TheArrayViewto copy bytes from.targetOffset- The offset within this ByteArraySegment to start copying at.length- The number of bytes to copy.- Throws:
java.lang.ArrayIndexOutOfBoundsException- If targetOffset or length are invalid.
-
copyFrom
public void copyFrom(ArrayView source, int sourceOffset, int targetOffset, int length)
Copies a specified number of bytes from the givenArrayViewinto this ByteArraySegment.- Parameters:
source- TheArrayViewto copy bytes from.sourceOffset- The offset within source to start copying from.targetOffset- The offset within this ByteArraySegment to start copying at.length- The number of bytes to copy.- Throws:
java.lang.ArrayIndexOutOfBoundsException- If targetOffset or length are invalid.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getLength
public int getLength()
Description copied from interface:BufferViewGets a value representing the length of thisBufferView.- Specified by:
getLengthin interfaceBufferView- Returns:
- The length.
-
-