forked from DNAProject/DNA-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryReader.java
More file actions
154 lines (129 loc) · 3.77 KB
/
Copy pathBinaryReader.java
File metadata and controls
154 lines (129 loc) · 3.77 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package DNA.IO;
import java.io.*;
import java.lang.reflect.Array;
import java.nio.*;
import org.bouncycastle.math.ec.ECPoint;
import DNA.Cryptography.ECC;
public class BinaryReader implements AutoCloseable {
private DataInputStream reader;
private byte[] array = new byte[8];
private ByteBuffer buffer = ByteBuffer.wrap(array).order(ByteOrder.LITTLE_ENDIAN);
public BinaryReader(InputStream stream) {
this.reader = new DataInputStream(stream);
}
@Override
public void close() throws IOException {
reader.close();
}
public void read(byte[] buffer) throws IOException {
reader.readFully(buffer);
}
public void read(byte[] buffer, int index, int length) throws IOException {
reader.readFully(buffer, index, length);
}
public boolean readBoolean() throws IOException {
return reader.readBoolean();
}
public byte readByte() throws IOException {
return reader.readByte();
}
public byte[] readBytes(int count) throws IOException {
byte[] buffer = new byte[count];
reader.readFully(buffer);
return buffer;
}
public double readDouble() throws IOException {
reader.readFully(array, 0, 8);
return buffer.getDouble(0);
}
public ECPoint readECPoint() throws IOException {
byte[] encoded;
byte fb = reader.readByte();
switch (fb)
{
case 0x00:
encoded = new byte[1];
break;
case 0x02:
case 0x03:
encoded = new byte[33];
encoded[0] = fb;
reader.readFully(encoded, 1, 32);
break;
case 0x04:
encoded = new byte[65];
encoded[0] = fb;
reader.readFully(encoded, 1, 64);
break;
default:
throw new IOException();
}
return ECC.secp256r1.getCurve().decodePoint(encoded);
}
public String readFixedString(int length) throws IOException {
byte[] data = readBytes(length);
int count = -1;
while (data[++count] != 0);
return new String(data, 0, count, "UTF-8");
}
public float readFloat() throws IOException {
reader.readFully(array, 0, 4);
return buffer.getFloat(0);
}
public int readInt() throws IOException {
reader.readFully(array, 0, 4);
return buffer.getInt(0);
}
public long readLong() throws IOException {
reader.readFully(array, 0, 8);
return buffer.getLong(0);
}
public <T extends Serializable> T readSerializable(Class<T> t) throws InstantiationException, IllegalAccessException, IOException {
T obj = t.newInstance();
obj.deserialize(this);
return obj;
}
public <T extends Serializable> T[] readSerializableArray(Class<T> t) throws InstantiationException, IllegalAccessException, IOException {
@SuppressWarnings("unchecked")
T[] array = (T[])Array.newInstance(t, (int)readVarInt(0x10000000));
// System.out.println("arr.len==="+array.length);
for (int i = 0; i < array.length; i++) {
array[i] = t.newInstance();
array[i].deserialize(this);
}
return array;
}
public short readShort() throws IOException {
reader.readFully(array, 0, 2);
return buffer.getShort(0);
}
public byte[] readVarBytes() throws IOException {
return readVarBytes(0X7fffffc7);
}
public byte[] readVarBytes(int max) throws IOException {
return readBytes((int)readVarInt(max));
}
public long readVarInt() throws IOException {
return readVarInt(Long.MAX_VALUE);
}
public long readVarInt(long max) throws IOException {
long fb = Byte.toUnsignedLong(readByte());
long value;
if (fb == 0xFD) {
value = Short.toUnsignedLong(readShort());
} else if (fb == 0xFE) {
value = Integer.toUnsignedLong(readInt());
} else if (fb == 0xFF) {
value = readLong();
} else {
value = fb;
}
if (Long.compareUnsigned(value, max) > 0) {
throw new IOException();
}
return value;
}
public String readVarString() throws IOException {
return new String(readVarBytes(), "UTF-8");
}
}