package com.jstatcom.engine; /* * @(#)CPtr.java 1.8 98/03/22 * * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved. * * See also the LICENSE file in this distribution. */ /** * An abstraction for a C pointer data type. A CPtr instance represents, on the * Java side, a C pointer. The C pointer could be any type of C * pointer. Methods such as. * * @param offset * byte offset from pointer at whichcopyIn,copyOut, *getXXX, andsetXXX, provide means to indirect * the underlying C pointer. * * @author Sheng Liang * @see CFunc */ public class CPtr { /** * The size of a C pointer on the platform this Java virtual machine is * running on. */ public static final int SIZE; /** * A canonical representation of C's NULL pointer. */ public static final CPtr NULL; /** * Compares thisCPtrto the specified object. * * @param other * aCPtr* @return true if the class of thisCPtrobject and the * class ofotherare exactly equal, and the C * pointers being pointed to by these objects are also equal. * Returns false otherwise. */ public boolean equals(Object other) { if (other == null) return false; if (other == this) return true; if (CPtr.class != other.getClass()) return false; return peer == ((CPtr) other).peer; } /** * Returns a hashcode for the C pointer represented by this *Cptrobject. * * @return a hash code value for the represented C pointer. */ public int hashCode() { return (int) ((peer >>> 32) + (peer & 0xFFFFFFFF)); } /** * Indirect the C pointer, copying into memory pointed to by C * pointer, from the specified array. * * @param bOff * byte offset from pointer into which data is copied * @param buf *bytearray from which to copy * @param index * array index from which to start copying * @param length * number of elements frombufthat must be copied */ public native void copyIn(int bOff, byte[] buf, int index, int length); /** * Indirect the C pointer, copying into memory pointed to by C * pointer, from the specified array. * * @param bOff * byte offset from pointer into which data is copied * @param buf *shortarray from which to copy * @param index * array index from which to start copying * @param length * number of elements frombufthat must be copied */ public native void copyIn(int bOff, short[] buf, int index, int length); /** * Indirect the C pointer, copying into memory pointed to by C * pointer, from the specified array. * * @param bOff * byte offset from pointer into which data is copied * @param buf *chararray from which to copy * @param index * array index from which to start copying * @param length * number of elements frombufthat must be copied */ public native void copyIn(int bOff, char[] buf, int index, int length); /** * Indirect the C pointer, copying into memory pointed to by C * pointer, from the specified array. * * @param bOff * byte offset from pointer into which data is copied * @param buf *intarray from which to copy * @param index * array index from which to start copying * @param length * number of elements frombufthat must be copied */ public native void copyIn(int bOff, int[] buf, int index, int length); /** * Indirect the C pointer, copying into memory pointed to by C * pointer, from the specified array. * * @param bOff * byte offset from pointer into which data is copied * @param buf *longarray from which to copy * @param index * array index from which to start copying * @param length * number of elements frombufthat must be copied */ public native void copyIn(int bOff, long[] buf, int index, int length); /** * Indirect the C pointer, copying into memory pointed to by C * pointer, from the specified array. * * @param bOff * byte offset from pointer into which data is copied * @param buf *floatarray from which to copy * @param index * array index from which to start copying * @param length * number of elements frombufthat must be copied */ public native void copyIn(int bOff, float[] buf, int index, int length); /** * Indirect the C pointer, copying into memory pointed to by C * pointer, from the specified array. * * @param bOff * byte offset from pointer into which data is copied * @param buf *doublearray from which to copy * @param index * array index from which to start copying * @param length * number of elements frombufthat must be copied */ public native void copyIn(int bOff, double[] buf, int index, int length); /** * Indirect the C pointer, copying from memory pointed to by C * pointer, into the specified array. * * @param bOff * byte offset from pointer into which data is copied * @param buf *bytearray into which data is copied * @param index * array index from which to start copying * @param length * number of elements from C pointer that must be copied */ public native void copyOut(int bOff, byte[] buf, int index, int length); /** * Indirect the C pointer, copying from memory pointed to by C * pointer, into the specified array. * * @param bOff * byte offset from pointer from which data is copied * @param buf *shortarray into which data is copied * @param index * array index to which data is copied * @param length * number of elements from C pointer that must be copied */ public native void copyOut(int bOff, short[] buf, int index, int length); /** * Indirect the C pointer, copying from memory pointed to by C * pointer, into the specified array. * * @param bOff * byte offset from pointer from which data is copied * @param buf *chararray into which data is copied * @param index * array index to which data is copied * @param length * number of elements from C pointer that must be copied */ public native void copyOut(int bOff, char[] buf, int index, int length); /** * Indirect the C pointer, copying from memory pointed to by C * pointer, into the specified array. * * @param bOff * byte offset from pointer from which data is copied * @param buf *intarray into which data is copied * @param index * array index to which data is copied * @param length * number of elements from C pointer that must be copied */ public native void copyOut(int bOff, int[] buf, int index, int length); /** * Indirect the C pointer, copying from memory pointed to by C * pointer, into the specified array. * * @param bOff * byte offset from pointer from which data is copied * @param buf *longarray into which data is copied * @param index * array index to which data is copied * @param length * number of elements from C pointer that must be copied */ public native void copyOut(int bOff, long[] buf, int index, int length); /** * Indirect the C pointer, copying from memory pointed to by C * pointer, into the specified array. * * @param bOff * byte offset from pointer from which data is copied * @param buf *floatarray into which data is copied * @param index * array index to which data is copied * @param length * number of elements from C pointer that must be copied */ public native void copyOut(int bOff, float[] buf, int index, int length); /** * Indirect the C pointer, copying from memory pointed to by C * pointer, into the specified array. * * @param bOff * byte offset from pointer from which data is copied * @param buf *doublearray into which data is copied * @param index * array index to which data is copied * @param length * number of elements from C pointer that must be copied */ public native void copyOut(int bOff, double[] buf, int index, int length); /** * Indirect the C pointer as a pointer tobyte. This is * equivalent to the expression **((jbyte *)((char *)cptr + * offset)). * * @param offset * offset from pointer to perform the indirection * @return thebytevalue being pointed to */ public native byte getByte(int offset); /** * Indirect the C pointer as a pointer toshort. This is * equivalent to the expression **((jshort *)((char *)cptr + offset)). * * @param offset * byte offset from pointer to perform the indirection * @return theshortvalue being pointed to */ public native short getShort(int offset); /** * Indirect the C pointer as a pointer toint. This is * equivalent to the expression **((jint *)((char *)cptr + offset)). * * @param offset * byte offset from pointer to perform the indirection * @return theintvalue being pointed to */ public native int getInt(int offset); /** * Indirect the C pointer as a pointer tolong. This is * equivalent to the expression **((jlong *)((char *)cptr + offset)). * * @param offset * byte offset from pointer to perform the indirection * @return thelongvalue being pointed to */ public native long getLong(int offset); /** * Indirect the C pointer as a pointer tofloat. This is * equivalent to the expression **((jfloat *)((char *)cptr + offset)). * * @param offset * byte offset from pointer to perform the indirection * @return thefloatvalue being pointed to */ public native float getFloat(int offset); /** * Indirect the C pointer as a pointer todouble. This is * equivalent to the expression **((jdouble *)((char *)cptr + offset)). * * @param offset * byte offset from pointer to perform the indirection * @return thedoublevalue being pointed to */ public native double getDouble(int offset); /** * Indirect the C pointer as a pointer to pointer. This is equivalent to the * expression*((void **)((char *)cptr + offset)). * * @param offset * byte offset from pointer to perform the indirection * @return thepointervalue being pointed to */ public native CPtr getCPtr(int offset); /** * Indirect the C pointer as a pointer tochar *, a *NULL-terminated C string. Convert the C string to a *String. * * @param offset * byte offset from pointer to obtain the C string * @return theStringvalue being pointed to */ public native String getString(int offset); /** * Setvalueat location being pointed to. This is equivalent * to the expression **((jbyte *)((char *)cptr + offset)) = value. * * @param offset * byte offset from pointer at whichvaluemust be * set * @param value *bytevalue to set */ public native void setByte(int offset, byte value); /** * Setvalueat location being pointed to. This is equivalent * to the expression **((jshort *)((char *)cptr + offset)) = value. * * @param offset * byte offset from pointer at whichvaluemust be * set * @param value *shortvalue to set */ public native void setShort(int offset, short value); /** * Setvalueat location being pointed to. This is equivalent * to the expression*((jint *)((char *)cptr + offset)) = value. * * @param offset * byte offset from pointer at whichvaluemust be * set * @param value *intvalue to set */ public native void setInt(int offset, int value); /** * Setvalueat location being pointed to. This is equivalent * to the expression **((jlong *)((char *)cptr + offset)) = value. * * @param offset * byte offset from pointer at whichvaluemust be * set * @param value *longvalue to set */ public native void setLong(int offset, long value); /** * Setvalueat location being pointed to. This is equivalent * to the expression **((jfloat *)((char *)cptr + offset)) = value. * * @param offset * byte offset from pointer at whichvaluemust be * set * @param value *floatvalue to set */ public native void setFloat(int offset, float value); /** * Setvalueat location being pointed to. This is equivalent * to the expression **((jdouble *)((char *)cptr + offset)) = value. * * @param offset * byte offset from pointer at whichvaluemust be * set * @param value *doublevalue to set */ public native void setDouble(int offset, double value); /** * Setvalueat location being pointed to. This is equivalent * to the expression *((void **)((char *)cptr + offset)) = valuevaluemust be * set * @param value *CPtrvalue to set */ public native void setCPtr(int offset, CPtr value); /** * Copy stringvalueto the location being pointed to. Copy * each element invalue, converted to native encoding, at * anoffsetfrom the location pointed to by this pointer. * * @param offset * byte offset from pointer at which characters in *valuemust be set * @param value *Stringvalue to set */ public native void setString(int offset, String value); /* Initialize field and method IDs for native methods of this class. */ private static native int initIDs(CPtr p); static { String osName = System.getProperty("os.name").toUpperCase(); if (osName.indexOf("LINUX") > -1) System.loadLibrary("disp_linux_i386"); else if (osName.indexOf("WIN") > -1) System.loadLibrary("disp_win_i386"); else throw new RuntimeException("Operating system \"" + osName + "\" not supported for direct DLL calls."); NULL = new CPtr(); SIZE = initIDs(NULL); } /* * peer and the no-args constructor are deliberately package private, so all * classes that are abstractions of a C pointer will can only be in one * isolated package. */ /* Pointer value of the real C pointer. Use long to be 64-bit safe. */ long peer; /* No-args constructor, see comment above. */ CPtr() { } }
Ads help maintain this website.