Class Pointer
- Direct Known Subclasses:
AbstractMemoryIO
Pointerinstances will represent direct
memory (that is, a fixed address in the process address space, directly accessible by native code), however,
it is possible to wrap a java byte array in a Pointerinstance to pass to a native function
as a memory address. See isDirect() for more information.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal longaddress()Gets the native address of this memory object (optional operation).abstract Objectarray()Returns the array that backs this pointer.abstract intReturns the length of this pointer's backing array that is used by this pointer.abstract intReturns the offset within this pointer's backing array of the first element.abstract voidcheckBounds(long offset, long length) Checks that the memory region is within the bounds of this memory objectabstract voidget(long offset, byte[] dst, int idx, int len) Bulk get method for multiplebytevalues.abstract voidget(long offset, double[] dst, int idx, int len) Bulk get method for multipledoublevalues.abstract voidget(long offset, float[] dst, int idx, int len) Bulk get method for multiplefloatvalues.abstract voidget(long offset, int[] dst, int idx, int len) Bulk get method for multipleintvalues.abstract voidget(long offset, long[] dst, int idx, int len) Bulk get method for multiplelongvalues.abstract voidget(long offset, short[] dst, int idx, int len) Bulk get method for multipleshortvalues.voidBulk get method for multiplePointervalues.abstract longgetAddress(long offset) Reads a native memory address value at the given offset.abstract bytegetByte(long offset) Reads anbyte(8 bit) value at the given offset.abstract doublegetDouble(long offset) Reads adouble(64 bit) value at the given offset.abstract floatgetFloat(long offset) Reads afloat(32 bit) value at the given offset.abstract intgetInt(long offset) Reads anint(32 bit) value at the given offset.abstract longReads an integer value of the given type, at the given offset.abstract longgetLong(long offset) Reads along(64 bit) value at the given offset.abstract longgetLongLong(long offset) Reads along(64 bit) value at the given offset.abstract longgetNativeLong(long offset) Reads a nativelongvalue at the given offset.Pointer[]getNullTerminatedPointerArray(long offset) String[]getNullTerminatedStringArray(long offset) abstract PointergetPointer(long offset) Reads anPointervalue at the given offset.abstract PointergetPointer(long offset, long size) Reads anPointervalue at the given offset.final RuntimeGets theRuntimethisPointerinstance belongs to.abstract shortgetShort(long offset) Reads ashort(16 bit) value at the given offset.abstract StringgetString(long offset) Reads anStringvalue at the given offset.abstract StringReads aStringvalue at the given offset, using a specificCharsetabstract booleanhasArray()Indicates whether thisPointerinstance is backed by an array.abstract intindexOf(long offset, byte value) Returns the location of a byte value within the memory area represented by thisPointer.abstract intindexOf(long offset, byte value, int maxlen) Returns the location of a byte value within the memory area represented by thisPointer.final booleanisDirect()Indicates whether or not this memory object represents a native memory address.static PointernewIntPointer(Runtime runtime, long address) Wraps an integer value in an opaquePointerinstance.abstract voidput(long offset, byte[] src, int idx, int len) Bulk put method for multiplebytevalues.abstract voidput(long offset, double[] src, int idx, int len) Bulk put method for multipledoublevalues.abstract voidput(long offset, float[] src, int idx, int len) Bulk put method for multiplefloatvalues.abstract voidput(long offset, int[] src, int idx, int len) Bulk put method for multipleintvalues.abstract voidput(long offset, long[] src, int idx, int len) Bulk put method for multiplelongvalues.abstract voidput(long offset, short[] src, int idx, int len) Bulk put method for multipleshortvalues.voidBulk put method for multiplePointervalues.abstract voidputAddress(long offset, long value) Writes a native memory address value at the given offset.abstract voidputAddress(long offset, Address value) Writes a native memory address value at the given offset.abstract voidputByte(long offset, byte value) Writes abyte(8 bit) value at the given offset.abstract voidputDouble(long offset, double value) Writes adouble(64 bit, double precision) value at the given offset.abstract voidputFloat(long offset, float value) Writes afloat(32 bit, single precision) value at the given offset.abstract voidputInt(long offset, int value) Writes anint(32 bit) value at the given offset.abstract voidWrites an integer of a specific type, at the given offset.abstract voidputLong(long offset, long value) Writes anative longvalue at the given offset.abstract voidputLongLong(long offset, long value) Writes along(64 bit) value at the given offset.abstract voidputNativeLong(long offset, long value) Writes a nativelongvalue at the given offset.abstract voidputPointer(long offset, Pointer value) Writes aPointervalue at the given offset.abstract voidputShort(long offset, short value) Writes ashort(16 bit) value at the given offset.abstract voidWrites aStringvalue at the given offset, using a specificCharsetabstract voidsetMemory(long offset, long size, byte value) Sets the value of each byte in the memory area represented by thisPointer.abstract longsize()Gets the size of this memory object in bytes (optional operation).abstract Pointerslice(long offset) Creates a newPointerrepresenting a sub-region of the memory referred to by thisPointer.abstract Pointerslice(long offset, long size) Creates a newPointerrepresenting a sub-region of the memory referred to by thisPointer.toString()abstract voidtransferFrom(long offset, Pointer src, long srcOffset, long count) Bulk data transfer from one memory location to another.abstract voidtransferTo(long offset, Pointer dst, long dstOffset, long count) Bulk data transfer from one memory location to another.static PointerWraps a native address in aPointerinstance.static PointerWraps a native address in aPointerinstance.static Pointerwrap(Runtime runtime, ByteBuffer buffer) Wraps an existing ByteBuffer in aPointerimplementation so it can be used as a parameter to native functions.
-
Constructor Details
-
Pointer
-
-
Method Details
-
wrap
Wraps a native address in aPointerinstance.- Parameters:
runtime- theRuntimeof the pointer.address- theaddressto wrap in a Pointer instance.- Returns:
- a
Pointerinstance.
-
wrap
Wraps a native address in aPointerinstance.- Parameters:
runtime- theRuntimeof the pointer.address- theaddressto wrap in a Pointer instance.size- the size of the native memory region.- Returns:
- a
Pointerinstance.
-
wrap
Wraps an existing ByteBuffer in aPointerimplementation so it can be used as a parameter to native functions.Wrapping a ByteBuffer is only neccessary if the native function parameter was declared as a
Pointer. The if the method will always be used withByteBufferparameters, then the parameter type can just be declared asByteBufferand the conversion will be performed automatically.- Parameters:
runtime- theRuntimethe wrappedByteBufferwill be used with.buffer- theByteBufferto wrap.- Returns:
- a
Pointerinstance that will proxy all accesses to the ByteBuffer contents.
-
newIntPointer
Wraps an integer value in an opaquePointerinstance. This is a Pointer instance that throws errors when any of the memory access methods are used, but can be otherwise used interchangeably with a real Pointer.- Parameters:
runtime- theRuntimeof the pointer.address- theaddressto wrap in a Pointer instance.- Returns:
- a
Pointerinstance.
-
isDirect
public final boolean isDirect()Indicates whether or not this memory object represents a native memory address.Memory objects can be either direct (representing native memory), or non-direct (representing java heap memory).
Non-direct memory objects can still be passed to native functions as pointer (void *, char *, etc) parameters, but the java memory will first be copied to a temporary native memory area. The temporary memory area will then be used as the parameter value for the call. If needed, the java memory will be automatically reloaded from the temporary native memory after the native function returns.
Note: the transient nature of the temporary memory allocated for non-direct memory means native functions which store the address value passed to them will fail in unpredictable ways when using non-direct memory. You will need to explicitly allocate direct memory to use those types of functions.
- Returns:
- true if, and only if, this memory object represents a native address.
-
address
public final long address()Gets the native address of this memory object (optional operation).- Returns:
- the native address of this memory object. If this object is not a native memory address, an address of zero is returned.
-
getRuntime
Gets theRuntimethisPointerinstance belongs to.- Returns:
- the
Runtimeinstance of thisPointer.
-
toString
-
size
public abstract long size()Gets the size of this memory object in bytes (optional operation).- Returns:
- the size of the memory area this
Pointerpoints to. If the size is unknown,Long.MAX_VALUEis returned}.
-
hasArray
public abstract boolean hasArray()Indicates whether thisPointerinstance is backed by an array.- Returns:
- true if, and only if, this memory object is backed by an array
-
array
Returns the array that backs this pointer.- Returns:
- The array that backs this pointer.
- Throws:
UnsupportedOperationException- if this pointer does not have a backing array.
-
arrayOffset
public abstract int arrayOffset()Returns the offset within this pointer's backing array of the first element.- Returns:
- The offset of the first element on the backing array
- Throws:
UnsupportedOperationException- if this pointer does not have a backing array
-
arrayLength
public abstract int arrayLength()Returns the length of this pointer's backing array that is used by this pointer.- Returns:
- The length of the backing array used
- Throws:
UnsupportedOperationException- if this pointer does not have a backing array
-
getByte
public abstract byte getByte(long offset) Reads anbyte(8 bit) value at the given offset.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be read.- Returns:
- the
bytevalue at the offset.
-
getShort
public abstract short getShort(long offset) Reads ashort(16 bit) value at the given offset.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be read.- Returns:
- the
shortvalue at the offset.
-
getInt
public abstract int getInt(long offset) Reads anint(32 bit) value at the given offset.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be read.- Returns:
- the
intvalue contained in the memory at the offset.
-
getLong
public abstract long getLong(long offset) Reads along(64 bit) value at the given offset.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be read.- Returns:
- the
longvalue at the offset.
-
getLongLong
public abstract long getLongLong(long offset) Reads along(64 bit) value at the given offset.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be read.- Returns:
- the
longvalue at the offset.
-
getFloat
public abstract float getFloat(long offset) Reads afloat(32 bit) value at the given offset.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be read.- Returns:
- the
floatvalue at the offset.
-
getDouble
public abstract double getDouble(long offset) Reads adouble(64 bit) value at the given offset.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be read.- Returns:
- the
doublevalue at the offset.
-
getNativeLong
public abstract long getNativeLong(long offset) Reads a nativelongvalue at the given offset.A native
longcan be either 32 or 64 bits in size, depending on the cpu architecture, and the C ABI in use.For windows, a long is always 32 bits (4 bytes) in size, but on unix systems, a long on a 32bit system is 32 bits, and on a 64bit system, is 64 bits.
- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be read.- Returns:
- the native
longvalue at the offset. - See Also:
-
getInt
Reads an integer value of the given type, at the given offset.- Parameters:
type- Type of integer to read.offset- The offset from the start of the memory thisPointerrepresents at which the value will be read.- Returns:
- the
intvalue contained in the memory at the offset.
-
putByte
public abstract void putByte(long offset, byte value) Writes abyte(8 bit) value at the given offset.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be written.value- thebytevalue to be written.
-
putShort
public abstract void putShort(long offset, short value) Writes ashort(16 bit) value at the given offset.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be written.value- theshortvalue to be written.
-
putInt
public abstract void putInt(long offset, int value) Writes anint(32 bit) value at the given offset.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be written.value- theintvalue to be written.
-
putLong
public abstract void putLong(long offset, long value) Writes anative longvalue at the given offset.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be written.value- thelongvalue to be written.
-
putLongLong
public abstract void putLongLong(long offset, long value) Writes along(64 bit) value at the given offset.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be written.value- thelongvalue to be written.
-
putFloat
public abstract void putFloat(long offset, float value) Writes afloat(32 bit, single precision) value at the given offset.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be written.value- thefloatvalue to be written.
-
putDouble
public abstract void putDouble(long offset, double value) Writes adouble(64 bit, double precision) value at the given offset.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be written.value- thedoublevalue to be written.
-
putNativeLong
public abstract void putNativeLong(long offset, long value) Writes a nativelongvalue at the given offset.A native
longcan be either 32 or 64 bits in size, depending on the cpu architecture, and the C ABI in use.For windows, a long is always 32 bits (4 bytes) in size, but on unix systems, a long on a 32bit system is 32 bits, and on a 64bit system, is 64 bits.
- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be written.value- the nativelongvalue to be written.
-
putInt
Writes an integer of a specific type, at the given offset.- Parameters:
type- The integer type.offset- The offset from the start of the memory thisPointerrepresents at which the value will be written.value- theintvalue to be written.
-
getAddress
public abstract long getAddress(long offset) Reads a native memory address value at the given offset.A native address can be either 32 or 64 bits in size, depending on the cpu architecture.
- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be read.- Returns:
- the native address value contained in the memory at the offset
- See Also:
-
putAddress
public abstract void putAddress(long offset, long value) Writes a native memory address value at the given offset.A native address can be either 32 or 64 bits in size, depending on the cpu architecture.
- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be written.value- The native address value to be written.- See Also:
-
putAddress
Writes a native memory address value at the given offset.A native address can be either 32 or 64 bits in size, depending on the cpu architecture.
- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be written.value- The native address value to be written.- See Also:
-
get
public abstract void get(long offset, byte[] dst, int idx, int len) Bulk get method for multiplebytevalues. This method reads multiplebytevalues from consecutive addresses, beginning at the given offset, and stores them in an array.- Parameters:
offset- the offset from the start of the memory thisPointerrepresents at which the first value will be read.dst- the array into which values are to be stored.idx- the start index in thedstarray to begin storing the values.len- the number of values to be read.
-
put
public abstract void put(long offset, byte[] src, int idx, int len) Bulk put method for multiplebytevalues. This method writes multiplebytevalues to consecutive addresses, beginning at the given offset, from an array.- Parameters:
offset- the offset from the start of the memory thisPointerrepresents at which the first value will be written.src- the array to get values from.idx- the start index in thedstarray to begin reading values.len- the number of values to be written.
-
get
public abstract void get(long offset, short[] dst, int idx, int len) Bulk get method for multipleshortvalues. This method reads multipleshortvalues from consecutive addresses, beginning at the given offset, and stores them in an array.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the first value will be read.dst- The array into which values are to be stored.idx- the start index in thedstarray to begin storing the values.len- the number of values to be read.
-
put
public abstract void put(long offset, short[] src, int idx, int len) Bulk put method for multipleshortvalues. This method writes multipleshortvalues to consecutive addresses, beginning at the given offset, from an array.- Parameters:
offset- the offset from the start of the memory thisPointerrepresents at which the first value will be written.src- the array to get values from.idx- the start index in thedstarray to begin reading values.len- the number of values to be written.
-
get
public abstract void get(long offset, int[] dst, int idx, int len) Bulk get method for multipleintvalues. This method reads multipleintvalues from consecutive addresses, beginning at the given offset, and stores them in an array.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the first value will be read.dst- The array into which values are to be stored.idx- the start index in thedstarray to begin storing the values.len- the number of values to be read.
-
put
public abstract void put(long offset, int[] src, int idx, int len) Bulk put method for multipleintvalues. This method writes multipleintvalues to consecutive addresses, beginning at the given offset, from an array.- Parameters:
offset- the offset from the start of the memory thisPointerrepresents at which the first value will be written.src- the array to get values from.idx- the start index in thedstarray to begin reading values.len- the number of values to be written.
-
get
public abstract void get(long offset, long[] dst, int idx, int len) Bulk get method for multiplelongvalues. This method reads multiplelongvalues from consecutive addresses, beginning at the given offset, and stores them in an array.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the first value will be read.dst- The array into which values are to be stored.idx- the start index in thedstarray to begin storing the values.len- the number of values to be read.
-
put
public abstract void put(long offset, long[] src, int idx, int len) Bulk put method for multiplelongvalues. This method writes multiplelongvalues to consecutive addresses, beginning at the given offset, from an array.- Parameters:
offset- the offset from the start of the memory thisPointerrepresents at which the first value will be written.src- the array to get values from.idx- the start index in thedstarray to begin reading values.len- the number of values to be written.
-
get
public abstract void get(long offset, float[] dst, int idx, int len) Bulk get method for multiplefloatvalues. This method reads multiplefloatvalues from consecutive addresses, beginning at the given offset, and stores them in an array.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the first value will be read.dst- The array into which values are to be stored.idx- the start index in thedstarray to begin storing the values.len- the number of values to be read.
-
put
public abstract void put(long offset, float[] src, int idx, int len) Bulk put method for multiplefloatvalues. This method writes multiplefloatvalues to consecutive addresses, beginning at the given offset, from an array.- Parameters:
offset- the offset from the start of the memory thisPointerrepresents at which the first value will be written.src- the array to get values from.idx- the start index in thedstarray to begin reading values.len- the number of values to be written.
-
get
public abstract void get(long offset, double[] dst, int idx, int len) Bulk get method for multipledoublevalues. This method reads multipledoublevalues from consecutive addresses, beginning at the given offset, and stores them in an array.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the first value will be read.dst- The array into which values are to be stored.idx- the start index in thedstarray to begin storing the values.len- the number of values to be read.
-
put
public abstract void put(long offset, double[] src, int idx, int len) Bulk put method for multipledoublevalues. This method writes multipledoublevalues to consecutive addresses, beginning at the given offset, from an array.- Parameters:
offset- the offset from the start of the memory thisPointerrepresents at which the first value will be written.src- the array to get values from.idx- the start index in thedstarray to begin reading values.len- the number of values to be written.
-
getPointer
Reads anPointervalue at the given offset.- Parameters:
offset- the offset from the start of the memory thisPointerrepresents at which the value will be read.- Returns:
- the
Pointervalue read from memory.
-
getPointer
Reads anPointervalue at the given offset.- Parameters:
offset- the offset from the start of the memory thisPointerrepresents at which the value will be read.size- the maximum size of the memory location the returnedPointerrepresents.- Returns:
- the
Pointervalue read from memory.
-
putPointer
Writes aPointervalue at the given offset.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the value will be written.value- thePointervalue to be written to memory.
-
getString
Reads anStringvalue at the given offset.- Parameters:
offset- the offset from the start of the memory thisPointerrepresents at which the value will be read.- Returns:
- the
Stringvalue read from memory.
-
getString
Reads aStringvalue at the given offset, using a specificCharset- Parameters:
offset- the offset from the start of the memory thisPointerrepresents at which the value will be read.maxLength- the maximum size of memory to search for a NUL byte.cs- theCharsetto use to decode the string.- Returns:
- the
Stringvalue read from memory.
-
putString
Writes aStringvalue at the given offset, using a specificCharset- Parameters:
offset- the offset from the start of the memory thisPointerrepresents at which the value will be written.string- the string to be written.maxLength- the maximum size of memory to use to store the string.cs- theCharsetto use to decode the string.
-
slice
Creates a newPointerrepresenting a sub-region of the memory referred to by thisPointer.- Parameters:
offset- the offset from the start of the memory thisPointerrepresents at which the newPointerwill start.- Returns:
- a
Pointerinstance representing the new sub-region.
-
slice
Creates a newPointerrepresenting a sub-region of the memory referred to by thisPointer.- Parameters:
offset- the offset from the start of the memory thisPointerrepresents at which the newPointerwill start.size- the maximum size of the memory sub-region.- Returns:
- a
Pointerinstance representing the new sub-region.
-
transferTo
Bulk data transfer from one memory location to another.- Parameters:
offset- the offset from the start of the memory location thisPointerrepresents to begin copying from.dst- the destination memory location to transfer data to.dstOffset- the offset from the start of the memory location the destinationPointerrepresents to begin copying to.count- the number of bytes to transfer.
-
transferFrom
Bulk data transfer from one memory location to another.- Parameters:
offset- the offset from the start of the memory location thisPointerrepresents to begin copying to.src- the destination memory location to transfer data from.srcOffset- the offset from the start of the memory location the destinationPointerrepresents to begin copying from.count- the number of bytes to transfer.
-
checkBounds
public abstract void checkBounds(long offset, long length) Checks that the memory region is within the bounds of this memory object- Parameters:
offset- the starting point within this memory region.length- the length of the memory region in bytes- Throws:
IndexOutOfBoundsException- if the memory region is not within the bounds.
-
setMemory
public abstract void setMemory(long offset, long size, byte value) Sets the value of each byte in the memory area represented by thisPointer. to a specified value.- Parameters:
offset- the offset from the start of the memory location thisPointerrepresents to begin writing to.size- the number of bytes to set to the value.value- the value to set each byte to.
-
indexOf
public abstract int indexOf(long offset, byte value) Returns the location of a byte value within the memory area represented by thisPointer.- Parameters:
offset- the offset from the start of the memory location thisPointerrepresents to begin searching.value- thebytevalue to locate.- Returns:
- the offset from the start of the search area (i.e. relative to the offset parameter), or -1 if not found.
-
indexOf
public abstract int indexOf(long offset, byte value, int maxlen) Returns the location of a byte value within the memory area represented by thisPointer.- Parameters:
offset- the offset from the start of the memory location thisPointerrepresents to begin searching.value- thebytevalue to locate.maxlen- the maximum number of bytes to search for the desired value.- Returns:
- the offset from the start of the search area (i.e. relative to the offset parameter), or -1 if not found.
-
get
Bulk get method for multiplePointervalues. This method reads multiplePointervalues from consecutive addresses, beginning at the given offset, and stores them in an array.- Parameters:
offset- The offset from the start of the memory thisPointerrepresents at which the first value will be read.dst- The array into which values are to be stored.idx- the start index in thedstarray to begin storing the values.len- the number of values to be read.
-
put
Bulk put method for multiplePointervalues. This method writes multiplePointervalues to consecutive addresses, beginning at the given offset, from an array.- Parameters:
offset- the offset from the start of the memory thisPointerrepresents at which the first value will be written.src- the array to get values from.idx- the start index in thesrcarray to begin reading values.len- the number of values to be written.
-
getNullTerminatedStringArray
-
getNullTerminatedPointerArray
-