forked from DNAProject/DNA-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryWriter.java
More file actions
132 lines (109 loc) · 3.07 KB
/
Copy pathBinaryWriter.java
File metadata and controls
132 lines (109 loc) · 3.07 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
package DNA.IO;
import java.io.*;
import java.nio.*;
import org.bouncycastle.math.ec.ECPoint;
public class BinaryWriter implements AutoCloseable {
private DataOutputStream writer;
private byte[] array = new byte[8];
private ByteBuffer buffer = ByteBuffer.wrap(array).order(ByteOrder.LITTLE_ENDIAN);
public BinaryWriter(OutputStream stream) {
this.writer = new DataOutputStream(stream);
}
@Override
public void close() throws IOException {
writer.close();
}
public void flush() throws IOException {
writer.flush();
}
public void write(byte[] buffer) throws IOException {
writer.write(buffer);
}
public void write(byte[] buffer, int index, int length) throws IOException {
writer.write(buffer, index, length);
}
public void writeBoolean(boolean v) throws IOException {
writer.writeBoolean(v);
}
public void writeByte(byte v) throws IOException {
writer.writeByte(v);
}
public void writeDouble(double v) throws IOException {
buffer.putDouble(0, v);
writer.write(array, 0, 8);
}
public void writeECPoint(ECPoint v) throws IOException {
writer.write(v.getEncoded(true));
}
public void writeFixedString(String v, int length) throws IOException {
if (v == null) {
throw new IllegalArgumentException();
}
if (v.length() > length) {
throw new IllegalArgumentException();
}
byte[] bytes = v.getBytes("UTF-8");
if (bytes.length > length) {
throw new IllegalArgumentException();
}
writer.write(bytes);
if (bytes.length < length) {
writer.write(new byte[length - bytes.length]);
}
}
public void writeFloat(float v) throws IOException {
buffer.putFloat(0, v);
writer.write(array, 0, 4);
}
public void writeInt(int v) throws IOException {
buffer.putInt(0, v);
writer.write(array, 0, 4);
}
public void writeLong(long v) throws IOException {
buffer.putLong(0, v);
writer.write(array, 0, 8);
}
public void writeSerializable(Serializable v) throws IOException {
v.serialize(this);
}
public void writeSerializableArray(Serializable[] v) throws IOException {
writeVarInt(v.length);
for (int i = 0; i < v.length; i++) {
v[i].serialize(this);
}
}
public void writeSerializableArray2(Serializable[] v) throws IOException {
writeInt(v.length);
for (int i = 0; i < v.length; i++) {
v[i].serialize(this);
}
}
public void writeShort(short v) throws IOException {
buffer.putShort(0, v);
writer.write(array, 0, 2);
}
public void writeVarBytes(byte[] v) throws IOException {
writeVarInt(v.length);
writer.write(v);
}
public void writeVarInt(long v) throws IOException {
if (v < 0) {
throw new IllegalArgumentException();
}
if (v < 0xFD) {
writeByte((byte)v);
} else if (v <= 0xFFFF) {
writeByte((byte)0xFD);
writeShort((short)v);
} else if (v <= 0xFFFFFFFF) {
writeByte((byte)0xFE);
writeInt((int)v);
} else {
writeByte((byte)0xFF);
writeLong(v);
}
}
public void writeVarString(String v) throws IOException {
writeVarBytes(v.getBytes("UTF-8"));
}
}