Package io.pravega.common.util
Interface BufferView
-
- All Known Subinterfaces:
ArrayView,CompositeArrayView,StructuredReadableBuffer,StructuredWritableBuffer
- All Known Implementing Classes:
AbstractBufferView,ByteArraySegment,CompositeByteArraySegment
public interface BufferViewDefines a generic read-only view of a readable memory buffer with a known length. For array-backed Buffers, seeArrayView. For any implementations that wrap direct memory (aByteBufferor Netty ByteBuf and thus support reference counting, consider usingretain()release()to ensure the underlying buffer is correctly managed. Invokeretain()if thisBufferViewinstance is to be needed for more than the buffer creator anticipates (i.e., a background async task) and invokerelease()to notify that it is no longer needed. The behavior of these two methods are dependent on the actual buffer implementation; for example, Netty ByteBufs only release the memory once the internal reference count reaches 0 (refer to Netty ByteBuf documentation for more details).
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceBufferView.Collector<ExceptionT extends java.lang.Exception>Defines a collector function that can be applied to a ByteBuffer.static interfaceBufferView.ReaderDefines a reader for aBufferView.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static BufferViewBuilderbuilder()Creates a newBufferViewBuilderthat can be used to construct compositeBufferViewinstances.static BufferViewBuilderbuilder(int expectedComponentCount)Creates a newBufferViewBuilderthat can be used to construct compositeBufferViewinstances.<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.voidcopyTo(java.io.OutputStream target)Copies the contents of thisBufferViewto the givenOutputStream.intcopyTo(java.nio.ByteBuffer byteBuffer)Copies the contents of thisBufferViewto the givenByteBuffer.static BufferViewempty()Returns the emptyBufferView.BufferView.ReadergetBufferViewReader()Creates a newBufferView.Readerthat can be used to read thisBufferView.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()default voidrelease()When implemented in a derived class, notifies any wrapped buffer that thisBufferViewno longer has a need for it.default voidretain()When implemented in a derived class, notifies any wrapped buffer that thisBufferViewhas a need for it.default BufferViewslice()Equivalent to invokingslice(int, int)with offset 0 and getLength().BufferViewslice(int offset, int length)Creates a newBufferViewthat represents a sub-range of thisBufferViewinstance.static BufferViewwrap(java.util.List<BufferView> components)Wraps the givenBufferViewinto a single instance.
-
-
-
Method Detail
-
getLength
int getLength()
Gets a value representing the length of thisBufferView.- Returns:
- The length.
-
getBufferViewReader
BufferView.Reader getBufferViewReader()
Creates a newBufferView.Readerthat can be used to read thisBufferView. This reader is preferable togetReader()that returns anInputStreamas it contains optimized methods for copying directly into otherBufferViewinstances, such asByteArraySegments.- Returns:
- A new
BufferView.Reader.
-
getReader
java.io.InputStream getReader()
Creates an InputStream that can be used to read the contents of thisBufferView. The InputStream returned spans the entireBufferView.- Returns:
- The InputStream.
-
getReader
java.io.InputStream getReader(int offset, int length)Creates an InputStream that can be used to read the contents of thisBufferView.- Parameters:
offset- The starting offset of the section to read.length- The length of the section to read.- Returns:
- The InputStream.
-
slice
default BufferView slice()
Equivalent to invokingslice(int, int)with offset 0 and getLength(). Depending on the implementation, this may return this instance or a new instance that is a duplicate of this one but pointing to the same backing buffer. No data copies are being made as part of invoking this method.- Returns:
- A
BufferView.
-
slice
BufferView slice(int offset, int length)
Creates a newBufferViewthat represents a sub-range of thisBufferViewinstance. The new instance will share the same backing buffer as this one, so a change to one will be reflected in the other.- Parameters:
offset- The starting offset to begin the slice at.length- The sliced length.- Returns:
- A new
BufferView.
-
getCopy
byte[] getCopy()
Returns a copy of the contents of thisBufferView.- Returns:
- A byte array with the same length as this ArrayView, containing a copy of the data within it.
-
copyTo
void copyTo(java.io.OutputStream target) throws java.io.IOExceptionCopies the contents of thisBufferViewto the givenOutputStream.- Parameters:
target- TheOutputStreamto write to.- Throws:
java.io.IOException- If an exception occurred while writing to the targetOutputStream.
-
copyTo
int copyTo(java.nio.ByteBuffer byteBuffer)
Copies the contents of thisBufferViewto the givenByteBuffer.- Parameters:
byteBuffer- 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 usingslice()to select a sub-range of thisBufferView.- Returns:
- The number of bytes copied.
-
retain
default void retain()
When implemented in a derived class, notifies any wrapped buffer that thisBufferViewhas a need for it. Userelease()to do the opposite. See the main documentation on this interface for recommendations on how to use these to methods. Also refer to the implementing class' documentation for any additional details.
-
release
default void release()
When implemented in a derived class, notifies any wrapped buffer that thisBufferViewno longer has a need for it. After invoking this method, this object should no longer be considered safe to access as the underlying buffer may have been deallocated (the actual behavior may vary based on the wrapped buffer, please refer to the implementing class' documentation for any additional details).
-
collect
<ExceptionT extends java.lang.Exception> void collect(BufferView.Collector<ExceptionT> bufferCollector) throws ExceptionT extends java.lang.Exception
Iterates through each of the buffers that make up thisBufferView, in order, and invokes the givenBufferView.Collectoron each.- 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
java.util.Iterator<java.nio.ByteBuffer> iterateBuffers()
- Returns:
- A
Iterator.
-
wrap
static BufferView wrap(java.util.List<BufferView> components)
Wraps the givenBufferViewinto a single instance.- Parameters:
components- The components to wrap. These components will be added by reference, without making any data copies. Any modifications made to these components will be reflected in the returnedBufferViewand vice-versa.- Returns:
- An empty
BufferView(if the component list is empty), the first item in the list (if the component list has 1 element) or aCompositeBufferViewwrapping all the given instances otherwise.
-
builder
static BufferViewBuilder builder()
Creates a newBufferViewBuilderthat can be used to construct compositeBufferViewinstances.- Returns:
- A new
BufferViewBuilderwith default initial component count.
-
builder
static BufferViewBuilder builder(int expectedComponentCount)
Creates a newBufferViewBuilderthat can be used to construct compositeBufferViewinstances.- Parameters:
expectedComponentCount- The initial component count. Knowing this value beforehand will avoid any list copies that are done as the builder component list grows.- Returns:
- A new
BufferViewBuilderwith specified initial component count.
-
empty
static BufferView empty()
Returns the emptyBufferView.- Returns:
- The empty
BufferView.
-
-