Class UnsafeMemoryIO

java.lang.Object
com.kenai.jffi.MemoryIO
com.kenai.jffi.UnsafeMemoryIO

public abstract class UnsafeMemoryIO
extends MemoryIO
An implementation of MemoryIO using sun.misc.Unsafe
  • Field Summary

    Fields
    Modifier and Type Field Description
    protected static sun.misc.Unsafe unsafe  
  • Constructor Summary

    Constructors
    Constructor Description
    UnsafeMemoryIO()  
  • Method Summary

    Modifier and Type Method Description
    void _copyMemory​(long src, long dst, long size)  
    byte getByte​(long address)
    Reads an 8 bit integer from a native memory location.
    void getByteArray​(long address, byte[] data, int offset, int length)
    Reads a java byte array from native memory.
    void getCharArray​(long address, char[] data, int offset, int length)
    Reads a java char array from native memory.
    double getDouble​(long address)
    Reads a 64 bit floating point value from a native memory location.
    void getDoubleArray​(long address, double[] data, int offset, int length)
    Reads a java double array from native memory.
    float getFloat​(long address)
    Reads a 32 bit floating point value from a native memory location.
    void getFloatArray​(long address, float[] data, int offset, int length)
    Reads a java float array from native memory.
    int getInt​(long address)
    Reads a 32 bit integer from a native memory location.
    void getIntArray​(long address, int[] data, int offset, int length)
    Reads a java int array from native memory.
    long getLong​(long address)
    Reads a 64 bit integer from a native memory location.
    void getLongArray​(long address, long[] data, int offset, int length)
    Reads a java long array from native memory.
    short getShort​(long address)
    Reads a 16 bit integer from a native memory location.
    void getShortArray​(long address, short[] data, int offset, int length)
    Reads a java short array from native memory.
    long getStringLength​(long address)
    Gets the length of a native ascii or utf-8 string.
    byte[] getZeroTerminatedByteArray​(long address)
    Reads a byte array from native memory, stopping when a zero byte is found.
    byte[] getZeroTerminatedByteArray​(long address, int maxlen)
    Reads a byte array from native memory, stopping when a zero byte is found, or the maximum length is reached.
    long memchr​(long address, int value, long size)
    Gets the address of a byte value in a native memory region.
    void memcpy​(long dst, long src, long size)
    Copies bytes from one memory location to another.
    void memmove​(long dst, long src, long size)
    Copies potentially overlapping memory areas.
    void putByte​(long address, byte value)
    Writes an 8 bit integer value to a native memory location.
    void putByteArray​(long address, byte[] data, int offset, int length)
    Writes a java byte array to native memory.
    void putCharArray​(long address, char[] data, int offset, int length)
    Writes a java char array to native memory.
    void putDouble​(long address, double value)
    Writes a 64 bit floating point value to a native memory location.
    void putDoubleArray​(long address, double[] data, int offset, int length)
    Writes a java double array to native memory.
    void putFloat​(long address, float value)
    Writes a 32 bit floating point value to a native memory location.
    void putFloatArray​(long address, float[] data, int offset, int length)
    Writes a java double array to native memory.
    void putInt​(long address, int value)
    Writes a 32 bit integer value to a native memory location.
    void putIntArray​(long address, int[] data, int offset, int length)
    Writes a java int array to native memory.
    void putLong​(long address, long value)
    Writes a 64 bit integer value to a native memory location.
    void putLongArray​(long address, long[] data, int offset, int length)
    Writes a java long array to native memory.
    void putShort​(long address, short value)
    Writes a 16 bit integer value to a native memory location.
    void putShortArray​(long address, short[] data, int offset, int length)
    Writes a java short array to native memory.
    void putZeroTerminatedByteArray​(long address, byte[] data, int offset, int length)
    Copies a java byte array to native memory and appends a NUL terminating byte.
    void setMemory​(long src, long size, byte value)
    Sets a region of native memory to a specific byte value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • unsafe

      protected static sun.misc.Unsafe unsafe
  • Constructor Details

    • UnsafeMemoryIO

      public UnsafeMemoryIO()
  • Method Details

    • getByte

      public final byte getByte​(long address)
      Description copied from class: MemoryIO
      Reads an 8 bit integer from a native memory location.
      Specified by:
      getByte in class MemoryIO
      Parameters:
      address - The memory location to get the value from.
      Returns:
      A byte containing the value.
    • getShort

      public final short getShort​(long address)
      Description copied from class: MemoryIO
      Reads a 16 bit integer from a native memory location.
      Specified by:
      getShort in class MemoryIO
      Parameters:
      address - The memory location to get the value from.
      Returns:
      A short containing the value.
    • getInt

      public final int getInt​(long address)
      Description copied from class: MemoryIO
      Reads a 32 bit integer from a native memory location.
      Specified by:
      getInt in class MemoryIO
      Parameters:
      address - The memory location to get the value from.
      Returns:
      An int containing the value.
    • getLong

      public final long getLong​(long address)
      Description copied from class: MemoryIO
      Reads a 64 bit integer from a native memory location.
      Specified by:
      getLong in class MemoryIO
      Parameters:
      address - The memory location to get the value from.
      Returns:
      A long containing the value.
    • getFloat

      public final float getFloat​(long address)
      Description copied from class: MemoryIO
      Reads a 32 bit floating point value from a native memory location.
      Specified by:
      getFloat in class MemoryIO
      Parameters:
      address - The memory location to get the value from.
      Returns:
      A float containing the value.
    • getDouble

      public final double getDouble​(long address)
      Description copied from class: MemoryIO
      Reads a 64 bit floating point value from a native memory location.
      Specified by:
      getDouble in class MemoryIO
      Parameters:
      address - The memory location to get the value from.
      Returns:
      A double containing the value.
    • putByte

      public final void putByte​(long address, byte value)
      Description copied from class: MemoryIO
      Writes an 8 bit integer value to a native memory location.
      Specified by:
      putByte in class MemoryIO
      Parameters:
      address - The memory location to put the value.
      value - The value to write to memory.
    • putShort

      public final void putShort​(long address, short value)
      Description copied from class: MemoryIO
      Writes a 16 bit integer value to a native memory location.
      Specified by:
      putShort in class MemoryIO
      Parameters:
      address - The memory location to put the value.
      value - The value to write to memory.
    • putInt

      public final void putInt​(long address, int value)
      Description copied from class: MemoryIO
      Writes a 32 bit integer value to a native memory location.
      Specified by:
      putInt in class MemoryIO
      Parameters:
      address - The memory location to put the value.
      value - The value to write to memory.
    • putLong

      public final void putLong​(long address, long value)
      Description copied from class: MemoryIO
      Writes a 64 bit integer value to a native memory location.
      Specified by:
      putLong in class MemoryIO
      Parameters:
      address - The memory location to put the value.
      value - The value to write to memory.
    • putFloat

      public final void putFloat​(long address, float value)
      Description copied from class: MemoryIO
      Writes a 32 bit floating point value to a native memory location.
      Specified by:
      putFloat in class MemoryIO
      Parameters:
      address - The memory location to put the value.
      value - The value to write to memory.
    • putDouble

      public final void putDouble​(long address, double value)
      Description copied from class: MemoryIO
      Writes a 64 bit floating point value to a native memory location.
      Specified by:
      putDouble in class MemoryIO
      Parameters:
      address - The memory location to put the value.
      value - The value to write to memory.
    • _copyMemory

      public final void _copyMemory​(long src, long dst, long size)
    • setMemory

      public final void setMemory​(long src, long size, byte value)
      Description copied from class: MemoryIO
      Sets a region of native memory to a specific byte value.
      Specified by:
      setMemory in class MemoryIO
      Parameters:
      src - The address of start of the native memory.
      size - The number of bytes to set.
      value - The value to set the native memory to.
    • memcpy

      public final void memcpy​(long dst, long src, long size)
      Description copied from class: MemoryIO
      Copies bytes from one memory location to another. The memory areas
      Specified by:
      memcpy in class MemoryIO
      Parameters:
      dst - The destination memory address.
      src - The source memory address.
      size - The number of bytes to copy.
    • memmove

      public final void memmove​(long dst, long src, long size)
      Description copied from class: MemoryIO
      Copies potentially overlapping memory areas.
      Specified by:
      memmove in class MemoryIO
      Parameters:
      dst - The destination memory address.
      src - The source memory address.
      size - The number of bytes to copy.
    • memchr

      public final long memchr​(long address, int value, long size)
      Description copied from class: MemoryIO
      Gets the address of a byte value in a native memory region.
      Specified by:
      memchr in class MemoryIO
      Parameters:
      address - The native memory address to start searching.
      value - The value to search for.
      size - The size of the native memory region being searched.
      Returns:
      The address of the value, or 0 (zero) if not found.
    • putByteArray

      public final void putByteArray​(long address, byte[] data, int offset, int length)
      Description copied from class: MemoryIO
      Writes a java byte array to native memory.
      Specified by:
      putByteArray in class MemoryIO
      Parameters:
      address - The native memory address to copy the array to.
      data - The java array to copy.
      offset - The offset within the array to start copying from.
      length - The number of array elements to copy.
    • getByteArray

      public final void getByteArray​(long address, byte[] data, int offset, int length)
      Description copied from class: MemoryIO
      Reads a java byte array from native memory.
      Specified by:
      getByteArray in class MemoryIO
      Parameters:
      address - The native memory address to copy the array from.
      data - The java array to copy.
      offset - The offset within the array to start copying to.
      length - The number of array elements to copy.
    • putCharArray

      public final void putCharArray​(long address, char[] data, int offset, int length)
      Description copied from class: MemoryIO
      Writes a java char array to native memory.
      Specified by:
      putCharArray in class MemoryIO
      Parameters:
      address - The native memory address to copy the array to.
      data - The java array to copy.
      offset - The offset within the array to start copying from.
      length - The number of array elements to copy.
    • getCharArray

      public final void getCharArray​(long address, char[] data, int offset, int length)
      Description copied from class: MemoryIO
      Reads a java char array from native memory.
      Specified by:
      getCharArray in class MemoryIO
      Parameters:
      address - The native memory address to copy the array from.
      data - The java array to copy.
      offset - The offset within the array to start copying to.
      length - The number of array elements to copy.
    • putShortArray

      public final void putShortArray​(long address, short[] data, int offset, int length)
      Description copied from class: MemoryIO
      Writes a java short array to native memory.
      Specified by:
      putShortArray in class MemoryIO
      Parameters:
      address - The native memory address to copy the array to.
      data - The java array to copy.
      offset - The offset within the array to start copying from.
      length - The number of array elements to copy.
    • getShortArray

      public final void getShortArray​(long address, short[] data, int offset, int length)
      Description copied from class: MemoryIO
      Reads a java short array from native memory.
      Specified by:
      getShortArray in class MemoryIO
      Parameters:
      address - The native memory address to copy the array from.
      data - The java array to copy.
      offset - The offset within the array to start copying to.
      length - The number of array elements to copy.
    • putIntArray

      public final void putIntArray​(long address, int[] data, int offset, int length)
      Description copied from class: MemoryIO
      Writes a java int array to native memory.
      Specified by:
      putIntArray in class MemoryIO
      Parameters:
      address - The native memory address to copy the array to.
      data - The java array to copy.
      offset - The offset within the array to start copying from.
      length - The number of array elements to copy.
    • getIntArray

      public final void getIntArray​(long address, int[] data, int offset, int length)
      Description copied from class: MemoryIO
      Reads a java int array from native memory.
      Specified by:
      getIntArray in class MemoryIO
      Parameters:
      address - The native memory address to copy the array from.
      data - The java array to copy.
      offset - The offset within the array to start copying to.
      length - The number of array elements to copy.
    • putLongArray

      public final void putLongArray​(long address, long[] data, int offset, int length)
      Description copied from class: MemoryIO
      Writes a java long array to native memory.
      Specified by:
      putLongArray in class MemoryIO
      Parameters:
      address - The native memory address to copy the array to.
      data - The java array to copy.
      offset - The offset within the array to start copying from.
      length - The number of array elements to copy.
    • getLongArray

      public final void getLongArray​(long address, long[] data, int offset, int length)
      Description copied from class: MemoryIO
      Reads a java long array from native memory.
      Specified by:
      getLongArray in class MemoryIO
      Parameters:
      address - The native memory address to copy the array from.
      data - The java array to copy.
      offset - The offset within the array to start copying to.
      length - The number of array elements to copy.
    • putFloatArray

      public final void putFloatArray​(long address, float[] data, int offset, int length)
      Description copied from class: MemoryIO
      Writes a java double array to native memory.
      Specified by:
      putFloatArray in class MemoryIO
      Parameters:
      address - The native memory address to copy the array to.
      data - The java array to copy.
      offset - The offset within the array to start copying from.
      length - The number of array elements to copy.
    • getFloatArray

      public final void getFloatArray​(long address, float[] data, int offset, int length)
      Description copied from class: MemoryIO
      Reads a java float array from native memory.
      Specified by:
      getFloatArray in class MemoryIO
      Parameters:
      address - The native memory address to copy the array from.
      data - The java array to copy.
      offset - The offset within the array to start copying to.
      length - The number of array elements to copy.
    • putDoubleArray

      public final void putDoubleArray​(long address, double[] data, int offset, int length)
      Description copied from class: MemoryIO
      Writes a java double array to native memory.
      Specified by:
      putDoubleArray in class MemoryIO
      Parameters:
      address - The native memory address to copy the array to.
      data - The java array to copy.
      offset - The offset within the array to start copying from.
      length - The number of array elements to copy.
    • getDoubleArray

      public final void getDoubleArray​(long address, double[] data, int offset, int length)
      Description copied from class: MemoryIO
      Reads a java double array from native memory.
      Specified by:
      getDoubleArray in class MemoryIO
      Parameters:
      address - The native memory address to copy the array from.
      data - The java array to copy.
      offset - The offset within the array to start copying to.
      length - The number of array elements to copy.
    • getStringLength

      public final long getStringLength​(long address)
      Description copied from class: MemoryIO
      Gets the length of a native ascii or utf-8 string.
      Specified by:
      getStringLength in class MemoryIO
      Parameters:
      address - The native address of the string.
      Returns:
      The length of the string, in bytes.
    • getZeroTerminatedByteArray

      public final byte[] getZeroTerminatedByteArray​(long address)
      Description copied from class: MemoryIO
      Reads a byte array from native memory, stopping when a zero byte is found. This can be used to read ascii or utf-8 strings from native memory.
      Specified by:
      getZeroTerminatedByteArray in class MemoryIO
      Parameters:
      address - The address to read the data from.
      Returns:
      The byte array containing a copy of the native data. Any zero byte is stripped from the end.
    • getZeroTerminatedByteArray

      public final byte[] getZeroTerminatedByteArray​(long address, int maxlen)
      Description copied from class: MemoryIO
      Reads a byte array from native memory, stopping when a zero byte is found, or the maximum length is reached. This can be used to read ascii or utf-8 strings from native memory.
      Specified by:
      getZeroTerminatedByteArray in class MemoryIO
      Parameters:
      address - The address to read the data from.
      maxlen - The limit of the memory area to scan for a zero byte.
      Returns:
      The byte array containing a copy of the native data. Any zero byte is stripped from the end.
    • putZeroTerminatedByteArray

      public final void putZeroTerminatedByteArray​(long address, byte[] data, int offset, int length)
      Description copied from class: MemoryIO
      Copies a java byte array to native memory and appends a NUL terminating byte. Note A total of length + 1 bytes is written to native memory.
      Specified by:
      putZeroTerminatedByteArray in class MemoryIO
      Parameters:
      address - The address to copy to.
      data - The byte array to copy to native memory
      offset - The offset within the byte array to begin copying from
      length - The number of bytes to copy to native memory