Class ByteBuf
- Direct Known Subclasses:
ByteArrayBuf,ByteItemBuf,ByteMatrixBuf,EmptyByteBuf,SharedByteArrayBuf,SharedByteBuf
A buffer may be used to send one or more messages at the same time in multiple threads. If a buffer is being used to send a message or messages, the buffer must not be used to receive a message at the same time.
A buffer may be used to receive one message at a time. If a buffer is being used to receive a message, the buffer must not be used to receive another message in a different thread, and the buffer must not be used to send a message or messages.
A buffer is a conduit for retrieving and storing data in some underlying data structure. If the underlying data structure is multiple thread safe, then one thread can be retrieving or storing data via the buffer at the same time as other threads are accessing the data structure. If the underlying data structure is not multiple thread safe, then other threads must not access the data structure while one thread is retrieving or storing data via the buffer.
To create a ByteBuf, call one of the following static factory methods:
emptyBuffer()buffer()buffer (byte)buffer (byte[])sliceBuffer (byte[], Range)sliceBuffers (byte[], Range[])buffer (byte[][])rowSliceBuffer (byte[][], Range)rowSliceBuffers (byte[][], Range[])colSliceBuffer (byte[][], Range)colSliceBuffers (byte[][], Range[])patchBuffer (byte[][], Range, Range)patchBuffers (byte[][], Range[], Range[])buffer (SharedByte)buffer (SharedByteArray)sliceBuffer (SharedByteArray, Range)sliceBuffers (SharedByteArray, Range[])
- Version:
- 03-May-2008
- Author:
- Alan Kaminsky
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ByteItemBufbuffer()Create a buffer for a byte item.static ByteItemBufbuffer(byte item) Create a buffer for a byte item with the given initial value.static ByteBufbuffer(byte[] theArray) Create a buffer for the entire given byte array.static ByteBufbuffer(byte[][] theMatrix) Create a buffer for the entire given byte matrix.static ByteBufbuffer(SharedByte item) Create a buffer for a shared byte item.static ByteBufbuffer(SharedByteArray theArray) Create a buffer for the entire given shared byte array.static ByteBufcolSliceBuffer(byte[][] theMatrix, Range theColRange) Create a buffer for one column slice of the given byte matrix.static ByteBuf[]colSliceBuffers(byte[][] theMatrix, Range[] theColRanges) Create an array of buffers for multiple column slices of the given byte matrix.voidCopy items from the given buffer to this buffer.protected static voiddefaultCopy(ByteBuf theSrc, ByteBuf theDst) Copy items from the given source buffer to the given destination buffer.static ByteBufCreate an empty buffer.voidFill this buffer with the given item.abstract byteget(int i) Obtain the given item from this buffer.Create a temporary buffer with the same type of items and the same length as this buffer.static ByteBufpatchBuffer(byte[][] theMatrix, Range theRowRange, Range theColRange) Create a buffer for one patch of the given byte matrix.static ByteBuf[]patchBuffers(byte[][] theMatrix, Range[] theRowRanges, Range[] theColRanges) Create an array of buffers for multiple patches of the given byte matrix.abstract voidput(int i, byte item) Store the given item in this buffer.static ByteBufrowSliceBuffer(byte[][] theMatrix, Range theRowRange) Create a buffer for one row slice of the given byte matrix.static ByteBuf[]rowSliceBuffers(byte[][] theMatrix, Range[] theRowRanges) Create an array of buffers for multiple row slices of the given byte matrix.static ByteBufsliceBuffer(byte[] theArray, Range theRange) Create a buffer for one slice of the given byte array.static ByteBufsliceBuffer(SharedByteArray theArray, Range theRange) Create a buffer for one slice of the given shared byte array.static ByteBuf[]sliceBuffers(byte[] theArray, Range[] theRanges) Create an array of buffers for multiple slices of the given byte array.static ByteBuf[]sliceBuffers(SharedByteArray theArray, Range[] theRanges) Create an array of buffers for multiple slices of the given shared byte array.Methods inherited from class edu.rit.mp.Buf
getReductionBuf, length, receiveItems, sendItems
-
Constructor Details
-
ByteBuf
protected ByteBuf(int theLength) Construct a new byte buffer.- Parameters:
theLength- Number of items.- Throws:
IllegalArgumentException- (unchecked exception) Thrown iftheLength< 0.
-
-
Method Details
-
emptyBuffer
Create an empty buffer. The buffer's length is 0. The buffer's item type is byte.- Returns:
- Empty buffer.
-
buffer
Create a buffer for a byte item. The item is stored in theitemfield of the buffer.- Returns:
- Buffer.
-
buffer
Create a buffer for a byte item with the given initial value. The item is stored in theitemfield of the buffer.- Parameters:
item- Initial value of theitemfield.- Returns:
- Buffer.
-
buffer
Create a buffer for the entire given byte array. The returned buffer encompasses all the elements intheArray.- Parameters:
theArray- Array.- Returns:
- Buffer.
- Throws:
NullPointerException- (unchecked exception) Thrown iftheArrayis null.
-
sliceBuffer
Create a buffer for one slice of the given byte array. The returned buffer encompassestheRangeof elements intheArray. The range's stride may be 1 or greater than 1.- Parameters:
theArray- Array.theRange- Range of elements to include.- Returns:
- Buffer.
- Throws:
NullPointerException- (unchecked exception) Thrown iftheArrayis null ortheRangeis null.IndexOutOfBoundsException- (unchecked exception) Thrown iftheArraydoes not include all the indexes intheRange.
-
sliceBuffers
Create an array of buffers for multiple slices of the given byte array. The returned buffer array has the same length astheRanges. Each element [i] of the returned buffer array encompasses the elements oftheArrayspecified bytheRanges[i]. Each range's stride may be 1 or greater than 1.- Parameters:
theArray- Array.theRanges- Array of ranges of elements to include.- Returns:
- Array of buffers.
- Throws:
NullPointerException- (unchecked exception) Thrown iftheArrayis null ortheRangesor any element thereof is null.IndexOutOfBoundsException- (unchecked exception) Thrown iftheArray's allocation does not include any element oftheRanges.
-
buffer
Create a buffer for the entire given byte matrix. The returned buffer encompasses all the rows and all the columns intheMatrix.- Parameters:
theMatrix- Matrix.- Returns:
- Buffer.
- Throws:
NullPointerException- (unchecked exception) Thrown iftheMatrixis null.
-
rowSliceBuffer
Create a buffer for one row slice of the given byte matrix. The returned buffer encompassestheRowRangeof rows, and all the columns, intheMatrix. The range's stride may be 1 or greater than 1.- Parameters:
theMatrix- Matrix.theRowRange- Range of rows to include.- Returns:
- Buffer.
- Throws:
NullPointerException- (unchecked exception) Thrown iftheMatrixis null ortheRowRangeis null.IndexOutOfBoundsException- (unchecked exception) Thrown iftheMatrix's allocation does not includetheRowRange.
-
rowSliceBuffers
Create an array of buffers for multiple row slices of the given byte matrix. The returned buffer array has the same length astheRowRanges. Each element [i] of the returned buffer array encompasses the rows oftheMatrixspecified bytheRowRanges[i]and all the columns oftheMatrix. Each range's stride may be 1 or greater than 1.- Parameters:
theMatrix- Matrix.theRowRanges- Array of ranges of rows to include.- Returns:
- Array of buffers.
- Throws:
NullPointerException- (unchecked exception) Thrown iftheMatrixis null ortheRowRangesor any element thereof is null.IndexOutOfBoundsException- (unchecked exception) Thrown iftheMatrix's allocation does not include any element oftheRowRanges.
-
colSliceBuffer
Create a buffer for one column slice of the given byte matrix. The returned buffer encompasses all the rows, andtheColRangeof columns, intheMatrix. The range's stride may be 1 or greater than 1.- Parameters:
theMatrix- Matrix.theColRange- Range of columns to include.- Returns:
- Buffer.
- Throws:
NullPointerException- (unchecked exception) Thrown iftheMatrixis null ortheColRangeis null.IndexOutOfBoundsException- (unchecked exception) Thrown iftheMatrix's allocation does not includetheColRange.
-
colSliceBuffers
Create an array of buffers for multiple column slices of the given byte matrix. The returned buffer array has the same length astheColRanges. Each element [i] of the returned buffer array encompasses all the rows oftheMatrixand the columns oftheMatrixspecified bytheColRanges[i]. Each range's stride may be 1 or greater than 1.- Parameters:
theMatrix- Matrix.theColRanges- Array of ranges of columns to include.- Returns:
- Array of buffers.
- Throws:
NullPointerException- (unchecked exception) Thrown iftheMatrixis null ortheColRangesor any element thereof is null.IndexOutOfBoundsException- (unchecked exception) Thrown iftheMatrix's allocation does not include any element oftheColRanges.
-
patchBuffer
Create a buffer for one patch of the given byte matrix. The returned buffer encompassestheRowRangeof rows, andtheColRangeof columns, intheMatrix. Each range's stride may be 1 or greater than 1.- Parameters:
theMatrix- Matrix.theRowRange- Range of rows to include.theColRange- Range of columns to include.- Returns:
- Buffer.
- Throws:
NullPointerException- (unchecked exception) Thrown iftheMatrixis null,theRowRangeis null, ortheColRangeis null.IndexOutOfBoundsException- (unchecked exception) Thrown iftheMatrix's allocation does not includetheRowRangeandtheColRange.
-
patchBuffers
public static ByteBuf[] patchBuffers(byte[][] theMatrix, Range[] theRowRanges, Range[] theColRanges) Create an array of buffers for multiple patches of the given byte matrix. The length of the returned buffer array is equal to the length oftheRowRangestimes the length oftheColRanges. Each element of the returned buffer array encompasses the rows given in one element oftheRowRangesarray, and the columns given in one element oftheColRangesarray, in all possible combinations, oftheMatrix. Each range's stride may be 1 or greater than 1.- Parameters:
theMatrix- Matrix.theRowRanges- Array of ranges of rows to include.theColRanges- Array of ranges of columns to include.- Returns:
- Array of buffers.
- Throws:
NullPointerException- (unchecked exception) Thrown iftheMatrixis null,theRowRangesor any element thereof is null, ortheColRangesor any element thereof is null.IndexOutOfBoundsException- (unchecked exception) Thrown iftheMatrix's allocation does not include any element oftheRowRangesortheColRanges.
-
get
public abstract byte get(int i) Obtain the given item from this buffer.The
get()method must not block the calling thread; if it does, all message I/O in MP will be blocked.- Parameters:
i- Item index in the range 0 ..length()-1.- Returns:
- Item at index
i.
-
put
public abstract void put(int i, byte item) Store the given item in this buffer.The
put()method must not block the calling thread; if it does, all message I/O in MP will be blocked.- Parameters:
i- Item index in the range 0 ..length()-1.item- Item to be stored at indexi.
-
copy
Copy items from the given buffer to this buffer. The number of items copied is this buffer's length ortheSrc's length, whichever is smaller. IftheSrcis this buffer, thecopy()method does nothing. Copy items from the given buffer to this buffer. The number of items copied is this buffer's length ortheSrc's length, whichever is smaller. IftheSrcis this buffer, thecopy()method does nothing.The default implementation of the
copy()method calls thedefaultCopy()method. A subclass can override thecopy()method to use a more efficient algorithm.- Specified by:
copyin classBuf- Parameters:
theSrc- Source of items to copy into this buffer.- Throws:
ClassCastException- (unchecked exception) Thrown iftheSrc's item data type is not the same as this buffer's item data type.
-
fill
Fill this buffer with the given item. Theitemis assigned to each element in this buffer.If this buffer's item data type is a primitive type, the
itemmust be an instance of the corresponding primitive wrapper class -- class Integer for typeint, class Double for typedouble, and so on. If theitemis null, the item data type's default initial value is assigned to each element in this buffer.If this buffer's item data type is a nonprimitive type, the
itemmust be an instance of the item class or a subclass thereof. Theitemmay be null. Note that sinceitemis assigned to every buffer element, every buffer element ends up referring to the sameitem. Fill this buffer with the given item. Theitemis assigned to each element in this buffer.The
itemmust be an instance of class Byte. If theitemis null, 0 is assigned to each element in this buffer.- Specified by:
fillin classBuf- Parameters:
item- Item.- Throws:
ClassCastException- (unchecked exception) Thrown if theitem's data type is not the same as this buffer's item data type.
-
getTemporaryBuf
Create a temporary buffer with the same type of items and the same length as this buffer. The new buffer items are stored in a newly created array, separate from the storage for this buffer's items.- Specified by:
getTemporaryBufin classBuf- Returns:
- a
Bufobject.
-
defaultCopy
Copy items from the given source buffer to the given destination buffer. The number of items copied istheSrc's length ortheDst's length, whichever is smaller. Each item is copied individually using theget()andput()methods. It is assumed thattheSrcis not the same astheDst.- Parameters:
theSrc- Source of items to copy.theDst- Destination of items to copy.
-