mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
将VMSConnection模块加入min-dev-java,以支持与VMS进行通信,执行注册、登录等功能
This commit is contained in:
1 parent
a26f908470
commit
ba105bdc3c
36 files changed
+2961
-2
No files matched your search
@@ -0,0 +1,48 @@
|
||||
package VMSConnection.Utils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 22:20 2021/6/22
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class Base64Helper {
|
||||
/**
|
||||
* 与vpn-management适配的base64编码
|
||||
* @param src
|
||||
* @return
|
||||
*/
|
||||
public static String BlockChain_Base64Encode(byte[] src){
|
||||
Base64.Encoder encoder = Base64.getUrlEncoder();
|
||||
encoder = encoder.withoutPadding();
|
||||
|
||||
return encoder.encodeToString(src);
|
||||
}
|
||||
public static String BlockChain_Base64Encode(String src){
|
||||
return BlockChain_Base64Encode(src.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/**
|
||||
* 与vpn-management适配的base64解码
|
||||
* @param src
|
||||
* @return
|
||||
*/
|
||||
public static byte[] BlockChain_Base64Decode(String src) {
|
||||
Base64.Decoder decoder = Base64.getUrlDecoder();
|
||||
return decoder.decode(src.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/**
|
||||
* 参考链接: https://blog.csdn.net/qq_36928715/article/details/110489464
|
||||
* golang的Unmarshal函数,自动把结构体中的base64转成了byte[],这里填补java的此功能
|
||||
* @return
|
||||
*/
|
||||
public static byte[] BlockChain_Base64Decode_GoLanUn(String src){
|
||||
edu.pku.commons.codec.binary.Base64 decoder = new edu.pku.commons.codec.binary.Base64();
|
||||
return decoder.decode(src);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,398 @@
|
||||
package VMSConnection.Utils;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 21:03 2021/6/30
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class ByteHelper {
|
||||
// 小头字节序的Int转byte[]
|
||||
public static byte[] int32ToByteArray(int int32) {
|
||||
// return (byte[])int32;
|
||||
byte[] result = new byte[4];
|
||||
|
||||
for(int i = 0; i < 4; ++i) {
|
||||
int offset = (result.length - 1 - i) * 8;
|
||||
result[result.length - 1 - i] = (byte)((int32 >>> offset & 0xff));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// 参考链接:https://blog.csdn.net/jiangxinyu/article/details/8165064
|
||||
// 小头字节序的byte[]转Int
|
||||
public static int byteArrayToInt32(byte[] int32) {
|
||||
if (int32.length != 4) {
|
||||
return Integer.parseInt(null);
|
||||
} else {
|
||||
int s = 0;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (int32[3-i] >= 0) {
|
||||
s = s + int32[3-i];
|
||||
} else {
|
||||
s = s + 256 + int32[3-i];
|
||||
}
|
||||
s = s * 256;
|
||||
}
|
||||
if (int32[0] >= 0) {
|
||||
s = s + int32[0];
|
||||
} else {
|
||||
s = s + 256 + int32[0];
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取从start到末尾的字节数组
|
||||
* @param buffer
|
||||
* @param startIndex
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getTailBytes(byte[] buffer,int startIndex) {
|
||||
if(startIndex >= buffer.length || startIndex < 0)
|
||||
return null;
|
||||
int len=buffer.length-startIndex;
|
||||
byte[] newBytes=new byte[len];
|
||||
System.arraycopy(buffer,startIndex,newBytes,0,len);
|
||||
return newBytes;
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 从byte[]数组指定开始位置获取第一个字节
|
||||
// *
|
||||
// * @param buffer
|
||||
// * @param start
|
||||
//// * @return
|
||||
//// */
|
||||
// public static byte getFirstByte(byte[] buffer, VlInt start) throws ByteHelperException {
|
||||
// int startInt= 0;
|
||||
// try {
|
||||
// startInt = start.getVlIntValue2Int();
|
||||
// } catch (VlIntException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// if(startInt >= buffer.length || startInt < 0){
|
||||
// throw new ByteHelperException("buffer out of bounds");
|
||||
// }
|
||||
// return buffer[startInt];
|
||||
// // byte[] firstbyte=new byte[1];
|
||||
// // System.arraycopy(buffer,startInt,firstbyte,0,firstbyte.length);
|
||||
// // return firstbyte[0];
|
||||
// }
|
||||
public static byte getFirstByte(byte[] buffer, int startInt) throws Exception {
|
||||
if(startInt >= buffer.length || startInt < 0){
|
||||
throw new Exception("buffer out of bounds");
|
||||
}
|
||||
return buffer[startInt];
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 从byte[]数组指定开始位置获取长度为len的字节数组
|
||||
// * todo: start位置可能是大于int类型的数值
|
||||
// * @param buffer
|
||||
// * @param start
|
||||
// * @return
|
||||
// */
|
||||
// public static byte[] getLenBytes(byte[] buffer, VlInt start,VlInt len) throws ByteHelperException {
|
||||
// int startInt= -1;
|
||||
// int lenInt = -1;
|
||||
// try {
|
||||
// startInt = start.getVlIntValue2Int();
|
||||
// lenInt=len.getVlIntValue2Int();
|
||||
// } catch (VlIntException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// if(startInt >= buffer.length || startInt < 0){
|
||||
// throw new ByteHelperException("buffer out of bounds");
|
||||
// }
|
||||
// if(lenInt < 0){
|
||||
// throw new ByteHelperException("len should > 0");
|
||||
// }
|
||||
// byte[] lenbytes=new byte[lenInt];
|
||||
//// byte[] lenbytes=new byte[1];
|
||||
// System.arraycopy(buffer,startInt,lenbytes,0,lenInt);
|
||||
// return lenbytes;
|
||||
// }
|
||||
// public static byte[] getLenBytes(byte[] buffer, VlInt start,int len){
|
||||
// int startInt= -1;
|
||||
// try {
|
||||
// startInt = start.getVlIntValue2Int();
|
||||
// } catch (VlIntException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// byte[] lenbytes=new byte[len];
|
||||
//// byte[] lenbytes=new byte[1];
|
||||
// System.arraycopy(buffer,startInt,lenbytes,0,len);
|
||||
// return lenbytes;
|
||||
// }
|
||||
public static byte[] getLenBytes(byte[] buffer, int startInt,int len){
|
||||
byte[] lenbytes=new byte[len];
|
||||
System.arraycopy(buffer,startInt,lenbytes,0,len);
|
||||
return lenbytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 无符号int转byte[]
|
||||
* @param i_int
|
||||
* @return
|
||||
*/
|
||||
private static byte[] intToByteArray(int i_int) {
|
||||
byte[] result = new byte[4];
|
||||
//由高位到低位
|
||||
for(int i=0;i<4;i++) {
|
||||
int offset=(result.length-i-1)*8;
|
||||
result[i] = (byte) ((i_int >>>offset) & 0xff);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 无符号byte[]转int
|
||||
* @param bytes
|
||||
* @return
|
||||
*/
|
||||
private static int byteArrayToInt(byte[] bytes) {
|
||||
if(bytes.length!=4){
|
||||
return Integer.parseInt(null);
|
||||
}
|
||||
int value= 0;
|
||||
//由高位到低位
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int offset= (bytes.length - 1 - i) * 8;
|
||||
value +=(bytes[i] & 0x000000ff) << offset; //往高位游
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 无符号long转byte[]
|
||||
* @param l_long
|
||||
* @return
|
||||
*/
|
||||
private static byte[] longToByteArray(long l_long) {
|
||||
byte[] result = new byte[8];
|
||||
for (int i = 0; i<8; i++) {
|
||||
int offset = (result.length - 1 - i) * 8;
|
||||
result[i] = (byte) ((l_long >>> offset) & 0xff);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 无符号byte[]转long
|
||||
* @param bytes
|
||||
* @return
|
||||
*/
|
||||
private static long byteArrayToLong(byte[] bytes) {
|
||||
if(bytes.length!=8){
|
||||
return Integer.parseInt(null);
|
||||
}
|
||||
long value= 0;
|
||||
//由高位到低位
|
||||
for (int i = 0; i < 8; i++) {
|
||||
int offset= (bytes.length - 1 - i) * 8;
|
||||
value +=((long)bytes[i] & 0xff) << offset; //往高位游
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 无符号字节转int
|
||||
* @param b
|
||||
* @return
|
||||
*/
|
||||
// private static int unsignedbyte2int(byte b){
|
||||
// return (int) b&0xff;
|
||||
// }
|
||||
|
||||
/**
|
||||
* uint8转int
|
||||
* @param uint8
|
||||
* @return
|
||||
*/
|
||||
public static int uint8ToInt(byte uint8){
|
||||
return uint8 & 0xff;
|
||||
}
|
||||
|
||||
/**
|
||||
* 一个uint8转为的int,通过本函数再次转为byte
|
||||
* @param uint8
|
||||
* @return
|
||||
*/
|
||||
public static byte uint8ToByte(int uint8){
|
||||
return (byte)(uint8 & 0xff);
|
||||
}
|
||||
|
||||
/**
|
||||
* uint8转long
|
||||
* @param uint8
|
||||
* @return
|
||||
*/
|
||||
public static long uint8ToLong(byte uint8){
|
||||
return uint8ToInt(uint8);
|
||||
}
|
||||
|
||||
/**
|
||||
* uint16转int
|
||||
* @param uint16
|
||||
* @return
|
||||
*/
|
||||
public static int uint16ToInt(byte[] uint16){
|
||||
if(uint16.length!=2){
|
||||
return Integer.parseInt(null);
|
||||
}
|
||||
byte[] bytes=new byte[4];
|
||||
for(int i=0;i<2;i++) {
|
||||
bytes[i] = 0x00;
|
||||
}
|
||||
System.arraycopy(uint16,0,bytes,2,uint16.length);
|
||||
return byteArrayToInt(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 一个uint16转为的int,通过本函数再次转为byte[]
|
||||
* @param uint16
|
||||
* @return
|
||||
*/
|
||||
public static byte[] uint16ToByteArray(int uint16){
|
||||
byte[] result = new byte[2];
|
||||
for (int i = 0; i<2; i++) {
|
||||
int offset = (result.length - 1 - i) * 8;
|
||||
result[i] = (byte) ((uint16 >>> offset) & 0xff);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* uint16转long
|
||||
* @param uint16
|
||||
* @return
|
||||
*/
|
||||
public static long uint16ToLong(byte[] uint16){
|
||||
return uint16ToInt(uint16);
|
||||
}
|
||||
|
||||
/**
|
||||
* uint32转long
|
||||
* @param uint32
|
||||
* @return
|
||||
*/
|
||||
public static long uint32ToLong(byte[] uint32){
|
||||
if(uint32.length!=4){
|
||||
return Long.parseLong(null);
|
||||
}
|
||||
byte[] bytes=new byte[8];
|
||||
for(int i=0;i<4;i++) {
|
||||
bytes[i] = 0x00;
|
||||
}
|
||||
System.arraycopy(uint32,0,bytes,4,uint32.length);
|
||||
// System.out.println(Arrays.toString(uint32) + "uint32ToLong: " + Arrays.toString(bytes));
|
||||
return byteArrayToLong(bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 一个uint32转为的long,通过本函数再次转为byte[]
|
||||
* @param uint32
|
||||
* @return
|
||||
*/
|
||||
public static byte[] uint32ToByteArray(long uint32){
|
||||
byte[] result = new byte[4];
|
||||
for (int i = 0; i<4; i++) {
|
||||
int offset = (result.length - 1 - i) * 8;
|
||||
result[i] = (byte) ((uint32 >>> offset) & 0xff);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* uint64转long
|
||||
* todo: 理论上可能发生越界
|
||||
* @param uint64
|
||||
* @return
|
||||
*/
|
||||
public static long uint64ToLong(byte[] uint64){
|
||||
if(uint64.length!=8){
|
||||
return Long.parseLong(null);
|
||||
}
|
||||
return byteArrayToLong(uint64);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 一个uint64转为的long,通过本函数再次转为byte[]
|
||||
* todo: 理论上可能发生越界
|
||||
* @param uint64
|
||||
* @return
|
||||
*/
|
||||
public static byte[] uint64ToByteArray(long uint64){
|
||||
byte[] result = new byte[8];
|
||||
for (int i = 0; i<8; i++) {
|
||||
int offset = (result.length - 1 - i) * 8;
|
||||
result[i] = (byte) ((uint64 >>> offset) & 0xff);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// short整型转为byte类型的数组
|
||||
// private static byte[] unsigned_short_2byte(int length) {
|
||||
// byte[] targets = new byte[2];
|
||||
// for (int i = 0; i < 2; i++) {
|
||||
// int offset = (targets.length - 1 - i) * 8;
|
||||
// targets[i] = (byte) ((length >>> offset) & 0xff);
|
||||
// }
|
||||
// return targets;
|
||||
// }
|
||||
|
||||
//测试:对于uint8,应当测试1 127 128
|
||||
public static void main(String[] args){
|
||||
|
||||
// byte[] testBytes={0x01,0x02};
|
||||
// for(int i=0;i<2;i++) {
|
||||
// System.out.println(testBytes[i]&0xFF);
|
||||
//// System.out.println(Integer.toHexString(testBytes[i]&0xFF));
|
||||
// }
|
||||
byte b=-1;
|
||||
System.out.println(b&0xff);
|
||||
}
|
||||
|
||||
// 便于测试,新添加两个public方法:
|
||||
// 无符号byte[]转int
|
||||
// 传入的是长度为2或4的byte[]
|
||||
public static int new_byteArrayToInt(byte[] bytes){
|
||||
int value= 0;
|
||||
//由高位到低位
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
int offset= (bytes.length - 1 - i) * 8;
|
||||
value +=(bytes[i] & 0x000000ff) << offset; //往高位游
|
||||
}
|
||||
return value;
|
||||
}
|
||||
// 便于测试,新添加两个public方法:
|
||||
// 无符号byte[]转int(溢出部分)
|
||||
// 传入的是长度为4的byte[]
|
||||
public static long new_byteArrayToAllInt(byte[] bytes){
|
||||
int v=new_byteArrayToInt(bytes);
|
||||
if(v<0){
|
||||
return (long) (v+Math.pow(2,32));
|
||||
}else{
|
||||
return v;
|
||||
}
|
||||
}
|
||||
// 无符号byte[]转long
|
||||
// 传入的是长度为8的byte[]
|
||||
public static long new_byteArrayToLong(byte[] bytes){
|
||||
long value= 0;
|
||||
//由高位到低位
|
||||
for (int i = 0; i < 8; i++) {
|
||||
int offset= (8 - 1 - i) * 8;
|
||||
value +=((long)bytes[i] & 0xff) << offset; //往高位游
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user