Files

71 lines
2.0 KiB
Java
Raw Permalink Normal View History

2020-08-27 12:39:39 +08:00
package com.proxgrind.chameleon.utils.system;
/**
* @author DXL
*/
public class SystemUtils {
/**
* 判断当前是否超时
*
* @param startTimeStamp 开始时间(时间戳形式)
* @param timeoutms 超时值,当最新的时间超过了这个值时将会被判断为超时!
* @return true 超时,
*/
public static boolean isTimeout(long startTimeStamp, long timeoutms) {
return System.currentTimeMillis() - startTimeStamp >= timeoutms;
}
// 简化休眠
public static void sleep(long ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* Calculates the checksum of the passed byte buffer.
*
* @param buffer
* @return byte checksum value
*/
public static byte calcChecksum(byte[] buffer, boolean sub) {
if (buffer == null) return 0;
byte checksum = 0;
int bufPos = 0;
int byteCount = buffer.length;
while (byteCount-- != 0) {
byte b = buffer[bufPos++];
if (!sub)
checksum += b;
else
checksum -= b;
}
return checksum;
}
/**
* Calculates the checksum of the passed byte buffer.
*
* @param buffer
* @return byte checksum value
*/
public static byte calcChecksub(byte checkSum, byte[] buffer, boolean sub) {
if (buffer == null) return 0;
byte checksum = checkSum;
int bufPos = 0;
int byteCount = buffer.length;
while (byteCount-- != 0) {
byte b = buffer[bufPos++];
if (!sub)
checksum += b;
else
checksum -= b;
//System.out.println("value: " + HexUtil.toHexString(tmp) + "," + HexUtil.toHexString(b) + " checksum: " + HexUtil.toHexString(checksum));
}
return checksum;
}
}