mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
修复bytehelper & VlInt中的已知bug
This commit is contained in:
1 parent
227d10a391
commit
2ad446ca2c
29 files changed
+315
-80
No files matched your search
@@ -2,6 +2,10 @@ package util;
|
||||
|
||||
|
||||
import encoding.VlInt;
|
||||
import encoding.VlIntException;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Arrays;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
@@ -18,7 +22,9 @@ public class ByteHelper {
|
||||
* @param startIndex
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getTailBytes(byte[] buffer,int startIndex){
|
||||
public static byte[] getTailBytes(byte[] buffer,int startIndex) throws ByteHelperException {
|
||||
if(startIndex >= buffer.length || startIndex < 0)
|
||||
throw new ByteHelperException("buffer out of bounds");
|
||||
int len=buffer.length-startIndex;
|
||||
byte[] newBytes=new byte[len];
|
||||
System.arraycopy(buffer,startIndex,newBytes,0,len);
|
||||
@@ -27,16 +33,25 @@ public class ByteHelper {
|
||||
|
||||
/**
|
||||
* 从byte[]数组指定开始位置获取第一个字节
|
||||
* todo: start位置可能是大于int类型的数值
|
||||
*
|
||||
* @param buffer
|
||||
* @param start
|
||||
* @return
|
||||
*/
|
||||
public static byte getFirstByte(byte[] buffer, VlInt start){
|
||||
int startInt=start.getVlIntValue2Int();
|
||||
byte[] firstbyte=new byte[1];
|
||||
System.arraycopy(buffer,startInt,firstbyte,0,firstbyte.length);
|
||||
return firstbyte[0];
|
||||
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];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,16 +61,33 @@ public class ByteHelper {
|
||||
* @param start
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getLenBytes(byte[] buffer, VlInt start,VlInt len){
|
||||
int startInt=start.getVlIntValue2Int();
|
||||
int lenInt=len.getVlIntValue2Int();
|
||||
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=start.getVlIntValue2Int();
|
||||
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);
|
||||
@@ -118,11 +150,11 @@ public class ByteHelper {
|
||||
if(bytes.length!=8){
|
||||
return Integer.parseInt(null);
|
||||
}
|
||||
int value= 0;
|
||||
long value= 0;
|
||||
//由高位到低位
|
||||
for (int i = 0; i < 8; i++) {
|
||||
int offset= (bytes.length - 1 - i) * 8;
|
||||
value +=(bytes[i] & 0x000000000000ff) << offset; //往高位游
|
||||
value +=((long)bytes[i] & 0xff) << offset; //往高位游
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -217,6 +249,7 @@ public class ByteHelper {
|
||||
bytes[i] = 0x00;
|
||||
}
|
||||
System.arraycopy(uint32,0,bytes,4,uint32.length);
|
||||
// System.out.println(Arrays.toString(uint32) + "uint32ToLong: " + Arrays.toString(bytes));
|
||||
return byteArrayToLong(bytes);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package util;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:38 2021/03/15
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class ByteHelperException extends Exception{
|
||||
public ByteHelperException(String msg){
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user