mirror of
https://gitee.com/BDWare/cm
synced 2025-04-27 14:32:16 +00:00
54 lines
1.1 KiB
Java
54 lines
1.1 KiB
Java
package org.bdware.sc.units;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
public class ByteUtil {
|
|
// public static byte[] readRest(ByteArrayInputStream bi) {
|
|
// byte[] ret = new byte[bi.available()];
|
|
// bi.read(ret, 0, ret.length);
|
|
// return ret;
|
|
// }
|
|
|
|
public static int readInt(ByteArrayInputStream bi) {
|
|
int ret = 0;
|
|
ret |= (bi.read() & 0xff);
|
|
ret <<= 8;
|
|
ret |= (bi.read() & 0xff);
|
|
ret <<= 8;
|
|
ret |= (bi.read() & 0xff);
|
|
ret <<= 8;
|
|
ret |= (bi.read() & 0xff);
|
|
return ret;
|
|
}
|
|
|
|
public static void writeInt(ByteArrayOutputStream bo, int i) {
|
|
bo.write((i >> 24) & 0xff);
|
|
bo.write((i >> 16) & 0xff);
|
|
bo.write((i >> 8) & 0xff);
|
|
bo.write(i & 0xff);
|
|
|
|
}
|
|
|
|
public static void writeLong(ByteArrayOutputStream bo, long l) {
|
|
for (int i = 56; i >= 0; i -= 8) {
|
|
bo.write(((int) (l >> i)) & 0xff);
|
|
}
|
|
}
|
|
|
|
public static long readLong(ByteArrayInputStream bi) {
|
|
long ret = 0L;
|
|
for (int i = 0; i < 8; i++) {
|
|
ret <<= 8;
|
|
ret |= (bi.read() & 0xff);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
public static byte[] readBytes(ByteArrayInputStream bi, int len) {
|
|
byte[] ret = new byte[len];
|
|
bi.read(ret, 0, len);
|
|
return ret;
|
|
}
|
|
}
|