-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnsigned.java
More file actions
57 lines (50 loc) · 1.63 KB
/
Copy pathUnsigned.java
File metadata and controls
57 lines (50 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package model;
import java.io.IOException;
import java.io.InputStream;
public abstract class Unsigned {
// 无符号数或表的字节数组
byte[] bytes;
public byte[] getBytes() {
return bytes;
}
/**
* 派生类需要重写一个方法,然后实现字节数组bytes的new动作
* @return
*/
abstract protected void newBytes();
/**
* 通过 inputStream 读取 bytes数组长度 的字节 到 bytes数组中,并用 Big-Endian 顺序计算整型值
* @param is
* @param bytes
* @return
* @throws IOException
*/
protected long read(InputStream is, byte[] bytes) throws IOException {
if (is == null)
throw new NullPointerException("InputStream is null");
int len = is.read(bytes);
if (len == -1 || len != bytes.length)
throw new RuntimeException("len " + len + " is not equal bytes.length " + bytes.length);
long num = 0;
for (int i = 0; i < bytes.length; i++) {
num <<= 8;
num |= bytes[i] & 0xff;
}
return num;
}
/**
* 用 Big-Endian 顺序将字节数组转为字符串(十六进制表达式)
* @return
*/
public String parseBytesToHexString() {
StringBuilder sb = new StringBuilder();
if (bytes == null)
throw new RuntimeException("bytes is null");
sb.append("0x");
for (int i = 0; i < bytes.length; i ++) {
sb.append(Character.forDigit((bytes[i] & 0xff) / 16, 16));
sb.append(Character.forDigit((bytes[i] & 0xff) % 16, 16));
}
return sb.toString();
}
}