forked from google/flatbuffers
/
ArrayReadWriteBuf.java
252 lines (210 loc) · 6.07 KB
/
ArrayReadWriteBuf.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package com.google.flatbuffers;
import java.util.Arrays;
/**
* Implements {@code ReadBuf} using an array of bytes
* as a backing storage. Using array of bytes are
* usually faster than {@code ByteBuffer}.
*
* This class is not thread-safe, meaning that
* it must operate on a single thread. Operating from
* multiple thread leads into a undefined behavior
*/
public class ArrayReadWriteBuf implements ReadWriteBuf {
private byte[] buffer;
private int writePos;
public ArrayReadWriteBuf() {
this(10);
}
public ArrayReadWriteBuf(int initialCapacity) {
this(new byte[initialCapacity]);
}
public ArrayReadWriteBuf(byte[] buffer) {
this.buffer = buffer;
this.writePos = 0;
}
public ArrayReadWriteBuf(byte[] buffer, int startPos) {
this.buffer = buffer;
this.writePos = startPos;
}
@Override
public void clear() {
this.writePos = 0;
}
@Override
public boolean getBoolean(int index) {
return buffer[index] != 0;
}
@Override
public byte get(int index) {
return buffer[index];
}
@Override
public short getShort(int index) {
return (short) ((buffer[index+ 1] << 8) | (buffer[index] & 0xff));
}
@Override
public int getInt(int index) {
return (((buffer[index + 3]) << 24) |
((buffer[index + 2] & 0xff) << 16) |
((buffer[index + 1] & 0xff) << 8) |
((buffer[index] & 0xff)));
}
@Override
public long getLong(int index) {
return ((((long) buffer[index++] & 0xff)) |
(((long) buffer[index++] & 0xff) << 8) |
(((long) buffer[index++] & 0xff) << 16) |
(((long) buffer[index++] & 0xff) << 24) |
(((long) buffer[index++] & 0xff) << 32) |
(((long) buffer[index++] & 0xff) << 40) |
(((long) buffer[index++] & 0xff) << 48) |
(((long) buffer[index]) << 56));
}
@Override
public float getFloat(int index) {
return Float.intBitsToFloat(getInt(index));
}
@Override
public double getDouble(int index) {
return Double.longBitsToDouble(getLong(index));
}
@Override
public String getString(int start, int size) {
return Utf8Safe.decodeUtf8Array(buffer, start, size);
}
@Override
public byte[] data() {
return buffer;
}
@Override
public void putBoolean(boolean value) {
setBoolean(writePos, value);
writePos++;
}
@Override
public void put(byte[] value, int start, int length) {
set(writePos, value, start, length);
writePos+=length;
}
@Override
public void put(byte value) {
set(writePos, value);
writePos++;
}
@Override
public void putShort(short value) {
setShort(writePos, value);
writePos +=2;
}
@Override
public void putInt(int value) {
setInt(writePos, value);
writePos +=4;
}
@Override
public void putLong(long value) {
setLong(writePos, value);
writePos +=8;
}
@Override
public void putFloat(float value) {
setFloat(writePos, value);
writePos +=4;
}
@Override
public void putDouble(double value) {
setDouble(writePos, value);
writePos +=8;
}
@Override
public void setBoolean(int index, boolean value) {
set(index, value ? (byte)1 : (byte)0);
}
@Override
public void set(int index, byte value) {
requestCapacity(index + 1);
buffer[index] = value;
}
@Override
public void set(int index, byte[] toCopy, int start, int length) {
requestCapacity(index + (length - start));
System.arraycopy(toCopy, start, buffer, index, length);
}
@Override
public void setShort(int index, short value) {
requestCapacity(index + 2);
buffer[index++] = (byte) ((value) & 0xff);
buffer[index ] = (byte) ((value >> 8) & 0xff);
}
@Override
public void setInt(int index, int value) {
requestCapacity(index + 4);
buffer[index++] = (byte) ((value) & 0xff);
buffer[index++] = (byte) ((value >> 8) & 0xff);
buffer[index++] = (byte) ((value >> 16) & 0xff);
buffer[index ] = (byte) ((value >> 24) & 0xff);
}
@Override
public void setLong(int index, long value) {
requestCapacity(index + 8);
int i = (int) value;
buffer[index++] = (byte) ((i) & 0xff);
buffer[index++] = (byte) ((i >> 8) & 0xff);
buffer[index++] = (byte) ((i >> 16) & 0xff);
buffer[index++] = (byte) ((i >> 24) & 0xff);
i = (int) (value >> 32);
buffer[index++] = (byte) ((i) & 0xff);
buffer[index++] = (byte) ((i >> 8) & 0xff);
buffer[index++] = (byte) ((i >> 16) & 0xff);
buffer[index ] = (byte) ((i >> 24) & 0xff);
}
@Override
public void setFloat(int index, float value) {
requestCapacity(index + 4);
int iValue = Float.floatToRawIntBits(value);
buffer[index++] = (byte) ((iValue) & 0xff);
buffer[index++] = (byte) ((iValue >> 8) & 0xff);
buffer[index++] = (byte) ((iValue >> 16) & 0xff);
buffer[index ] = (byte) ((iValue >> 24) & 0xff);
}
@Override
public void setDouble(int index, double value) {
requestCapacity(index + 8);
long lValue = Double.doubleToRawLongBits(value);
int i = (int) lValue;
buffer[index++] = (byte) ((i) & 0xff);
buffer[index++] = (byte) ((i >> 8) & 0xff);
buffer[index++] = (byte) ((i >> 16) & 0xff);
buffer[index++] = (byte) ((i >> 24) & 0xff);
i = (int) (lValue >> 32);
buffer[index++] = (byte) ((i) & 0xff);
buffer[index++] = (byte) ((i >> 8) & 0xff);
buffer[index++] = (byte) ((i >> 16) & 0xff);
buffer[index ] = (byte) ((i >> 24) & 0xff);
}
@Override
public int limit() {
return writePos;
}
@Override
public int writePosition() {
return writePos;
}
@Override
public boolean requestCapacity(int capacity) {
if (capacity < 0) {
throw new IllegalArgumentException("Capacity may not be negative (likely a previous int overflow)");
}
if (buffer.length >= capacity) {
return true;
}
// implemented in the same growing fashion as ArrayList
int oldCapacity = buffer.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity < capacity) { // Note: this also catches newCapacity int overflow
newCapacity = capacity;
}
buffer = Arrays.copyOf(buffer, newCapacity);
return true;
}
}