diff --git a/ruoyi-code/src/main/java/sdk/java/lib/DataUtils.java b/ruoyi-code/src/main/java/sdk/java/lib/DataUtils.java new file mode 100644 index 0000000..1e85dd5 --- /dev/null +++ b/ruoyi-code/src/main/java/sdk/java/lib/DataUtils.java @@ -0,0 +1,459 @@ +package sdk.java.lib; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Array; +import com.sun.jna.Memory; +import com.sun.jna.Pointer; +import com.sun.jna.Structure; + + +// 数据类型操作类 +public class DataUtils +{ + ///////////////////////////////////////////////////////////////////////////////////////////////// + + // 本地化代码数据指针转换为 JAVA 数据结构 + public static void sdk_data_ptrToStructure(Pointer ptr, Structure structure) + { + sdk_data_ptrToStructure(ptr, 0, structure); + } + + // 本地化代码数据指针转换为 JAVA 数据结构 + public static void sdk_data_ptrToStructure(Pointer ptr, long offset, Structure structure) + { + structure.write(); + Pointer ptrMemory = structure.getPointer(); + ptrMemory.write(0, ptr.getByteArray(offset, structure.size()), 0, structure.size()); + structure.read(); + } + + // 本地化代码数据指针转换为 JAVA 数据结构数组 + public static void sdk_data_ptrToStructureArray(Pointer ptr, Structure[] array) + { + long offset = 0; + for (int i = 0; i < array.length; i++) + { + sdk_data_ptrToStructure(ptr, offset, array[i]); + offset += array[i].size(); + } + } + + // JAVA 数据结构转换为本地化代码数据结构 + public static void sdk_data_structureToPtr(Structure structure, Pointer ptr, long offset) + { + structure.write(); + Pointer ptrMemory = structure.getPointer(); + ptr.write(offset, ptrMemory.getByteArray(0, structure.size()), 0, structure.size()); + } + + // JAVA 数据结构数组转换为本地化代码数据结构数组 + public static void sdk_data_structureArrayToPtr(Structure[] array, Pointer ptr) + { + long offset = 0; + for (int i = 0; i < array.length; i++) + { + sdk_data_structureToPtr(array[i], ptr, offset); + offset += array[i].size(); + } + } + + ///////////////////////////////////////////////////////////////////////////////////////////////// + // 泛型类型数组 + public static class GenericTypeArray + { + private T[] array; + + @SuppressWarnings("unchecked") + public GenericTypeArray(Class type, int sz) { + array = (T[]) Array.newInstance(type, sz); + } + + public void put(int index, T item) { + array[index] = item; + } + + public T get(int index) { + return array[index]; + } + + public T[] get() { + return array; + } + } + + ///////////////////////////////////////////////////////////////////////////////////////////////// + // 创建Pointer + public static Pointer sdk_data_createPtr(int length) + { + if(0 >= length) + { + return null; + } + + Pointer ptr = new Memory(length); + + for(int i=0; i= length) + { + return; + } + + for(int i=0; i offset || 0 >= length) + { + return null; + } + + byte[] array = new byte[length]; + for(int i = 0; i < length; i++) + { + Array.setByte(array, i, (byte) 0); + } + + ptr.read(offset, array, 0, length); + return array; + } + + // byte 数组转换为 Pointer + public static Pointer sdk_data_byteArrayToPtr(byte[] array) + { + if(null == array || 0 == array.length) + return null; + + Pointer ptr = new Memory(array.length); + ptr.write(0, array, 0, array.length); + return ptr; + } + + // byte 数组转换为字符串,charset 为字符集名称,如 GBK,UTF-8 + public static String sdk_data_byteArrayToString(byte[] array, String charset) + { + if(null == array || 0 == array.length) + return null; + + String encoding = (null == charset || charset.isEmpty()) ? "UTF-8" : charset; + String dst = null; + try + { + dst = new String(array, encoding).trim(); + } + catch(Exception ex) + { + dst = null; + } + + return dst; + } + + // 字符串转换为 byte 数组,charset 为字符集名称,如 GBK,UTF-8 + public static byte[] sdk_data_stringToByteArray(String text, String charset) + { + if(null == text || text.isEmpty()) + return null; + + String encoding = (null == charset || charset.isEmpty()) ? "UTF-8" : charset; + byte[] array = null; + try + { + array = text.getBytes(encoding); + } + catch (UnsupportedEncodingException e) + { + e.printStackTrace(); + return null; + } + return array; + } + + // 字符串数据拷贝到 byte 数组,charset 为字符集名称,如 GBK,UTF-8 + public static boolean sdk_data_copyStringToByteArray(byte[] dst, String src, int nOffset, String charset) + { + if(null == src || src.isEmpty() || null == dst || 0 >= dst.length) + return false; + + String encoding = (null == charset || charset.isEmpty()) ? "UTF-8" : charset; + byte[] array = null; + try + { + array = src.getBytes(encoding); + } + catch (UnsupportedEncodingException e) + { + e.printStackTrace(); + return false; + } + + for(int i=0; i