-
Notifications
You must be signed in to change notification settings - Fork 1
/
3.java
98 lines (82 loc) · 2.8 KB
/
3.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
public class BinaryHelper {
private ByteBuffer buffer;
public BinaryHelper(int initialSize) {
buffer = ByteBuffer.allocate(initialSize);
buffer.order(ByteOrder.LITTLE_ENDIAN); // 设置为小端模式,根据实际需求调整
}
public byte readByte(int index) {
checkIndex(index);
return buffer.get(index);
}
public void writeByte(int index, byte value) {
ensureCapacity(index + 1);
buffer.put(index, value);
}
public void writeBytes(byte[] data) {
ensureCapacity(buffer.position() + data.length);
buffer.put(data);
}
// 用于位操作的辅助方法
public static boolean getBit(byte b, int position) {
return ((b >> position) & 1) == 1;
}
public boolean getBit(int index, int position) {
checkIndex(index);
byte b = buffer.get(index);
return getBit(b, position);
}
public static byte setBit(byte b, int position, boolean value) {
if (position < 0 || position > 7) {
throw new IllegalArgumentException("Invalid bit position");
}
if (value) {
// set bit
return (byte) (b | (1 << position));
} else {
// unset bit
return (byte) (b & ~(1 << position));
}
}
public void setBit(int index, int position, boolean value) {
checkIndex(index);
byte oldByte = buffer.get(index);
byte newByte = setBit(oldByte, position, value);
buffer.put(index, newByte);
}
private void checkIndex(int index) {
if (index < 0 || index >= buffer.position()) {
throw new IndexOutOfBoundsException("Index is out of bounds");
}
}
private void ensureCapacity(int minimumCapacity) {
if (buffer.capacity() < minimumCapacity) {
int newCapacity = Math.max(buffer.capacity() * 2, minimumCapacity);
ByteBuffer newBuffer = ByteBuffer.allocate(newCapacity);
newBuffer.order(buffer.order());
buffer.flip();
newBuffer.put(buffer);
buffer = newBuffer;
}
}
public void fill(int length, byte value) {
ensureCapacity(buffer.position() + length);
byte[] fillArray = new byte[length];
Arrays.fill(fillArray, value);
buffer.put(fillArray);
}
public int getRemainingCapacity() {
return buffer.remaining();
}
public byte[] toByteArray() {
byte[] result = new byte[buffer.position()];
buffer.flip();
buffer.get(result);
return result;
}
public void clear() {
buffer.rewind();
}
}