java.lang.Object
uk.ac.manchester.tornado.api.types.arrays.TornadoNativeArray
uk.ac.manchester.tornado.api.types.arrays.ByteArray
ByteArray relies on preview features of the Java platform:
ByteArrayrefers to one or more preview APIs:MemorySegment.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
This class represents an array of bytes stored in native memory.
The byte data is stored in a
MemorySegmentPREVIEW, which represents a contiguous region of off-heap memory.
The class also encapsulates methods for setting and getting byte values,
for initializing the byte array, and for converting the array to and from different representations.-
Field Summary
Fields inherited from class uk.ac.manchester.tornado.api.types.arrays.TornadoNativeArray
ARRAY_HEADER -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Sets all the values of theByteArrayinstance to zero.static ByteArraystatic ByteArrayfromArray(byte[] values) Creates a new instance of theByteArrayclass from an on-heap byte array.static ByteArrayfromByteBuffer(ByteBuffer buffer) Creates a new instance of theByteArrayclass from aByteBuffer.static ByteArrayfromElements(byte... values) Creates a new instance of theByteArrayclass from a set of byte values.static ByteArrayfromSegment(MemorySegmentPREVIEW segment) static ByteArrayfromSegmentShallow(MemorySegmentPREVIEW segment) Creates a new instance of theByteArrayclass by wrapping an existingMemorySegmentPREVIEW without copying its contents.byteget(int index) Gets the byte value stored at the specified index of theByteArrayinstance.intgetHalfFloat(int byteIndex) Gets the half-float value stored at the specified byte index within theByteArrayinstance.longReturns the number of bytes of theMemorySegmentPREVIEW that is associated with theByteArrayinstance, excluding the header bytes.longReturns the total number of bytes that theMemorySegmentPREVIEW, associated with theByteArrayinstance, occupies.intgetSize()Returns the number of elements stored in the native array.voidinit(byte value) Initializes all the elements of theByteArrayinstance with a specified value.static voidinitialize(ByteArray array, byte value) Factory method to initialize aByteArray.voidset(int index, byte value) Sets the byte value at a specified index of theByteArrayinstance.voidsetHalfFloat(int byteIndex, HalfFloat value) Sets the half-float value at the specified byte index within theByteArrayinstance.slice(int offset, int length) byte[]Converts the byte data from off-heap to on-heap, by copying the values of aByteArrayinstance into a new on-heap array.
-
Constructor Details
-
ByteArray
public ByteArray(int numberOfElements) Constructs a new instance of theByteArraythat will store a user-specified number of elements.- Parameters:
numberOfElements- The number of elements in the array.
-
ByteArray
Constructs a newByteArrayinstance by concatenating the contents of the given array ofByteArrayinstances.- Parameters:
arrays- An array ofByteArrayinstances to be concatenated into the new instance.
-
-
Method Details
-
fromArray
Creates a new instance of theByteArrayclass from an on-heap byte array.- Parameters:
values- The on-heap byte array to create the instance from.- Returns:
- A new
ByteArrayinstance, initialized with values of the on-heap byte array.
-
fromElements
Creates a new instance of theByteArrayclass from a set of byte values.- Parameters:
values- The byte values to initialize the array with.- Returns:
- A new
ByteArrayinstance, initialized with the given values.
-
fromSegment
- Parameters:
segment- TheMemorySegmentPREVIEW containing the off-heap byte data.- Returns:
- A new
ByteArrayinstance, initialized with the segment data.
-
fromSegmentShallow
Creates a new instance of theByteArrayclass by wrapping an existingMemorySegmentPREVIEW without copying its contents.- Parameters:
segment- TheMemorySegmentPREVIEW containing *both* the off-heap byte *header* and *data*.- Returns:
- A new
ByteArrayinstance that wraps the given segment.
-
fromByteBuffer
Creates a new instance of theByteArrayclass from aByteBuffer.- Parameters:
buffer- TheByteBuffercontaining the float data.- Returns:
- A new
ByteArrayinstance, initialized with the buffer data.
-
toHeapArray
public byte[] toHeapArray()Converts the byte data from off-heap to on-heap, by copying the values of aByteArrayinstance into a new on-heap array.- Returns:
- A new on-heap byte array, initialized with the values stored in the
ByteArrayinstance.
-
set
public void set(int index, byte value) Sets the byte value at a specified index of theByteArrayinstance.- Parameters:
index- The index at which to set the byte value.value- The byte value to store at the specified index.
-
setHalfFloat
Sets the half-float value at the specified byte index within theByteArrayinstance. The specifiedbyteIndexmust be aligned to a 2-byte boundary; if it is not, anIllegalArgumentExceptionwill be thrown. The method internally calculates the appropriate short index for storage and updates the underlying memory segment.- Parameters:
byteIndex- The byte index at which to set the half-float value. Must be aligned to a 2-byte boundary.value- TheHalfFloatvalue to be stored at the specified index.- Throws:
IllegalArgumentException- If thebyteIndexis not aligned to a 2-byte boundary.
-
get
public byte get(int index) Gets the byte value stored at the specified index of theByteArrayinstance.- Parameters:
index- The index of which to retrieve the byte value.- Returns:
- en element byte of the off-heap array
-
getHalfFloat
Gets the half-float value stored at the specified byte index within theByteArrayinstance. The specifiedbyteIndexmust be aligned to a 2-byte boundary; if it is not, anIllegalArgumentExceptionwill be thrown. The method internally calculates the appropriate short index for storage and retrieves the value from the underlying memory segment.- Parameters:
byteIndex- The byte index from which to retrieve the half-float value. Must be aligned to a 2-byte boundary.- Returns:
- A
HalfFloatinstance containing the value stored at the specified index. - Throws:
IllegalArgumentException- If thebyteIndexis not aligned to a 2-byte boundary.
-
clear
public void clear()Sets all the values of theByteArrayinstance to zero.- Specified by:
clearin classTornadoNativeArray
-
getElementSize
public int getElementSize()- Specified by:
getElementSizein classTornadoNativeArray
-
init
public void init(byte value) Initializes all the elements of theByteArrayinstance with a specified value.- Parameters:
value- The byte value to initialize theByteArrayinstance with.
-
getSize
public int getSize()Description copied from class:TornadoNativeArrayReturns the number of elements stored in the native array.- Specified by:
getSizein classTornadoNativeArray- Returns:
- Returns the number of byte elements stored in the
ByteArrayinstance.
-
getSegment
- Specified by:
getSegmentin classTornadoNativeArray- Returns:
- The
MemorySegmentPREVIEW associated with theByteArrayinstance.
-
getSegmentWithHeader
- Specified by:
getSegmentWithHeaderin classTornadoNativeArray- Returns:
- The
MemorySegmentPREVIEW associated with theByteArrayinstance.
-
getNumBytesOfSegmentWithHeader
public long getNumBytesOfSegmentWithHeader()Returns the total number of bytes that theMemorySegmentPREVIEW, associated with theByteArrayinstance, occupies.- Specified by:
getNumBytesOfSegmentWithHeaderin classTornadoNativeArray- Returns:
- The total number of bytes of the
MemorySegmentPREVIEW.
-
getNumBytesOfSegment
public long getNumBytesOfSegment()Returns the number of bytes of theMemorySegmentPREVIEW that is associated with theByteArrayinstance, excluding the header bytes.- Specified by:
getNumBytesOfSegmentin classTornadoNativeArray- Returns:
- The number of bytes of the raw data in the
MemorySegmentPREVIEW.
-
initialize
Factory method to initialize aByteArray. This method can be invoked from a Task-Graph.- Parameters:
array- Input Array.value- The float value to initialize theByteArrayinstance with.
-
concat
-
slice
- Parameters:
offset- The starting index from which to begin the slice, inclusive.length- The number of elements to include in the slice.- Returns:
- A new
ByteArrayinstance representing the specified slice of the original array. - Throws:
IllegalArgumentException- if the specified slice is out of the bounds of the original array.
-
ByteArraywhen preview features are enabled.