jnr.ffi
Class Pointer

java.lang.Object
  extended by jnr.ffi.Pointer
Direct Known Subclasses:
AbstractMemoryIO

public abstract class Pointer
extends java.lang.Object


Constructor Summary
Pointer()
           
 
Method Summary
abstract  long address()
          Gets the native address of this memory object (optional operation).
abstract  java.lang.Object array()
           
abstract  int arrayLength()
           
abstract  int arrayOffset()
           
abstract  void checkBounds(long offset, long length)
          Checks that the memory region is within the bounds of this memory object
abstract  void get(long offset, byte[] dst, int idx, int len)
          Bulk get method for multiple byte values.
abstract  void get(long offset, double[] dst, int idx, int len)
          Bulk get method for multiple double values.
abstract  void get(long offset, float[] dst, int idx, int len)
          Bulk get method for multiple float values.
abstract  void get(long offset, int[] dst, int idx, int len)
          Bulk get method for multiple int values.
abstract  void get(long offset, long[] dst, int idx, int len)
          Bulk get method for multiple long values.
abstract  void get(long offset, short[] dst, int idx, int len)
          Bulk get method for multiple short values.
abstract  long getAddress(long offset)
           
abstract  byte getByte(long offset)
          Reads an byte (8 bit) value at the given offset.
abstract  double getDouble(long offset)
          Reads a double (64 bit) value at the given offset.
abstract  float getFloat(long offset)
          Reads a float (32 bit) value at the given offset.
abstract  int getInt(long offset)
          Reads an int (32 bit) value at the given offset.
abstract  long getInt(Type type, long offset)
          Reads an integer value of the given type, at the given offset.
abstract  long getLong(long offset)
          Reads a long (64 bit) value at the given offset.
abstract  long getLongLong(long offset)
          Reads a long (64 bit) value at the given offset.
abstract  long getNativeLong(long offset)
          Reads a native long value at the given offset.
abstract  Pointer getPointer(long offset)
          Reads an Pointer value at the given offset.
abstract  Pointer getPointer(long offset, long size)
          Reads an Pointer value at the given offset.
abstract  Runtime getRuntime()
          Gets the Runtime this Pointer instance belongs to.
abstract  short getShort(long offset)
          Reads a short (16 bit) value at the given offset.
abstract  java.lang.String getString(long offset)
          Reads an String value at the given offset.
abstract  java.lang.String getString(long offset, int maxLength, java.nio.charset.Charset cs)
          Reads a String value at the given offset, using a specific Charset
abstract  boolean hasArray()
           
abstract  int indexOf(long offset, byte value)
          Returns the location of a byte value within the memory area represented by this Pointer.
abstract  int indexOf(long offset, byte value, int maxlen)
          Returns the location of a byte value within the memory area represented by this Pointer.
abstract  boolean isDirect()
          Indicates whether or not this memory object represents a native memory address.
static Pointer newIntPointer(Runtime runtime, long address)
          Wraps an integer value in an opaque Pointer instance.
abstract  void put(long offset, byte[] dst, int idx, int len)
          Bulk put method for multiple byte values.
abstract  void put(long offset, double[] dst, int idx, int len)
          Bulk put method for multiple double values.
abstract  void put(long offset, float[] dst, int idx, int len)
          Bulk put method for multiple float values.
abstract  void put(long offset, int[] dst, int idx, int len)
          Bulk put method for multiple int values.
abstract  void put(long offset, long[] dst, int idx, int len)
          Bulk put method for multiple long values.
abstract  void put(long offset, short[] dst, int idx, int len)
          Bulk put method for multiple short values.
abstract  void putAddress(long offset, Address value)
           
abstract  void putAddress(long offset, long value)
           
abstract  void putByte(long offset, byte value)
          Writes a byte (8 bit) value at the given offset.
abstract  void putDouble(long offset, double value)
          Writes a double (64 bit, double precision) value at the given offset.
abstract  void putFloat(long offset, float value)
          Writes a float (32 bit, single precision) value at the given offset.
abstract  void putInt(long offset, int value)
          Writes an int (32 bit) value at the given offset.
abstract  void putInt(Type type, long offset, long value)
          Writes an integer of a specific type, at the given offset.
abstract  void putLong(long offset, long value)
          Writes a native long value at the given offset.
abstract  void putLongLong(long offset, long value)
          Writes a long (64 bit) value at the given offset.
abstract  void putNativeLong(long offset, long value)
          Writes a native long value at the given offset.
abstract  void putPointer(long offset, Pointer value)
          Writes a Pointer value at the given offset.
abstract  void putShort(long offset, short value)
          Writes a short (16 bit) value at the given offset.
abstract  void putString(long offset, java.lang.String string, int maxLength, java.nio.charset.Charset cs)
          Writes a String value at the given offset, using a specific Charset
abstract  void setMemory(long offset, long size, byte value)
          Sets the value of each byte in the memory area represented by this Pointer.
abstract  long size()
          Gets the size of this memory object (optional operation).
abstract  Pointer slice(long offset)
          Creates a new Pointer representing a sub-region of the memory referred to by this Pointer.
abstract  Pointer slice(long offset, long size)
          Creates a new Pointer representing a sub-region of the memory referred to by this Pointer.
abstract  void transferFrom(long offset, Pointer src, long srcOffset, long count)
          Bulk data transfer from one memory location to another.
abstract  void transferTo(long offset, Pointer dst, long dstOffset, long count)
          Bulk data transfer from one memory location to another.
static Pointer wrap(Runtime runtime, java.nio.ByteBuffer buffer)
          Wraps an existing ByteBuffer in a Pointer implementation so it can be used as a parameter to native functions.
static Pointer wrap(Runtime runtime, long address)
          Wraps a native address in a Pointer instance.
static Pointer wrap(Runtime runtime, long address, long size)
          Wraps a native address in a Pointer instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Pointer

public Pointer()
Method Detail

wrap

public static Pointer wrap(Runtime runtime,
                           long address)
Wraps a native address in a Pointer instance.

Parameters:
runtime - the Runtime of the pointer.
address - the address to wrap in a Pointer instance.
Returns:
a Pointer instance.

wrap

public static Pointer wrap(Runtime runtime,
                           long address,
                           long size)
Wraps a native address in a Pointer instance.

Parameters:
runtime - the Runtime of the pointer.
address - the address to wrap in a Pointer instance.
size - the size of the native memory region.
Returns:
a Pointer instance.

wrap

public static final Pointer wrap(Runtime runtime,
                                 java.nio.ByteBuffer buffer)
Wraps an existing ByteBuffer in a Pointer implementation 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 with ByteBuffer parameters, then the parameter type can just be declared as ByteBuffer and the conversion will be performed automatically.

Parameters:
runtime - the Runtime the wrapped ByteBuffer will be used with.
buffer - the ByteBuffer to wrap.
Returns:
a Pointer instance that will proxy all accesses to the ByteBuffer contents.

newIntPointer

public static Pointer newIntPointer(Runtime runtime,
                                    long address)
Wraps an integer value in an opaque Pointer instance. 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 - the Runtime of the pointer.
address - the address to wrap in a Pointer instance.
Returns:
a Pointer instance.

isDirect

public abstract 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 abstract 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.

size

public abstract long size()
Gets the size of this memory object (optional operation).

Returns:
the size of the memory area this Pointer points to. If the size is unknown, Long.MAX_VALUE is returned}.

hasArray

public abstract boolean hasArray()

array

public abstract java.lang.Object array()

arrayOffset

public abstract int arrayOffset()

arrayLength

public abstract int arrayLength()

getRuntime

public abstract Runtime getRuntime()
Gets the Runtime this Pointer instance belongs to.

Returns:
the Runtime instance of this Pointer.

getByte

public abstract byte getByte(long offset)
Reads an byte (8 bit) value at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be read.
Returns:
the byte value at the offset.

getShort

public abstract short getShort(long offset)
Reads a short (16 bit) value at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be read.
Returns:
the short value at the offset.

getInt

public abstract int getInt(long offset)
Reads an int (32 bit) value at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be read.
Returns:
the int value contained in the memory at the offset.

getLong

public abstract long getLong(long offset)
Reads a long (64 bit) value at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be read.
Returns:
the long value at the offset.

getLongLong

public abstract long getLongLong(long offset)
Reads a long (64 bit) value at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be read.
Returns:
the long value at the offset.

getFloat

public abstract float getFloat(long offset)
Reads a float (32 bit) value at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be read.
Returns:
the float value at the offset.

getDouble

public abstract double getDouble(long offset)
Reads a double (64 bit) value at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be read.
Returns:
the double value at the offset.

getNativeLong

public abstract long getNativeLong(long offset)
Reads a native long value at the given offset.

A native long can 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 this Pointer represents at which the value will be read.
Returns:
the native long value at the offset.
See Also:
NativeLong

getInt

public abstract long getInt(Type type,
                            long offset)
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 this Pointer represents at which the value will be read.
Returns:
the int value contained in the memory at the offset.

putByte

public abstract void putByte(long offset,
                             byte value)
Writes a byte (8 bit) value at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be written.
value - the byte value to be written.

putShort

public abstract void putShort(long offset,
                              short value)
Writes a short (16 bit) value at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be written.
value - the short value to be written.

putInt

public abstract void putInt(long offset,
                            int value)
Writes an int (32 bit) value at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be written.
value - the int value to be written.

putLong

public abstract void putLong(long offset,
                             long value)
Writes a native long value at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be written.
value - the long value to be written.

putLongLong

public abstract void putLongLong(long offset,
                                 long value)
Writes a long (64 bit) value at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be written.
value - the long value to be written.

putFloat

public abstract void putFloat(long offset,
                              float value)
Writes a float (32 bit, single precision) value at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be written.
value - the float value to be written.

putDouble

public abstract void putDouble(long offset,
                               double value)
Writes a double (64 bit, double precision) value at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be written.
value - the double value to be written.

putNativeLong

public abstract void putNativeLong(long offset,
                                   long value)
Writes a native long value at the given offset.

A native long can 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 this Pointer represents at which the value will be written.
value - the native long value to be written.

putInt

public abstract void putInt(Type type,
                            long offset,
                            long value)
Writes an integer of a specific type, at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be written.
value - the int value to be written.

getAddress

public abstract long getAddress(long offset)

putAddress

public abstract void putAddress(long offset,
                                long value)

putAddress

public abstract void putAddress(long offset,
                                Address value)

get

public abstract void get(long offset,
                         byte[] dst,
                         int idx,
                         int len)
Bulk get method for multiple byte values. This method reads multiple byte values from consecutive addresses, beginning at the given offset, and stores them in an array.

Parameters:
offset - the offset from the start of the memory this Pointer represents at which the first value will be read.
dst - the array into which values are to be stored.
idx - the start index in the dst array to begin storing the values.
len - the number of values to be read.

put

public abstract void put(long offset,
                         byte[] dst,
                         int idx,
                         int len)
Bulk put method for multiple byte values. This method writes multiple byte values to consecutive addresses, beginning at the given offset, from an array.

Parameters:
offset - the offset from the start of the memory this Pointer represents at which the first value will be written.
dst - the array to get values from.
idx - the start index in the dst array 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 multiple short values. This method reads multiple short values from consecutive addresses, beginning at the given offset, and stores them in an array.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the first value will be read.
dst - The array into which values are to be stored.
idx - the start index in the dst array to begin storing the values.
len - the number of values to be read.

put

public abstract void put(long offset,
                         short[] dst,
                         int idx,
                         int len)
Bulk put method for multiple short values. This method writes multiple short values to consecutive addresses, beginning at the given offset, from an array.

Parameters:
offset - the offset from the start of the memory this Pointer represents at which the first value will be written.
dst - the array to get values from.
idx - the start index in the dst array 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 multiple int values. This method reads multiple int values from consecutive addresses, beginning at the given offset, and stores them in an array.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the first value will be read.
dst - The array into which values are to be stored.
idx - the start index in the dst array to begin storing the values.
len - the number of values to be read.

put

public abstract void put(long offset,
                         int[] dst,
                         int idx,
                         int len)
Bulk put method for multiple int values. This method writes multiple int values to consecutive addresses, beginning at the given offset, from an array.

Parameters:
offset - the offset from the start of the memory this Pointer represents at which the first value will be written.
dst - the array to get values from.
idx - the start index in the dst array 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 multiple long values. This method reads multiple long values from consecutive addresses, beginning at the given offset, and stores them in an array.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the first value will be read.
dst - The array into which values are to be stored.
idx - the start index in the dst array to begin storing the values.
len - the number of values to be read.

put

public abstract void put(long offset,
                         long[] dst,
                         int idx,
                         int len)
Bulk put method for multiple long values. This method writes multiple long values to consecutive addresses, beginning at the given offset, from an array.

Parameters:
offset - the offset from the start of the memory this Pointer represents at which the first value will be written.
dst - the array to get values from.
idx - the start index in the dst array 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 multiple float values. This method reads multiple float values from consecutive addresses, beginning at the given offset, and stores them in an array.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the first value will be read.
dst - The array into which values are to be stored.
idx - the start index in the dst array to begin storing the values.
len - the number of values to be read.

put

public abstract void put(long offset,
                         float[] dst,
                         int idx,
                         int len)
Bulk put method for multiple float values. This method writes multiple float values to consecutive addresses, beginning at the given offset, from an array.

Parameters:
offset - the offset from the start of the memory this Pointer represents at which the first value will be written.
dst - the array to get values from.
idx - the start index in the dst array 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 multiple double values. This method reads multiple double values from consecutive addresses, beginning at the given offset, and stores them in an array.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the first value will be read.
dst - The array into which values are to be stored.
idx - the start index in the dst array to begin storing the values.
len - the number of values to be read.

put

public abstract void put(long offset,
                         double[] dst,
                         int idx,
                         int len)
Bulk put method for multiple double values. This method writes multiple double values to consecutive addresses, beginning at the given offset, from an array.

Parameters:
offset - the offset from the start of the memory this Pointer represents at which the first value will be written.
dst - the array to get values from.
idx - the start index in the dst array to begin reading values.
len - the number of values to be written.

getPointer

public abstract Pointer getPointer(long offset)
Reads an Pointer value at the given offset.

Parameters:
offset - the offset from the start of the memory this Pointer represents at which the value will be read.
Returns:
the Pointer value read from memory.

getPointer

public abstract Pointer getPointer(long offset,
                                   long size)
Reads an Pointer value at the given offset.

Parameters:
offset - the offset from the start of the memory this Pointer represents at which the value will be read.
size - the maximum size of the memory location the returned Pointer represents.
Returns:
the Pointer value read from memory.

putPointer

public abstract void putPointer(long offset,
                                Pointer value)
Writes a Pointer value at the given offset.

Parameters:
offset - The offset from the start of the memory this Pointer represents at which the value will be written.
value - the Pointer value to be written to memory.

getString

public abstract java.lang.String getString(long offset)
Reads an String value at the given offset.

Parameters:
offset - the offset from the start of the memory this Pointer represents at which the value will be read.
Returns:
the String value read from memory.

getString

public abstract java.lang.String getString(long offset,
                                           int maxLength,
                                           java.nio.charset.Charset cs)
Reads a String value at the given offset, using a specific Charset

Parameters:
offset - the offset from the start of the memory this Pointer represents at which the value will be read.
maxLength - the maximum size of memory to search for a NUL byte.
cs - the Charset to use to decode the string.
Returns:
the String value read from memory.

putString

public abstract void putString(long offset,
                               java.lang.String string,
                               int maxLength,
                               java.nio.charset.Charset cs)
Writes a String value at the given offset, using a specific Charset

Parameters:
offset - the offset from the start of the memory this Pointer represents at which the value will be written.
maxLength - the maximum size of memory to use to store the string.
cs - the Charset to use to decode the string.

slice

public abstract Pointer slice(long offset)
Creates a new Pointer representing a sub-region of the memory referred to by this Pointer.

Parameters:
offset - the offset from the start of the memory this Pointer represents at which the new Pointer will start.
Returns:
a Pointer instance representing the new sub-region.

slice

public abstract Pointer slice(long offset,
                              long size)
Creates a new Pointer representing a sub-region of the memory referred to by this Pointer.

Parameters:
offset - the offset from the start of the memory this Pointer represents at which the new Pointer will start.
size - the maximum size of the memory sub-region.
Returns:
a Pointer instance representing the new sub-region.

transferTo

public abstract void transferTo(long offset,
                                Pointer dst,
                                long dstOffset,
                                long count)
Bulk data transfer from one memory location to another.

Parameters:
offset - the offset from the start of the memory location this Pointer represents to begin copying from.
dst - the destination memory location to transfer data to.
dstOffset - the offset from the start of the memory location the destination Pointer represents to begin copying to.
count - the number of bytes to transfer.

transferFrom

public abstract void transferFrom(long offset,
                                  Pointer src,
                                  long srcOffset,
                                  long count)
Bulk data transfer from one memory location to another.

Parameters:
offset - the offset from the start of the memory location this Pointer represents to begin copying to.
src - the destination memory location to transfer data from.
srcOffset - the offset from the start of the memory location the destination Pointer represents 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:
java.lang.IndexOutOfBoundsException

setMemory

public abstract void setMemory(long offset,
                               long size,
                               byte value)
Sets the value of each byte in the memory area represented by this Pointer. to a specified value.

Parameters:
offset - the offset from the start of the memory location this Pointer represents 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 this Pointer.

Parameters:
offset - the offset from the start of the memory location this Pointer represents to begin searching.
value - the byte value 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 this Pointer.

Parameters:
offset - the offset from the start of the memory location this Pointer represents to begin searching.
value - the byte value 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.


Copyright © 2012. All Rights Reserved.