Package io.pravega.common.util
Class CompositeByteArraySegment
- java.lang.Object
-
- io.pravega.common.util.AbstractBufferView
-
- io.pravega.common.util.CompositeByteArraySegment
-
- All Implemented Interfaces:
BufferView,CompositeArrayView,StructuredWritableBuffer
public class CompositeByteArraySegment extends AbstractBufferView implements CompositeArrayView
A composite, index-based array-like structure that is made up of one or more individual arrays of equal size. Each component array maps to a contiguous offset range and is only allocated when the first index within its range needs to be set (if unallocated, any index within its range will have a value of 0).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCompositeByteArraySegment.BufferLayoutDefines the internal layout of aCompositeByteArraySegment.-
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>, BufferView.Reader
-
-
Constructor Summary
Constructors Constructor Description CompositeByteArraySegment(@lombok.NonNull byte[] source)Creates a new instance of theCompositeByteArraySegmentclass that wraps the given array.CompositeByteArraySegment(int length)Creates a new instance of theCompositeByteArraySegmentclass with a default component array size.CompositeByteArraySegment(int length, CompositeByteArraySegment.BufferLayout bufferLayout)Creates a new instance of theCompositeByteArraySegmentclass with the given component array size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <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(BufferView.Reader source, int targetOffset, int length)Copies a specified number of bytes from the givenBufferView.Readerinto thisCompositeArrayView.voidcopyTo(java.io.OutputStream target)Copies the contents of thisBufferViewto the givenOutputStream.intcopyTo(java.nio.ByteBuffer target)Copies the contents of thisBufferViewto the givenByteBuffer.byteget(int offset)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.CompositeByteArraySegment.CompositeReadergetBufferViewReader()Creates a newBufferView.Readerthat can be used to read thisBufferView.intgetComponentCount()Gets the number of components in thisCompositeArrayViewinstance.byte[]getCopy()Returns a copy of the contents of thisBufferView.intgetLength()Gets a value representing the length of thisBufferView.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.java.util.Iterator<java.nio.ByteBuffer>iterateBuffers()voidset(int offset, byte value)Sets the value at the specified index.voidsetInt(int offset, int value)Sets a 32 bit Integer value at the specified index.voidsetLong(int offset, long value)Sets a 64 bit Long value at the specified index.voidsetShort(int offset, short value)Sets a Short value at the specified index.CompositeArrayViewslice(int offset, int length)Creates a newCompositeArrayViewthat represents a sub-range of thisCompositeArrayViewinstance.-
Methods inherited from class io.pravega.common.util.AbstractBufferView
equals, equals, hashCode
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.pravega.common.util.BufferView
release, retain, slice
-
Methods inherited from interface io.pravega.common.util.StructuredWritableBuffer
setUnsignedLong
-
-
-
-
Constructor Detail
-
CompositeByteArraySegment
public CompositeByteArraySegment(int length)
Creates a new instance of theCompositeByteArraySegmentclass with a default component array size.- Parameters:
length- The length of theCompositeByteArraySegment. This will determine the number of components to use, but doesn't allocate any of them yet.
-
CompositeByteArraySegment
public CompositeByteArraySegment(int length, CompositeByteArraySegment.BufferLayout bufferLayout)Creates a new instance of theCompositeByteArraySegmentclass with the given component array size.- Parameters:
length- The length of theCompositeByteArraySegment. This will determine the number of components to use, but doesn't allocate any of them yet.bufferLayout- TheCompositeByteArraySegment.BufferLayoutto use.
-
CompositeByteArraySegment
public CompositeByteArraySegment(@NonNull @lombok.NonNull byte[] source)Creates a new instance of theCompositeByteArraySegmentclass that wraps the given array. This instance will have a single component array.- Parameters:
source- The byte array to wrap. Any changes made to this array will be reflected in thisCompositeByteArraySegmentinstance and vice-versa.
-
-
Method Detail
-
get
public byte get(int offset)
Description copied from interface:CompositeArrayViewGets the value at the specified index.- Specified by:
getin interfaceCompositeArrayView- Parameters:
offset- The index to query.- Returns:
- Byte indicating the value at the given index.
-
set
public void set(int offset, byte value)Description copied from interface:StructuredWritableBufferSets the value at the specified index.- Specified by:
setin interfaceStructuredWritableBuffer- Parameters:
offset- The index to set the value at.value- The Byte value to set.
-
setShort
public void setShort(int offset, short value)Description copied from interface:StructuredWritableBufferSets a Short value at the specified index.- Specified by:
setShortin interfaceStructuredWritableBuffer- Parameters:
offset- The index to set the value at.value- The Short value to set.
-
setInt
public void setInt(int offset, int value)Description copied from interface:StructuredWritableBufferSets a 32 bit Integer value at the specified index.- Specified by:
setIntin interfaceStructuredWritableBuffer- Parameters:
offset- The index to set the value at.value- The Integer value to set.
-
setLong
public void setLong(int offset, long value)Description copied from interface:StructuredWritableBufferSets a 64 bit Long value at the specified index.- Specified by:
setLongin interfaceStructuredWritableBuffer- Parameters:
offset- The index to set the value at.value- The Long value to set.
-
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.
-
getBufferViewReader
public io.pravega.common.util.CompositeByteArraySegment.CompositeReader 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 CompositeArrayView slice(int offset, int length)
Description copied from interface:CompositeArrayViewCreates a newCompositeArrayViewthat represents a sub-range of thisCompositeArrayViewinstance. The new instance will share the same backing part(s) as this one, so a change to one will be reflected in the other.- Specified by:
slicein interfaceBufferView- Specified by:
slicein interfaceCompositeArrayView- Parameters:
offset- The starting offset to begin the slice at.length- The sliced length.- Returns:
- A new
CompositeArrayView.
-
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.
-
getComponentCount
public int getComponentCount()
Description copied from interface:CompositeArrayViewGets the number of components in thisCompositeArrayViewinstance.- Specified by:
getComponentCountin interfaceCompositeArrayView- Returns:
- The number of components. This is the exact number of argument invocations for
BufferView.collect(Collector).
-
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.
-
copyFrom
public void copyFrom(BufferView.Reader source, int targetOffset, int length)
Description copied from interface:CompositeArrayViewCopies a specified number of bytes from the givenBufferView.Readerinto thisCompositeArrayView.- Specified by:
copyFromin interfaceCompositeArrayView- Parameters:
source- TheBufferView.Readerto copy bytes from.targetOffset- The offset within thisCompositeArrayViewto start copying at.length- The number of bytes to copy.
-
copyTo
public void copyTo(java.io.OutputStream target) throws java.io.IOExceptionDescription copied from interface:BufferViewCopies the contents of thisBufferViewto the givenOutputStream.- Specified by:
copyToin interfaceBufferView- Parameters:
target- TheOutputStreamto write to.- Throws:
java.io.IOException- If an exception occurred while writing to the targetOutputStream.
-
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.
-
getLength
public int getLength()
Description copied from interface:BufferViewGets a value representing the length of thisBufferView.- Specified by:
getLengthin interfaceBufferView- Returns:
- The length.
-
-