JNA结构数据是垃圾值

时间:2014-03-11 04:16:07

标签: java struct jna

######### interface.c文件
 typedef struct{
    int col_size;
    char* data;
 }J_ROW_DATA;
 typedef struct{
    int col_cnt;
    J_ROW_DATA* col;
 }J_ROW;
// Struct 

void jreplace(J_ROW* row)
{
    R2WR_ROW src;  // our struct 
    R2WR_ROW dst;
    int col_cnt = row->col_cnt;
    int rv,j,i;
    memset( &src, 0x00, sizeof(R2WR_ROW) );
    for( j = 0; j < col_cnt; j++ ) {
            src.col = (R2WR_COL*)realloc( src.col, sizeof(R2WR_COL)*(j+1) );
    if( !src.col ) {
                    printf("Memory allocation failed.<%d>\n", j);
        break;
    }
    memset( &(src.col[j]), 0x00, sizeof(R2WR_COL) );
         ///// .......
        ///// row->col[x].data setting




  }

// interface.c file

###### JNA接口java
  public static class J_ROW_DATA extends Structure{
    public static class ByReference extends J_ROW_DATA implements   Structure.ByReference{}
    public int col_size = 0;
    public Pointer data;

    public List getFieldOrder()
    {
        String[] s = new String[] { "col_size" ,"data" };
        return Arrays.asList(s);
    }

}

public static class J_ROW extends Structure
{
    public static class ByReference extends J_ROW implements
            Structure.ByReference{}
    public static class ByValue extends J_ROW implements
    Structure.ByValue{}  
    public int col_cnt = 0;
    public J_ROW_DATA.ByReference col;
    @Override
    public List getFieldOrder()
    {
        String[] s = new String[] { "col_cnt", "col" };
        return Arrays.asList(s);
    }
}

public void r2wr_place(J_ROW.ByReference row);

//这是declar Structure ;;

    /* other source */
    ArrayList<Object> transArray = null;
    CLibrary.J_ROW.ByReference data = new CLibrary.J_ROW.ByReference();
    data.col_cnt = colarray.size();
    data.col = new CLibrary.J_ROW_DATA.ByReference();
    CLibrary.J_ROW_DATA[] cols = (CLibrary.J_ROW_DATA[])data.col.toArray(data.col_cnt);
    for(int i = 0; i < data.col_cnt; i++)
    {
        byte[] tempbyte= null;
        if(colarray.get(i) == null){
            tempbyte = new byte[0];
            cols[i].col_size = 0;
        }
        else{
            tempbyte = colarray.get(i);
            cols[i].col_size = tempbyte.length;
        }
        ByteBuffer buf = ByteBuffer.allocateDirect(tempbyte.length);
        buf.put(tempbyte);
        buf.rewind();
        cols[i].data = Native.getDirectBufferPointer(buf);    

    }  // this is Java to C pass Structure member
     CLibrary.INSTANCE.jreplace(data); // C Library call 
     transArray = getObjList(data ,md);  // byte to Object

/////////////////////////////////////////////// ////////////////////////////////////

  private ArrayList<Object> getObjList(CLibrary.J_ROW.ByReference row, ResultSetMetaDatamd)throws SQLException
{
    ArrayList<Object> tmp = new ArrayList<Object>();
    int size = row.col_cnt;   
    CLibrary.J_ROW_DATA[] cols = (CLibrary.J_ROW_DATA[])row.col.toArray(row.col_cnt);
    for(int j = 0; j < size; j++)
    {
        byte[] column = cols[j].data.getByteArray(0, cols[j].col_size);
        // Problem  
        // column value is not expected  
        // for example, 1.  printf value in interface.c file is  [223] , but  get value after getByteArray is []  and value size is 3 
        // 2. interface.c file printf value is [3232] , but column value is [8*5?] 

并非总是会发生,有时候确实会获得价值

测试数据计数为10000000 它发生随机重复计数...... 我该怎么办?

0 个答案:

没有答案