com.jinvoke
Class Util

java.lang.Object
  extended by com.jinvoke.Util

public class Util
extends java.lang.Object

Provides utility methods for converting Java types to native types and vice versa. Also provides miscellaneous methods for obtaining the size of structs, pointers, chars etc.


Method Summary
static int byteArrayToPtr(byte[] ba)
          Allocates a block of native memory and copies the contents of the passed in byte array to native memory.
static void free(int ptr)
          Frees a block of native memory that has been allocated using alloc()
static void freeCoTaskMem(int ptr)
          Frees a block of native memory that has been allocated using CoTaskMemAlloc().
static javax.swing.Icon getIcon(int hicon)
          Converts a Windows HICON to a Java Icon.
static javax.swing.Icon getIcon(int hicon, int iconSize)
          Converts a Windows HICON to a Java Icon.
static int getLastError()
          On Windows, returns the calling threads's last error code.
static int getStructSize(java.lang.Class structClass)
          Returns the number of bytes occupied by a struct in native memory
static int getStructSize(java.lang.Object struct)
          Returns the number of bytes occupied by a struct in native memory
static int getWindowHandle(java.awt.Component component)
          Returns the Win32 HWND for a heavyweight Java component
static byte[] ptrToByteArray(int ptr, int numelements)
          Allocates a Java byte array and copies numelements bytes pointed to by ptr into the Java byte array.
static java.lang.String ptrToString(int ptr)
          Allocates a Java String and copies the characters from a null terminated Unicode string into it.
static java.lang.String ptrToStringAnsi(int ptr)
          Allocates a Java String and copies the characters from a null terminated Ansi string into it.
static
<T> T
ptrToStruct(int ptr, java.lang.Class<T> structclass)
          Converts data from a native block of memory to a Java object representing a structure.
static void ptrToStruct(int ptr, java.lang.Object o)
          Converts data from a native block of memory to a Java object representing a structure.
static byte readByte(int ptr)
          Reads a byte value from a pointer to native memory
static int readInt(int ptr)
          Reads an int from a pointer to native memory
static long readLong(int ptr)
          Reads a long from a pointer to native memory
static short readShort(int ptr)
          Reads a short from a pointer to native memory
static void readShort(int ptr, short s)
          Writes a short to native memory
static int sizeOfChar()
          Returns the number of bytes occupied by a char in native memory
static int sizeOfPointer()
          Returns the number of bytes occupied by a pointer in native memory
static int sizeOfWideChar()
          Returns the number of bytes occupied by a wide-character(wchar_t) in native memory
static int stringToPtr(java.lang.String s)
          Allocates a native Unicode(wide-character) string in native memory and sets it's characters to those in the Java String s.
static int stringToPtrAnsi(java.lang.String s)
          Allocates a native Ansi(char) string in native memory and sets it's characters to those in the Java String s.
static void writeByte(int ptr, byte b)
          Writes a byte value to native memory
static void writeInt(int ptr, int i)
          Writes an int to native memory
static void writeLong(int ptr, long l)
          Writes a long to native memory
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

ptrToStruct

public static <T> T ptrToStruct(int ptr,
                                java.lang.Class<T> structclass)
Converts data from a native block of memory to a Java object representing a structure.

This method creates a new instance of the Java class structclass representing a native struct and sets it's fields to the corresponding values in the native structure.

The structclass must represent a native struct. i.e. it must be annotated with the NativeStruct annotation, and must have public fields corresponding to members of the native struct.

Parameters:
ptr - - A pointer in native code (pointing to a struct)
structclass - - A class representing a native struct (annotated with @NativeStruct annotation)
Returns:
an instance of structclass initialized with values obtained from native struct pointer to by ptr

ptrToStruct

public static void ptrToStruct(int ptr,
                               java.lang.Object o)
Converts data from a native block of memory to a Java object representing a structure.

This method sets the fields of the Java object o representing a native struct to the corresponding values in the native structure.

The passed object must be an instance of a class annotated with the @NativeStruct annotation, and must have public fields corresponding to members of the native struct.

Parameters:
ptr - - A pointer to a struct in native memory
o - - An instance of a class representing a native struct (annotated with @NativeStruct annotation)

ptrToString

public static java.lang.String ptrToString(int ptr)
Allocates a Java String and copies the characters from a null terminated Unicode string into it. This method does NOT free the native memory occupied by the native string. Use freeCoTaskMem() for freeing the native memory.

Parameters:
ptr - - A pointer to a wide-character string in native memory
Returns:
a Java String with the same characters as the native string.

ptrToStringAnsi

public static java.lang.String ptrToStringAnsi(int ptr)
Allocates a Java String and copies the characters from a null terminated Ansi string into it. This method does NOT free the native memory occupied by the native string. Use free() for freeing the native memory.

Parameters:
ptr - - A pointer to an Ansi/character string in native memory
Returns:
a Java String with the same characters as the native string.

free

public static void free(int ptr)
Frees a block of native memory that has been allocated using alloc()

Parameters:
ptr - - address of the block of memory to be freed

freeCoTaskMem

public static void freeCoTaskMem(int ptr)
Frees a block of native memory that has been allocated using CoTaskMemAlloc().

Parameters:
ptr - - address of the block of memory to be freed

stringToPtr

public static int stringToPtr(java.lang.String s)
Allocates a native Unicode(wide-character) string in native memory and sets it's characters to those in the Java String s. Memory for the native string is allocated using CoTaskMemAlloc and can be freed by the freeCoTaskMem() method.

Parameters:
s - - A Java String which is to be sent to native code as a pointer (LPWSTR, LPCWSTR, wchar_t*, etc).

stringToPtrAnsi

public static int stringToPtrAnsi(java.lang.String s)
Allocates a native Ansi(char) string in native memory and sets it's characters to those in the Java String s. Memory for the native string is allocated using malloc and can be freed by the free() method.

Parameters:
s - - A Java String which is to be sent to native code as a pointer (LPSTR, LPCSTR, char*, etc).

sizeOfChar

public static int sizeOfChar()
Returns the number of bytes occupied by a char in native memory

Returns:
the number of bytes occupied by a char in native memory

sizeOfPointer

public static int sizeOfPointer()
Returns the number of bytes occupied by a pointer in native memory

Returns:
the number of bytes occupied by a pointer in native memory

sizeOfWideChar

public static int sizeOfWideChar()
Returns the number of bytes occupied by a wide-character(wchar_t) in native memory

Returns:
the number of bytes occupied by a wide-character(wchar_t) in native memory

getStructSize

public static int getStructSize(java.lang.Object struct)
Returns the number of bytes occupied by a struct in native memory

Parameters:
struct - - An instance of a Java class representing a native struct (annotated with @NativeStruct annotation)
Returns:
the number of bytes occupied by a struct in native memory

getStructSize

public static int getStructSize(java.lang.Class structClass)
Returns the number of bytes occupied by a struct in native memory

Parameters:
structClass - - The Java class representing a native struct (annotated with @NativeStruct annotation)
Returns:
the number of bytes occupied by a struct in native memory

byteArrayToPtr

public static int byteArrayToPtr(byte[] ba)
Allocates a block of native memory and copies the contents of the passed in byte array to native memory.

The number of bytes allocated is determined based on the size of the passed in byte array, ba. The memory is allocated using malloc().

Parameters:
ba - - the byte array to be sent to native code
Returns:
a pointer to the block of native memory allocated by this method

ptrToByteArray

public static byte[] ptrToByteArray(int ptr,
                                    int numelements)
Allocates a Java byte array and copies numelements bytes pointed to by ptr into the Java byte array. This method also frees the native memory pointed to by ptr

Parameters:
ptr - - A pointer to a byte buffer in native memory
numelements - - the number of bytes to be copied to the returned Java byte array
Returns:
a byte array of length numelements containing the bytes pointed to by ptr

getWindowHandle

public static int getWindowHandle(java.awt.Component component)
Returns the Win32 HWND for a heavyweight Java component

Parameters:
component - a heavyweight Java component
Returns:
the Win32 window handle (HWND) for the Java component

getLastError

public static int getLastError()
On Windows, returns the calling threads's last error code. Most functions that set the last-error code set it when they fail. Thus, the error code should be checked only if the error indicated failure.

Returns:
the last error code set by the previous native method call in the current thread (on Windows platform).

readByte

public static byte readByte(int ptr)
Reads a byte value from a pointer to native memory

Parameters:
ptr -
Returns:
byte at the memory location specified in ptr argument

readShort

public static short readShort(int ptr)
Reads a short from a pointer to native memory

Parameters:
ptr -
Returns:
short at the memory location specified in ptr argument

readInt

public static int readInt(int ptr)
Reads an int from a pointer to native memory

Parameters:
ptr -
Returns:
int at the memory location specified in ptr argument

readLong

public static long readLong(int ptr)
Reads a long from a pointer to native memory

Parameters:
ptr -
Returns:
long at the memory location specified in ptr argument

writeByte

public static void writeByte(int ptr,
                             byte b)
Writes a byte value to native memory

Parameters:
ptr -
b - byte to write at the memory location specified in ptr argument

readShort

public static void readShort(int ptr,
                             short s)
Writes a short to native memory

Parameters:
ptr -
s - short to write at the memory location specified in ptr argument

writeInt

public static void writeInt(int ptr,
                            int i)
Writes an int to native memory

Parameters:
ptr -
i - int to write at the memory location specified in ptr argument

writeLong

public static void writeLong(int ptr,
                             long l)
Writes a long to native memory

Parameters:
ptr -
l - long to write at the memory location specified in ptr argument

getIcon

public static javax.swing.Icon getIcon(int hicon)
Converts a Windows HICON to a Java Icon. The native hicon is freed/destroyed in this function.

Parameters:
hicon - the HICON to be converted
Returns:
the converted Java Icon

getIcon

public static javax.swing.Icon getIcon(int hicon,
                                       int iconSize)
Converts a Windows HICON to a Java Icon. The native hicon is freed/destroyed in this function.

Parameters:
hicon - the HICON to be converted
iconSize - icon size typically 16 or 32 - if you don't know the size use getIcon(int hicon)
Returns:
the converted Java Icon