Class IBuffer
- java.lang.Object
-
- com.avpkit.ferry.RefCounted
-
- com.avpkit.ferry.IBuffer
-
public class IBuffer extends RefCounted
Allows Java code to get data from a native buffers, and optionally modify native memory directly.
When accessing from Java, you can copy in and
out ranges of buffers. You can do this by-copy
(which is safer but a little slower) or by-reference
where you directly access underlying C++/native
memory from Java. Take special care if you decide
that native access is required.
When accessing from C++, you get direct access to
the underlying buffer.
To make an IBuffer object that is not a AVPKit internal object,
pass in null for the RefCounted parameter.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classIBuffer.Type
-
Field Summary
-
Fields inherited from class com.avpkit.ferry.RefCounted
swigCMemOwn
-
-
Constructor Summary
Constructors Modifier Constructor Description IBuffer(long ignore1, boolean ignore2)DO NOT USE THIS CONSTRUCTOR - USEmake(RefCounted,int)INSTEAD.protectedIBuffer(long cPtr, boolean cMemoryOwn, java.util.concurrent.atomic.AtomicLong ref)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description IBuffercopyReference()Create a new IBuffer object that is actually referring to the exact same underlying Native object.voiddelete()Releases ths instance of IBuffer and frees any underlying native memory.booleanequals(java.lang.Object obj)Compares two values, returning true if the underlying objects in native code are the same object.voidget(int srcPos, byte[] dest, int destPos, int length)Absolute bulk get method.voidget(int srcPos, char[] dest, int destPos, int length)Absolute bulk get method.voidget(int srcPos, double[] dest, int destPos, int length)Absolute bulk get method.voidget(int srcPos, float[] dest, int destPos, int length)Absolute bulk get method.voidget(int srcPos, int[] dest, int destPos, int length)Absolute bulk get method.voidget(int srcPos, long[] dest, int destPos, int length)Absolute bulk get method.voidget(int srcPos, short[] dest, int destPos, int length)Absolute bulk get method.intgetBufferSize()Get the current maximum number of bytes that can
be safely placed in this buffer.
byte[]getByteArray(int offset, int length)Returns up to length bytes, starting at offset in the
underlying buffer we're managing.java.nio.ByteBuffergetByteBuffer(int offset, int length)Returns up to length bytes, starting at offset in the underlying buffer we're managing.java.nio.ByteBuffergetByteBuffer(int offset, int length, java.util.concurrent.atomic.AtomicReference<JNIReference> referenceReturn)Returns up to length bytes, starting at offset in the underlying buffer we're managing, and also passed back aJNIReferencethat can optionally be used by the caller to free the underlying native memory.static longgetCPtr(IBuffer obj)Internal Only.longgetMyCPtr()Internal Only.intgetSize()Returns the size, in units of {#getType()} of
this buffer.
IBuffer.TypegetType()Get the type this buffer was created as.static intgetTypeSize(IBuffer.Type type)Returns the size, in bytes, of elements of given Type.
inthashCode()Get a hashable value for this object.java.nio.ByteBufferjava_getByteBuffer(int offset, int length)static IBuffermake(RefCounted requestor, byte[] buffer, int offset, int length)Allocate a new IBuffer, and copy the data in buffer into
the new IBuffer object.
static IBuffermake(RefCounted requestor, int bufferSize)Allocate a new buffer of at least bufferSize.
static IBuffermake(RefCounted requestor, IBuffer.Type type, int numElements, boolean zero)Allocate a new buffer of at least bufferSize.
static IBuffermake(RefCounted requestor, java.nio.ByteBuffer directByteBuffer, int offset, int length)Create a new IBuffer object that uses the direct byte buffer
passed in by reference (i.e.voidput(byte[] src, int srcPos, int destPos, int length)Absolute bulk put method.voidput(char[] src, int srcPos, int destPos, int length)Absolute bulk put method.voidput(double[] src, int srcPos, int destPos, int length)Absolute bulk put method.voidput(float[] src, int srcPos, int destPos, int length)Absolute bulk put method.voidput(int[] src, int srcPos, int destPos, int length)Absolute bulk put method.voidput(long[] src, int srcPos, int destPos, int length)Absolute bulk put method.voidput(short[] src, int srcPos, int destPos, int length)Absolute bulk put method.voidsetType(IBuffer.Type type)Reset the buffer type to a new type.java.lang.StringtoString()Prints the same asObject.toString()but appends the maximum number of bytes that will fit in thisIBuffer, the type of theIBuffer, and the maximum size in units ofIBuffer.Typethat will fit in theIBuffer.-
Methods inherited from class com.avpkit.ferry.RefCounted
acquire, getCPtr, getCurrentRefCount, getJavaRefCount, release
-
-
-
-
Constructor Detail
-
IBuffer
public IBuffer(long ignore1, boolean ignore2)
DO NOT USE THIS CONSTRUCTOR - USEmake(RefCounted,int)INSTEAD.Internal Only. Do not allocate this object using new. Not part of public API.
Unfortunately this constructor is public because the internal implementation needs it to be, but do not pass in values to this method as you may end up crashing the virtual machine.
- Parameters:
ignore1- ignore.ignore2- ignore.
-
IBuffer
protected IBuffer(long cPtr, boolean cMemoryOwn, java.util.concurrent.atomic.AtomicLong ref)
-
-
Method Detail
-
getCPtr
public static long getCPtr(IBuffer obj)
Internal Only. Not part of public API. Get the raw value of the native object that obj is proxying for.- Parameters:
obj- The java proxy object for a native object.- Returns:
- The raw pointer obj is proxying for.
-
getMyCPtr
public long getMyCPtr()
Internal Only. Not part of public API. Get the raw value of the native object that we're proxying for.- Overrides:
getMyCPtrin classRefCounted- Returns:
- The raw pointer we're proxying for.
-
delete
public void delete()
Releases ths instance of IBuffer and frees any underlying native memory.Releases any underlying native memory and marks this object as invalid.
Normally Ferry manages when to release native memory.
In the unlikely event you want to control EXACTLY when a native object is released, each AVPKit object has a
RefCounted.delete()method that you can use. Once you callRefCounted.delete(), you must ENSURE your object is never referenced again from that Java object -- Ferry tries to help you avoid crashes if you accidentally use an object after deletion but on this but we cannot offer 100% protection (specifically if another thread is accessing that object EXACTLY when youRefCounted.delete()it).- Overrides:
deletein classRefCounted
-
copyReference
public IBuffer copyReference()
Create a new IBuffer object that is actually referring to the exact same underlying Native object.- Overrides:
copyReferencein classRefCounted- Returns:
- the new Java object.
-
equals
public boolean equals(java.lang.Object obj)
Compares two values, returning true if the underlying objects in native code are the same object. That means you can have two different Java objects, but when you do a comparison, you'll find out they are the EXACT same object.- Overrides:
equalsin classjava.lang.Object- Returns:
- True if the underlying native object is the same. False otherwise.
-
hashCode
public int hashCode()
Get a hashable value for this object.- Overrides:
hashCodein classjava.lang.Object- Returns:
- the hashable value.
-
put
public void put(byte[] src, int srcPos, int destPos, int length)
Absolute bulk put method.This method transfers bytes into this buffer from the given source array. If there are more bytes to be copied from the array than there is space remaining at the specified destination offset, then no bytes are transferred and a java.nio.BufferOverflowException is thrown.
This method is equivalent to calling
getByteBuffer(int, int)yourself and copying the bytes over, but is more efficient in theJNIMemoryManager.MemoryModel.NATIVE_BUFFERSmemory model.- Parameters:
src- The array from which bytes are to be read. Must be non null.srcPos- The offset withinsrcof the first byte to be read; must be non-negative and less thansrc.lengthdestPos- The offset in the IBuffer where you want to copy data to. Must be non-negative and less thangetBufferSize()length- The number of bytes to be read fromsrc; must be non-negative and no larger thansrc.length - srcPos- Throws:
java.lang.NullPointerException- ifsrcis null.java.lang.IndexOutOfBoundsException- if the preconditions on the arguments mentioned above are not honored.java.nio.BufferOverflowException- if there is insufficient space in thisIBuffer.
-
get
public void get(int srcPos, byte[] dest, int destPos, int length)
Absolute bulk get method.This method transfers bytes from this
IBufferinto the givendestarray. If there are fewer bytes in theIBufferstarting at positionsrcPosthan are required to satisfy the request, then no bytes are transferred and a BufferUnderflowException is thrown.This method is equivalent to calling
getByteBuffer(int, int)yourself and reading the bytes out, but is more efficient in theJNIMemoryManager.MemoryModel.NATIVE_BUFFERSmemory model.- Parameters:
srcPos- The offset in the IBuffer where you want to start copying data from. Must be non-negative and less thangetBufferSize()dest- The array to which bytes are to be written. Must be non null.destPos- The offset withindestof the first byte to be written; must be non-negative and less thandest.lengthlength- The number of bytes to be copied intodest; must be non-negative and no larger thandest.length - destPos- Throws:
java.lang.NullPointerException- ifdestis null.java.lang.IndexOutOfBoundsException- if the preconditions on the arguments mentioned above are not honored.java.nio.BufferOverflowException- if there is insufficient data in thisIBufferto satisfy the request.
-
put
public void put(short[] src, int srcPos, int destPos, int length)
Absolute bulk put method.This method transfers shorts into this buffer from the given source array. If there are more shorts to be copied from the array than there is space remaining at the specified destination offset, then no shorts are transferred and a java.nio.BufferOverflowException is thrown.
This method is equivalent to calling
getByteBuffer(int, int)yourself and copying the bytes over, but is more efficient in theJNIMemoryManager.MemoryModel.NATIVE_BUFFERSmemory model.- Parameters:
src- The array from which shorts are to be read. Must be non null.srcPos- The offset withinsrcof the first short to be read; must be non-negative and less thansrc.lengthdestPos- The offset in the IBuffer where you want to copy data to. Must be non-negative and less thangetBufferSize()length- The number of shorts to be read fromsrc; must be non-negative and no larger thansrc.length - srcPos- Throws:
java.lang.NullPointerException- ifsrcis null.java.lang.IndexOutOfBoundsException- if the preconditions on the arguments mentioned above are not honored.java.nio.BufferOverflowException- if there is insufficient space in thisIBuffer.
-
get
public void get(int srcPos, short[] dest, int destPos, int length)
Absolute bulk get method.This method transfers shorts from this
IBufferinto the givendestarray. If there are fewer shorts in theIBufferstarting at positionsrcPosthan are required to satisfy the request, then no shorts are transferred and a BufferUnderflowException is thrown.This method is equivalent to calling
getByteBuffer(int, int)yourself and reading the bytes out, but is more efficient in theJNIMemoryManager.MemoryModel.NATIVE_BUFFERSmemory model.- Parameters:
srcPos- The offset in the IBuffer where you want to start copying data from. Must be non-negative and less thangetBufferSize()dest- The array to which shorts are to be written. Must be non null.destPos- The offset withindestof the first short to be written; must be non-negative and less thandest.lengthlength- The number of shorts to be copied intodest; must be non-negative and no larger thandest.length - destPos- Throws:
java.lang.NullPointerException- ifdestis null.java.lang.IndexOutOfBoundsException- if the preconditions on the arguments mentioned above are not honored.java.nio.BufferOverflowException- if there is insufficient data in thisIBufferto satisfy the request.
-
put
public void put(int[] src, int srcPos, int destPos, int length)
Absolute bulk put method.This method transfers ints into this buffer from the given source array. If there are more ints to be copied from the array than there is space remaining at the specified destination offset, then no ints are transferred and a java.nio.BufferOverflowException is thrown.
This method is equivalent to calling
getByteBuffer(int, int)yourself and copying the bytes over, but is more efficient in theJNIMemoryManager.MemoryModel.NATIVE_BUFFERSmemory model.- Parameters:
src- The array from which ints are to be read. Must be non null.srcPos- The offset withinsrcof the first int to be read; must be non-negative and less thansrc.lengthdestPos- The offset in the IBuffer where you want to copy data to. Must be non-negative and less thangetBufferSize()length- The number of ints to be read fromsrc; must be non-negative and no larger thansrc.length - srcPos- Throws:
java.lang.NullPointerException- ifsrcis null.java.lang.IndexOutOfBoundsException- if the preconditions on the arguments mentioned above are not honored.java.nio.BufferOverflowException- if there is insufficient space in thisIBuffer.
-
get
public void get(int srcPos, int[] dest, int destPos, int length)
Absolute bulk get method.This method transfers ints from this
IBufferinto the givendestarray. If there are fewer ints in theIBufferstarting at positionsrcPosthan are required to satisfy the request, then no ints are transferred and a BufferUnderflowException is thrown.This method is equivalent to calling
getByteBuffer(int, int)yourself and reading the bytes out, but is more efficient in theJNIMemoryManager.MemoryModel.NATIVE_BUFFERSmemory model.- Parameters:
srcPos- The offset in the IBuffer where you want to start copying data from. Must be non-negative and less thangetBufferSize()dest- The array to which ints are to be written. Must be non null.destPos- The offset withindestof the first int to be written; must be non-negative and less thandest.lengthlength- The number of ints to be copied intodest; must be non-negative and no larger thandest.length - destPos- Throws:
java.lang.NullPointerException- ifdestis null.java.lang.IndexOutOfBoundsException- if the preconditions on the arguments mentioned above are not honored.java.nio.BufferOverflowException- if there is insufficient data in thisIBufferto satisfy the request.
-
put
public void put(long[] src, int srcPos, int destPos, int length)
Absolute bulk put method.This method transfers longs into this buffer from the given source array. If there are more longs to be copied from the array than there is space remaining at the specified destination offset, then no longs are transferred and a java.nio.BufferOverflowException is thrown.
This method is equivalent to calling
getByteBuffer(int, int)yourself and copying the bytes over, but is more efficient in theJNIMemoryManager.MemoryModel.NATIVE_BUFFERSmemory model.- Parameters:
src- The array from which longs are to be read. Must be non null.srcPos- The offset withinsrcof the first long to be read; must be non-negative and less thansrc.lengthdestPos- The offset in the IBuffer where you want to copy data to. Must be non-negative and less thangetBufferSize()length- The number of longs to be read fromsrc; must be non-negative and no larger thansrc.length - srcPos- Throws:
java.lang.NullPointerException- ifsrcis null.java.lang.IndexOutOfBoundsException- if the preconditions on the arguments mentioned above are not honored.java.nio.BufferOverflowException- if there is insufficient space in thisIBuffer.
-
get
public void get(int srcPos, long[] dest, int destPos, int length)
Absolute bulk get method.This method transfers longs from this
IBufferinto the givendestarray. If there are fewer longs in theIBufferstarting at positionsrcPosthan are required to satisfy the request, then no longs are transferred and a BufferUnderflowException is thrown.This method is equivalent to calling
getByteBuffer(int, int)yourself and reading the bytes out, but is more efficient in theJNIMemoryManager.MemoryModel.NATIVE_BUFFERSmemory model.- Parameters:
srcPos- The offset in the IBuffer where you want to start copying data from. Must be non-negative and less thangetBufferSize()dest- The array to which longs are to be written. Must be non null.destPos- The offset withindestof the first long to be written; must be non-negative and less thandest.lengthlength- The number of longs to be copied intodest; must be non-negative and no larger thandest.length - destPos- Throws:
java.lang.NullPointerException- ifdestis null.java.lang.IndexOutOfBoundsException- if the preconditions on the arguments mentioned above are not honored.java.nio.BufferOverflowException- if there is insufficient data in thisIBufferto satisfy the request.
-
put
public void put(double[] src, int srcPos, int destPos, int length)
Absolute bulk put method.This method transfers doubles into this buffer from the given source array. If there are more doubles to be copied from the array than there is space remaining at the specified destination offset, then no doubles are transferred and a java.nio.BufferOverflowException is thrown.
This method is equivalent to calling
getByteBuffer(int, int)yourself and copying the bytes over, but is more efficient in theJNIMemoryManager.MemoryModel.NATIVE_BUFFERSmemory model.- Parameters:
src- The array from which doubles are to be read. Must be non null.srcPos- The offset withinsrcof the first double to be read; must be non-negative and less thansrc.lengthdestPos- The offset in the IBuffer where you want to copy data to. Must be non-negative and less thangetBufferSize()length- The number of doubles to be read fromsrc; must be non-negative and no larger thansrc.length - srcPos- Throws:
java.lang.NullPointerException- ifsrcis null.java.lang.IndexOutOfBoundsException- if the preconditions on the arguments mentioned above are not honored.java.nio.BufferOverflowException- if there is insufficient space in thisIBuffer.
-
get
public void get(int srcPos, double[] dest, int destPos, int length)
Absolute bulk get method.This method transfers doubles from this
IBufferinto the givendestarray. If there are fewer doubles in theIBufferstarting at positionsrcPosthan are required to satisfy the request, then no doubles are transferred and a BufferUnderflowException is thrown.This method is equivalent to calling
getByteBuffer(int, int)yourself and reading the bytes out, but is more efficient in theJNIMemoryManager.MemoryModel.NATIVE_BUFFERSmemory model.- Parameters:
srcPos- The offset in the IBuffer where you want to start copying data from. Must be non-negative and less thangetBufferSize()dest- The array to which doubles are to be written. Must be non null.destPos- The offset withindestof the first double to be written; must be non-negative and less thandest.lengthlength- The number of doubles to be copied intodest; must be non-negative and no larger thandest.length - destPos- Throws:
java.lang.NullPointerException- ifdestis null.java.lang.IndexOutOfBoundsException- if the preconditions on the arguments mentioned above are not honored.java.nio.BufferOverflowException- if there is insufficient data in thisIBufferto satisfy the request.
-
put
public void put(float[] src, int srcPos, int destPos, int length)
Absolute bulk put method.This method transfers floats into this buffer from the given source array. If there are more floats to be copied from the array than there is space remaining at the specified destination offset, then no floats are transferred and a java.nio.BufferOverflowException is thrown.
This method is equivalent to calling
getByteBuffer(int, int)yourself and copying the bytes over, but is more efficient in theJNIMemoryManager.MemoryModel.NATIVE_BUFFERSmemory model.- Parameters:
src- The array from which floats are to be read. Must be non null.srcPos- The offset withinsrcof the first float to be read; must be non-negative and less thansrc.lengthdestPos- The offset in the IBuffer where you want to copy data to. Must be non-negative and less thangetBufferSize()length- The number of floats to be read fromsrc; must be non-negative and no larger thansrc.length - srcPos- Throws:
java.lang.NullPointerException- ifsrcis null.java.lang.IndexOutOfBoundsException- if the preconditions on the arguments mentioned above are not honored.java.nio.BufferOverflowException- if there is insufficient space in thisIBuffer.
-
get
public void get(int srcPos, float[] dest, int destPos, int length)
Absolute bulk get method.This method transfers floats from this
IBufferinto the givendestarray. If there are fewer floats in theIBufferstarting at positionsrcPosthan are required to satisfy the request, then no floats are transferred and a BufferUnderflowException is thrown.This method is equivalent to calling
getByteBuffer(int, int)yourself and reading the bytes out, but is more efficient in theJNIMemoryManager.MemoryModel.NATIVE_BUFFERSmemory model.- Parameters:
srcPos- The offset in the IBuffer where you want to start copying data from. Must be non-negative and less thangetBufferSize()dest- The array to which floats are to be written. Must be non null.destPos- The offset withindestof the first float to be written; must be non-negative and less thandest.lengthlength- The number of floats to be copied intodest; must be non-negative and no larger thandest.length - destPos- Throws:
java.lang.NullPointerException- ifdestis null.java.lang.IndexOutOfBoundsException- if the preconditions on the arguments mentioned above are not honored.java.nio.BufferOverflowException- if there is insufficient data in thisIBufferto satisfy the request.
-
put
public void put(char[] src, int srcPos, int destPos, int length)
Absolute bulk put method.This method transfers chars into this buffer from the given source array. If there are more chars to be copied from the array than there is space remaining at the specified destination offset, then no chars are transferred and a java.nio.BufferOverflowException is thrown.
This method is equivalent to calling
getByteBuffer(int, int)yourself and copying the bytes over, but is more efficient in theJNIMemoryManager.MemoryModel.NATIVE_BUFFERSmemory model.- Parameters:
src- The array from which chars are to be read. Must be non null.srcPos- The offset withinsrcof the first char to be read; must be non-negative and less thansrc.lengthdestPos- The offset in the IBuffer where you want to copy data to. Must be non-negative and less thangetBufferSize()length- The number of chars to be read fromsrc; must be non-negative and no larger thansrc.length - srcPos- Throws:
java.lang.NullPointerException- ifsrcis null.java.lang.IndexOutOfBoundsException- if the preconditions on the arguments mentioned above are not honored.java.nio.BufferOverflowException- if there is insufficient space in thisIBuffer.
-
get
public void get(int srcPos, char[] dest, int destPos, int length)
Absolute bulk get method.This method transfers chars from this
IBufferinto the givendestarray. If there are fewer chars in theIBufferstarting at positionsrcPosthan are required to satisfy the request, then no chars are transferred and a BufferUnderflowException is thrown.This method is equivalent to calling
getByteBuffer(int, int)yourself and reading the bytes out, but is more efficient in theJNIMemoryManager.MemoryModel.NATIVE_BUFFERSmemory model.- Parameters:
srcPos- The offset in the IBuffer where you want to start copying data from. Must be non-negative and less thangetBufferSize()dest- The array to which chars are to be written. Must be non null.destPos- The offset withindestof the first char to be written; must be non-negative and less thandest.lengthlength- The number of chars to be copied intodest; must be non-negative and no larger thandest.length - destPos- Throws:
java.lang.NullPointerException- ifdestis null.java.lang.IndexOutOfBoundsException- if the preconditions on the arguments mentioned above are not honored.java.nio.BufferOverflowException- if there is insufficient data in thisIBufferto satisfy the request.
-
getByteBuffer
public java.nio.ByteBuffer getByteBuffer(int offset, int length)
Returns up to length bytes, starting at offset in the underlying buffer we're managing.The buffer position, mark are initialized to zero and limit is set to the maximum capacity of this buffer. For some IBuffer contents, the actual usable data in this buffer will be less that the limit.
If you want to control exactly when the underlying memory beyind the returned
ByteBufferis released, usegetByteBuffer(int, int, java.util.concurrent.atomic.AtomicReference).
-
getByteBuffer
public java.nio.ByteBuffer getByteBuffer(int offset, int length, java.util.concurrent.atomic.AtomicReference<JNIReference> referenceReturn)
Returns up to length bytes, starting at offset in the underlying buffer we're managing, and also passed back aJNIReferencethat can optionally be used by the caller to free the underlying native memory.The buffer position, mark are initialized to zero and limit is set to the maximum capacity of this buffer. For some IBuffer contents, the actual usable data in this buffer will be less that the limit.
If you use this method you are accessing the direct native memory associated with this buffer. That means changes you make to this buffer are immediately reflected in the underlying memory.
Once you call this method, the underlying native memory allocated will not be released until all references to the returned ByteBuffer are no longer reachable and at least one call to
JNIMemoryManager.gc()has been performed. You can force the memory to be collect earlier by using theJNIReferencevalue returned when you call this method.The
JNIMemoryManager.gc()is called whenever core tries to allocate new memory for any AVPKit interface, so normally you don't need to care about this. If for some reason no other AVPKit object is ever allocated, every AVPKit object has a finalizer as well that will do the right thing.You can also start up a separate thread to do continuously do Ferry garabage collections by calling
JNIMemoryManager.startCollectionThread(). This thread will only wake up when it has work to do, so the overhead is very low. We don't turn it on by default since in 99.999% of cases you don't need to worry about it.- Parameters:
offset- The offset (in bytes) into the buffer managed by this IBufferlength- The requested length (in bytes) you want to access. The buffer returned may actually be longer than length.referenceReturn- If non null, on exit callingAtomicReference.get()on this value will return aJNIReferenceyou can use for explicitly de-allocating the underlying native store of theByteBuffer. CallJNIReference.delete()to do that. Warning: if you do callJNIReference.delete()on the value returned in this parameter, then the returned byte buffer will be immediately invalid.- Returns:
- A java.nio.ByteBuffer that directly accesses the native memory this IBuffer manages, or null if error.
-
toString
public java.lang.String toString()
Prints the same asObject.toString()but appends the maximum number of bytes that will fit in thisIBuffer, the type of theIBuffer, and the maximum size in units ofIBuffer.Typethat will fit in theIBuffer.- Overrides:
toStringin classjava.lang.Object
-
getBufferSize
public int getBufferSize()
Get the current maximum number of bytes that can
be safely placed in this buffer.
- Returns:
- Maximum number of bytes this buffer can manage.
-
make
public static IBuffer make(RefCounted requestor, int bufferSize)
Allocate a new buffer of at least bufferSize.
- Parameters:
requestor- An optional value telling the IBuffer class what object requested it. This is used for debugging memory leaks; it's a marker for the FERRY object (e.g. IPacket) that actually requested the buffer. If you're not an FERRY object, pass in null here.bufferSize- The minimum buffer size you're requesting in bytes; a buffer with a larger size may be returned.
- Returns:
- A new buffer, or null on error.
-
getType
public IBuffer.Type getType()
Get the type this buffer was created as.
A type is really just a hint. Like
{java.nio.ByteBuffer objects},
{IBuffer} objects can be cast to and from any type.
- Returns:
- the type
-
setType
public void setType(IBuffer.Type type)
Reset the buffer type to a new type.
This method does not do any data conversion, it
just changes the reported type (so changing from
{Type#IBUFFER_UINT8} to {Type#IBUFFER_SINT16}
is really just a "cast" operation).
- Parameters:
type- the type to set to.
-
getTypeSize
public static int getTypeSize(IBuffer.Type type)
Returns the size, in bytes, of elements of given Type.
- Returns:
- the size in bytes.
-
getSize
public int getSize()
Returns the size, in units of {#getType()} of
this buffer.
- Returns:
- number of items of type {#getType()} that
will fit in this buffer.
-
make
public static IBuffer make(RefCounted requestor, IBuffer.Type type, int numElements, boolean zero)
Allocate a new buffer of at least bufferSize.
- Parameters:
requestor- An optional value telling the IBuffer class what object requested it. This is used for debugging memory leaks; it's a marker for the FERRY object (e.g. IPacket) that actually requested the buffer. If you're not an FERRY object, pass in null here.type- The type of buffer.numElements- The minimum number of elements of the specified
type you will put in this buffer.zero- If true, we will guarantee the buffer contains
only zeros. If false, we will not (it is
faster to not, but then the buffer will have
garbage-data in it).
- Returns:
- A new buffer, or null on error.
-
java_getByteBuffer
public java.nio.ByteBuffer java_getByteBuffer(int offset, int length)
-
getByteArray
public byte[] getByteArray(int offset, int length)
Returns up to length bytes, starting at offset in the
underlying buffer we're managing.
This method COPIES the data into the byte array being
returned..
If you don't NEED the direct access that getByteBuffer
offers (and most programs can in fact take the performance
hit of the copy), we recommend you use this method.
It's much harder to accidentally leave native memory lying
around waiting for cleanup then.
- Parameters:
offset- The offset (in bytes) into the buffer managed by this IBufferlength- The requested length (in bytes) you want to access. The buffer returned may
actually be longer than length.
- Returns:
- A copy of the data that is in this IBuffer, or null
if error.
-
make
public static IBuffer make(RefCounted requestor, byte[] buffer, int offset, int length)
Allocate a new IBuffer, and copy the data in buffer into
the new IBuffer object.
- Parameters:
requestor- An optional value telling the IBuffer class
what object requested it. This is used for debugging memory leaks;
it's a marker for the FERRY object (e.g. IPacket) that actually
requested the buffer. If you're not an FERRY object, pass in null here.buffer- A java byte buffer for the data containing the
data you want to copy.offset- The starting offset in buffer where you want
to start copying.length- The total number of bytes you want to copy from buffer.
- Returns:
- a new IBuffer object with a copy of the data in buffer,
or null on failure.
-
make
public static IBuffer make(RefCounted requestor, java.nio.ByteBuffer directByteBuffer, int offset, int length)
Create a new IBuffer object that uses the direct byte buffer
passed in by reference (i.e. it directly uses the bytes in
the direct byte buffer).
- Parameters:
requestor- An optional value telling the IBuffer class
what object requested it. This is used for debugging memory leaks;
it's a marker for the FERRY object (e.g. IPacket) that actually
requested the buffer. If you're not an FERRY object, pass in null here.directByteBuffer- A direct {java.nio.ByteBuffer} object
you want to use for your memory. This must be a direct object --
non direct objects will result in an JVM-dependent exception
being thrown.offset- The starting offset in directByteBuffer where you want
to start copying.length- The total number of bytes you want to copy from
directByteBuffer.
- Returns:
- a new IBuffer object that is using directByteBuffer
behind the scenes, or null on failure.
-
-