com.moesol.bindings
Class NativeStructure

java.lang.Object
  extended by com.moesol.bindings.NativeStructure
All Implemented Interfaces:
Comparable
Direct Known Subclasses:
BITMAP, BITMAPFILEHEADER, BITMAPINFOHEADER, GUID, LOGFONT, MSG, NASTY_STRUCT, NASTY_STRUCT, NON_OLE_AUTO_STRUCT, NON_OLE_AUTO_STRUCT, POINT, PROPPAGEINFO, RECT, SIZE, STGMEDIUM, tag_typeDefTest, tag_typeDefTest, TEST_FIELDS, TEST_FIELDS, TEST_NO_UUID_STRUCT, TEST_NO_UUID_STRUCT, TEST_STRUCT, TEST_STRUCT, TEXTMETRIC, VARIANT

public class NativeStructure
extends Object
implements Comparable

Wraps the bytes of a native structure with a java class that allows easier access to the java compatible values of structure fields at given offsets. A union is treated in the same way as a structure, but the offsets to the fields may be the same. For example, the may all be at zero. Nested structures are supported by creating slices. Since the slices refer to the same underlying bytes when a nested structure is changed through the java wrapper so is the outer structure. However, primitive arrays are copied.


Constructor Summary
protected NativeStructure(byte[] bytes)
           
protected NativeStructure(ByteBuffer bb)
           
 
Method Summary
 ByteBuffer _getByteBuffer()
          JUnit support
 byte[] _getStructureBytes()
          JNI support
 void clearRecord()
          NativeStructures that contain fields of type BSTR, VARIANT, SAFEARRAY, or COM Interface must call clearRecord or native resources will be leaked.
 int compareTo(Object other)
          Compares this native structure to another.
 boolean equals(Object other)
          Tells whether or not this structure is equal to another structure.
protected  void freeLpwstr(int offset)
           
protected  byte[] getArray(int offset, int buflen)
          Gets a copy of the array.
protected  boolean getBool(int offset)
           
protected  boolean[] getBOOLArray(int offset)
           
protected  String getBstr(int offset)
           
protected  String[] getBSTRArray(int offset)
           
protected  String[] getBstrArray(int offset, int count)
           
protected  byte getByte(int offset)
           
protected  short[] getBYTEArray(int offset)
           
protected  byte[] getByteArray(int offset, int buflen)
          Gets a copy of the array.
 ByteBuffer getByteBuffer()
          Gets the byte buffer used as this structures underlying storage.
static int getByteSize()
           
protected  String getByteString(int offset, int buflen)
           
protected  Date getDate(int offset)
           
protected  double getDouble(int offset)
           
protected  double[] getDOUBLEArray(int offset)
           
protected  double[] getDoubleArray(int offset, int count)
           
static int getDoubleSize()
           
protected  float getFloat(int offset)
           
protected  float[] getFLOATArray(int offset)
           
protected  float[] getFloatArray(int offset, int count)
           
static int getFloatSize()
           
protected  int getInt(int offset)
           
protected  int[] getINTArray(int offset)
           
protected  int[] getIntArray(int offset, int count)
           
protected  void getInterface(int offset, Object[] out)
           
static int getIntSize()
           
protected  long getLong(int offset)
           
protected  int[] getLONGArray(int offset)
           
static int getLongSize()
          Number of bytes in a C/C++ 32 bit long.
protected  String getLpwstr(int offset)
           
protected  int[] getPtrInt(int offset, int numIndirect)
           
protected  short[] getPtrShort(int offset, int numPointers)
           
protected  String getPtrShortString(int offset, int numPointers)
           
protected  ByteBuffer getPtrStruct(int offset, int size, int numIndirect)
           
protected  short[] getPtrUnsignedByte(int offset, int numIndirect)
           
protected  char[] getPtrUnsignedShort(int offset, int numIndirect)
           
protected  Object[] getPtrVariant(int offset, int numIndirect)
           
protected  ByteBuffer getPtrvoid(int offset, int numIndirect)
           
protected  ByteBuffer getRemaining(int offset)
          Gets a slice of the byte buffer that is all bytes after offset.
protected  int getSCODE(int offset)
           
protected  short getShort(int offset)
           
protected  short[] getSHORTArray(int offset)
           
protected  short[] getShortArray(int offset, int count)
           
static int getShortSize()
           
protected  ByteBuffer getSlice(int offset, int length)
          Gets a slice of the array.
protected  ByteBuffer getSlice(int offset, long length)
          Pass along a ULONG to getSlice as an int
protected  String getString(int offset, int buflen)
          Superceded by getByteString
protected  void getStruct(int offset, int length, NativeStructure s)
          Copy the sub-structure into s
protected  void getStruct(int offset, int length, NativeStructure[] v)
           
protected  void getStructArray(int offset, int size, int count, NativeStructure[] v)
          Copy the array sub-structures into v.
protected  long[] getUINTArray(int offset)
           
protected  short getUnsignedByte(int offset)
           
protected  short[] getUnsignedByteArray(int offset, int count)
          Gets a copy of the array.
static int getUnsignedByteSize()
           
protected  long getUnsignedInt(int offset)
           
protected  long[] getUnsignedIntArray(int offset, int count)
          Gets a copy of the array.
static int getUnsignedIntSize()
           
protected  BigInteger getUnsignedLongLong(int offset)
           
static int getUnsignedLongSize()
           
protected  char getUnsignedShort(int offset)
           
protected  char[] getUnsignedShortArray(int offset, int count)
          Gets a copy of the array.
static int getUnsignedShortSize()
           
protected  char[] getUSHORTArray(int offset)
           
protected  Object getVariant(int offset)
           
protected  Object[] getVARIANTArray(int offset)
           
 int hashCode()
          Computes the hash of the bytes of this native structure.
 ByteOrder order()
          Get the byte order for this structure.
 void order(ByteOrder bo)
          Allow the native order to be overriden for byte swapped files.
protected  void putArray(int offset, int buflen, byte[] v)
          Copies v into this structure.
protected  void putBool(int offset, boolean v)
           
protected  void putBOOLArray(int offset, boolean[] bytes)
           
protected  void putBstr(int offset, String v)
           
protected  void putBstrArray(int offset, int count, String[] v)
           
protected  void putBSTRArray(int offset, String[] bytes)
           
protected  void putByte(int offset, byte v)
           
protected  void putByteArray(int offset, int buflen, byte[] v)
           
protected  void putBYTEArray(int offset, short[] bytes)
           
protected  void putByteString(int offset, int buflen, String v)
           
protected  void putDate(int offset, Date v)
           
protected  void putDouble(int offset, double v)
           
protected  void putDOUBLEArray(int offset, double[] bytes)
           
protected  void putDoubleArray(int offset, int count, double[] v)
           
protected  void putFloat(int offset, float v)
           
protected  void putFLOATArray(int offset, float[] bytes)
           
protected  void putFloatArray(int offset, int count, float[] v)
           
protected  void putInt(int offset, int v)
           
protected  void putINTArray(int offset, int[] bytes)
           
protected  void putIntArray(int offset, int count, int[] v)
           
protected  void putInterface(int offset, IUnknown unk)
           
protected  void putLong(int offset, long v)
           
protected  void putLONGArray(int offset, int[] bytes)
           
protected  void putLpwstr(int offset, String v)
           
protected  void putPtrInt(int offset, int numIndirect, int[] v)
           
protected  void putPtrShort(int offset, int numPointers, short[] v)
           
protected  void putPtrShortString(int offet, int numPointers, String v)
           
protected  void putPtrStruct(int offset, int size, int numIndirect, NativeStructure[] v)
           
protected  void putPtrUnsignedByte(int offset, int numIndirect, short[] v)
           
protected  void putPtrUnsignedShort(int offset, int numIndirect, char[] v)
           
protected  void putPtrVariant(int offset, int numIndirect, Object[] v)
           
protected  void putPtrvoid(int offset, int numIndirect, ByteBuffer v)
           
protected  void putSCODE(int offset, int v)
           
protected  void putShort(int offset, short v)
           
protected  void putShortArray(int offset, int count, short[] v)
           
protected  void putSHORTArray(int offset, short[] bytes)
           
protected  void putString(int offset, int buflen, String v)
          Superceded by putByteString
protected  void putStruct(int offset, int length, NativeStructure s)
          Copy s into a sub-structure of this.
protected  void putStruct(int offset, int length, NativeStructure[] v)
           
protected  void putStructArray(int offset, int size, int count, NativeStructure[] v)
          Copy v into this structure
protected  void putUINTArray(int offset, long[] bytes)
           
protected  void putUnsignedByte(int offset, short v)
           
protected  void putUnsignedByteArray(int offset, int count, short[] v)
          Copies the array into this struct in native byte order and packing.
protected  void putUnsignedInt(int offset, long v)
           
protected  void putUnsignedIntArray(int offset, int count, long[] v)
          Copies the array into this struct in native byte order and packing.
protected  void putUnsignedLongLong(int offset, BigInteger v)
           
protected  void putUnsignedShort(int offset, char v)
           
protected  void putUnsignedShortArray(int offset, int count, char[] v)
          Copies the array into this struct in native byte order and packing.
protected  void putUSHORTArray(int offset, char[] bytes)
           
protected  void putVariant(int offset, Object v)
           
protected  void putVARIANTArray(int offset, Object[] bytes)
           
static String stringFromC(byte[] s)
          Turn an array of bytes that is a C '\0' terminated string into a java string.
static String stringFromC(byte[] s, int offset, int length)
          Turn an array of bytes that is a C '\0' terminated string into a java string.
 void zero()
          Fill the structure with zeros
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NativeStructure

protected NativeStructure(byte[] bytes)

NativeStructure

protected NativeStructure(ByteBuffer bb)
Method Detail

order

public void order(ByteOrder bo)
Allow the native order to be overriden for byte swapped files.

Parameters:
bo - The new byte order.

order

public ByteOrder order()
Get the byte order for this structure.

Returns:
ByteOrder

zero

public void zero()
Fill the structure with zeros


putLong

protected void putLong(int offset,
                       long v)

getLong

protected long getLong(int offset)

putUnsignedLongLong

protected void putUnsignedLongLong(int offset,
                                   BigInteger v)

getUnsignedLongLong

protected BigInteger getUnsignedLongLong(int offset)

putSCODE

protected void putSCODE(int offset,
                        int v)

getSCODE

protected int getSCODE(int offset)

putInt

protected void putInt(int offset,
                      int v)

getInt

protected int getInt(int offset)

putUnsignedInt

protected void putUnsignedInt(int offset,
                              long v)

getUnsignedInt

protected long getUnsignedInt(int offset)

putBool

protected void putBool(int offset,
                       boolean v)

getBool

protected boolean getBool(int offset)

putShort

protected void putShort(int offset,
                        short v)

getShort

protected short getShort(int offset)

putUnsignedShort

protected void putUnsignedShort(int offset,
                                char v)

getUnsignedShort

protected char getUnsignedShort(int offset)

putByte

protected void putByte(int offset,
                       byte v)

getByte

protected byte getByte(int offset)

putUnsignedByte

protected void putUnsignedByte(int offset,
                               short v)

getUnsignedByte

protected short getUnsignedByte(int offset)

putDouble

protected void putDouble(int offset,
                         double v)

getDouble

protected double getDouble(int offset)

putFloat

protected void putFloat(int offset,
                        float v)

getFloat

protected float getFloat(int offset)

putString

protected void putString(int offset,
                         int buflen,
                         String v)
Superceded by putByteString


getString

protected String getString(int offset,
                           int buflen)
Superceded by getByteString


putByteString

protected void putByteString(int offset,
                             int buflen,
                             String v)

getByteString

protected String getByteString(int offset,
                               int buflen)

stringFromC

public static String stringFromC(byte[] s)
Turn an array of bytes that is a C '\0' terminated string into a java string.

Returns:
java string from a C string.

stringFromC

public static String stringFromC(byte[] s,
                                 int offset,
                                 int length)
Turn an array of bytes that is a C '\0' terminated string into a java string.

Parameters:
s -
offset -
length -
Returns:
java string from a C string.

putArray

protected void putArray(int offset,
                        int buflen,
                        byte[] v)
Copies v into this structure.

Parameters:
offset -
buflen -
v -

getArray

protected byte[] getArray(int offset,
                          int buflen)
Gets a copy of the array. Modifying the returned array has no effect on the structure.

Parameters:
offset -
buflen -
Returns:
copy of sub-array

putByteArray

protected void putByteArray(int offset,
                            int buflen,
                            byte[] v)

getByteArray

protected byte[] getByteArray(int offset,
                              int buflen)
Gets a copy of the array. Modifying the returned array has no effect on the structure.

Parameters:
offset -
buflen -
Returns:
copy of sub-array.

putUnsignedIntArray

protected void putUnsignedIntArray(int offset,
                                   int count,
                                   long[] v)
Copies the array into this struct in native byte order and packing.

Parameters:
offset -
count -
v -

getUnsignedIntArray

protected long[] getUnsignedIntArray(int offset,
                                     int count)
Gets a copy of the array. Modifying the returned array has no effect on the structure.

Parameters:
offset -
count -
Returns:
a new array that is a copy of the values in the struct.

putShortArray

protected void putShortArray(int offset,
                             int count,
                             short[] v)

getShortArray

protected short[] getShortArray(int offset,
                                int count)

putIntArray

protected void putIntArray(int offset,
                           int count,
                           int[] v)

getIntArray

protected int[] getIntArray(int offset,
                            int count)

putFloatArray

protected void putFloatArray(int offset,
                             int count,
                             float[] v)

getFloatArray

protected float[] getFloatArray(int offset,
                                int count)

putDoubleArray

protected void putDoubleArray(int offset,
                              int count,
                              double[] v)

getDoubleArray

protected double[] getDoubleArray(int offset,
                                  int count)

putBstrArray

protected void putBstrArray(int offset,
                            int count,
                            String[] v)

getBstrArray

protected String[] getBstrArray(int offset,
                                int count)

putUnsignedShortArray

protected void putUnsignedShortArray(int offset,
                                     int count,
                                     char[] v)
Copies the array into this struct in native byte order and packing.

Parameters:
offset -
count -
v -

getUnsignedShortArray

protected char[] getUnsignedShortArray(int offset,
                                       int count)
Gets a copy of the array. Modifying the returned array has no effect on the structure.

Parameters:
offset -
count -
Returns:
a new array that is a copy of the values in the struct.

putUnsignedByteArray

protected void putUnsignedByteArray(int offset,
                                    int count,
                                    short[] v)
Copies the array into this struct in native byte order and packing.

Parameters:
offset -
count -
v -

getUnsignedByteArray

protected short[] getUnsignedByteArray(int offset,
                                       int count)
Gets a copy of the array. Modifying the returned array has no effect on the structure.

Parameters:
offset -
count -
Returns:
a new array that is a copy of the values in the struct.

getRemaining

protected ByteBuffer getRemaining(int offset)
Gets a slice of the byte buffer that is all bytes after offset. It is expected that the callee will adjust the limit of the byte buffer to match the structure size. This method allows for structures that contain variable sized arrays.

Parameters:
offset -
Returns:
slice of remaining bytes.

getSlice

protected ByteBuffer getSlice(int offset,
                              int length)
Gets a slice of the array. Modifying the returned slice modifies the underlying structure as well.

Parameters:
offset - start offset of slice
length - length of slice
Returns:
new slice

getSlice

protected ByteBuffer getSlice(int offset,
                              long length)
Pass along a ULONG to getSlice as an int

Parameters:
offset -
length -

putStruct

protected void putStruct(int offset,
                         int length,
                         NativeStructure s)
Copy s into a sub-structure of this.

Parameters:
offset -
length -
s -

getStruct

protected void getStruct(int offset,
                         int length,
                         NativeStructure s)
Copy the sub-structure into s

Parameters:
offset -
length -
s -

putStruct

protected void putStruct(int offset,
                         int length,
                         NativeStructure[] v)

getStruct

protected void getStruct(int offset,
                         int length,
                         NativeStructure[] v)

putInterface

protected void putInterface(int offset,
                            IUnknown unk)

getInterface

protected void getInterface(int offset,
                            Object[] out)

putVariant

protected void putVariant(int offset,
                          Object v)

getVariant

protected Object getVariant(int offset)

putPtrVariant

protected void putPtrVariant(int offset,
                             int numIndirect,
                             Object[] v)

getPtrVariant

protected Object[] getPtrVariant(int offset,
                                 int numIndirect)

putPtrStruct

protected void putPtrStruct(int offset,
                            int size,
                            int numIndirect,
                            NativeStructure[] v)

getPtrStruct

protected ByteBuffer getPtrStruct(int offset,
                                  int size,
                                  int numIndirect)

putPtrInt

protected void putPtrInt(int offset,
                         int numIndirect,
                         int[] v)

getPtrInt

protected int[] getPtrInt(int offset,
                          int numIndirect)

putPtrUnsignedByte

protected void putPtrUnsignedByte(int offset,
                                  int numIndirect,
                                  short[] v)

getPtrUnsignedByte

protected short[] getPtrUnsignedByte(int offset,
                                     int numIndirect)

putPtrUnsignedShort

protected void putPtrUnsignedShort(int offset,
                                   int numIndirect,
                                   char[] v)

getPtrUnsignedShort

protected char[] getPtrUnsignedShort(int offset,
                                     int numIndirect)

putPtrvoid

protected void putPtrvoid(int offset,
                          int numIndirect,
                          ByteBuffer v)

getPtrvoid

protected ByteBuffer getPtrvoid(int offset,
                                int numIndirect)

putBstr

protected void putBstr(int offset,
                       String v)

getBstr

protected String getBstr(int offset)

putDate

protected void putDate(int offset,
                       Date v)

getDate

protected Date getDate(int offset)

putLpwstr

protected void putLpwstr(int offset,
                         String v)

getLpwstr

protected String getLpwstr(int offset)

freeLpwstr

protected void freeLpwstr(int offset)

putPtrShort

protected void putPtrShort(int offset,
                           int numPointers,
                           short[] v)

getPtrShort

protected short[] getPtrShort(int offset,
                              int numPointers)

putPtrShortString

protected void putPtrShortString(int offet,
                                 int numPointers,
                                 String v)

getPtrShortString

protected String getPtrShortString(int offset,
                                   int numPointers)

putSHORTArray

protected void putSHORTArray(int offset,
                             short[] bytes)

getSHORTArray

protected short[] getSHORTArray(int offset)

putBYTEArray

protected void putBYTEArray(int offset,
                            short[] bytes)

getBYTEArray

protected short[] getBYTEArray(int offset)

putUSHORTArray

protected void putUSHORTArray(int offset,
                              char[] bytes)

getUSHORTArray

protected char[] getUSHORTArray(int offset)

putBSTRArray

protected void putBSTRArray(int offset,
                            String[] bytes)

getBSTRArray

protected String[] getBSTRArray(int offset)

putBOOLArray

protected void putBOOLArray(int offset,
                            boolean[] bytes)

getBOOLArray

protected boolean[] getBOOLArray(int offset)

putLONGArray

protected void putLONGArray(int offset,
                            int[] bytes)

getLONGArray

protected int[] getLONGArray(int offset)

putUINTArray

protected void putUINTArray(int offset,
                            long[] bytes)

getUINTArray

protected long[] getUINTArray(int offset)

putDOUBLEArray

protected void putDOUBLEArray(int offset,
                              double[] bytes)

getDOUBLEArray

protected double[] getDOUBLEArray(int offset)

putFLOATArray

protected void putFLOATArray(int offset,
                             float[] bytes)

getFLOATArray

protected float[] getFLOATArray(int offset)

putINTArray

protected void putINTArray(int offset,
                           int[] bytes)

getINTArray

protected int[] getINTArray(int offset)

putVARIANTArray

protected void putVARIANTArray(int offset,
                               Object[] bytes)

getVARIANTArray

protected Object[] getVARIANTArray(int offset)

clearRecord

public void clearRecord()
NativeStructures that contain fields of type BSTR, VARIANT, SAFEARRAY, or COM Interface must call clearRecord or native resources will be leaked. NativeStructure subclasses generated by tlb2java that contain such fields have a finalize method generated that calls clearRecord. However, you may call clearRecord whenever you no longer need the NativeStructure to free resources.


putStructArray

protected void putStructArray(int offset,
                              int size,
                              int count,
                              NativeStructure[] v)
Copy v into this structure

Parameters:
offset -
size -
count -
v -

getStructArray

protected void getStructArray(int offset,
                              int size,
                              int count,
                              NativeStructure[] v)
Copy the array sub-structures into v.

Parameters:
offset -
size -
count -
v -

_getStructureBytes

public byte[] _getStructureBytes()
JNI support

Returns:
the byte array which contains the bytes of the structure.

_getByteBuffer

public ByteBuffer _getByteBuffer()
JUnit support

Returns:
the byte buffer which contains the bytes of the structure.

equals

public boolean equals(Object other)
Tells whether or not this structure is equal to another structure. The classes of the structures must be the same and their byte buffers must also be equal.

Overrides:
equals in class Object
Returns:
true if two have structures are the same.

compareTo

public int compareTo(Object other)
Compares this native structure to another.

Specified by:
compareTo in interface Comparable
Parameters:
other -
Returns:
<0, 0, or >0
Throws:
ClassCastException - if native structures are not of the same type.

getByteBuffer

public ByteBuffer getByteBuffer()
Gets the byte buffer used as this structures underlying storage. Changing the returned buffer changes the structure values. However, the returned buffer is a duplicate byte buffer so changes to the position, mark, and limit do not affect the internal buffer. _getByteBuffer is slightly more efficient, but the position is not guaranteed to be 0.

Returns:
the byte buffer used as this structures underlying storage.

hashCode

public int hashCode()
Computes the hash of the bytes of this native structure. The underlying byte buffer may be direct or indirect.

Overrides:
hashCode in class Object
Returns:
the computed hash of the bytes of the native structure.

getByteSize

public static int getByteSize()

getUnsignedByteSize

public static int getUnsignedByteSize()

getShortSize

public static int getShortSize()

getUnsignedShortSize

public static int getUnsignedShortSize()

getIntSize

public static int getIntSize()

getUnsignedIntSize

public static int getUnsignedIntSize()

getLongSize

public static int getLongSize()
Number of bytes in a C/C++ 32 bit long.

Returns:
size of a long in MSVC

getUnsignedLongSize

public static int getUnsignedLongSize()

getFloatSize

public static int getFloatSize()

getDoubleSize

public static int getDoubleSize()


Copyright © 2004 Moebius Solutions, Inc. All Rights Reserved.