Repository navigation
Expand file tree
/
Copy pathDataIO.java
More file actions
220 lines (174 loc) · 5.32 KB
/
Copy pathDataIO.java
File metadata and controls
220 lines (174 loc) · 5.32 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
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
package org.mapdb;
import java.io.Closeable;
import java.io.DataInput;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
/**
* Various IO classes and utilities..
*/
public final class DataIO {
private DataIO(){}
/**
* Give state to internal byte[] or ByteBuffer in DataInput2..
* Should not be used unless you are writing MapDB extension and needs some performance bonus
*/
interface DataInputInternal extends DataInput,Closeable {
int getPos();
void setPos(int pos);
/** return underlying `byte[]` or null if it does not exist*/
byte[] internalByteArray();
/** return underlying `ByteBuffer` or null if it does not exist*/
ByteBuffer internalByteBuffer();
void close();
}
/** DataInput on top of `byte[]` */
static public final class DataInputByteArray implements DataInput, DataInputInternal {
protected final byte[] buf;
protected int pos;
public DataInputByteArray(byte[] b) {
this(b, 0);
}
public DataInputByteArray(byte[] bb, int pos) {
buf = bb;
this.pos = pos;
}
@Override
public void readFully(byte[] b) throws IOException {
readFully(b, 0, b.length);
}
@Override
public void readFully(byte[] b, int off, int len) throws IOException {
System.arraycopy(buf, pos, b, off, len);
pos += len;
}
@Override
public int skipBytes(final int n) throws IOException {
pos += n;
return n;
}
@Override
public boolean readBoolean() throws IOException {
return buf[pos++] == 1;
}
@Override
public byte readByte() throws IOException {
return buf[pos++];
}
@Override
public int readUnsignedByte() throws IOException {
return buf[pos++] & 0xff;
}
@Override
public short readShort() throws IOException {
return (short)((buf[pos++] << 8) | (buf[pos++] & 0xff));
}
@Override
public int readUnsignedShort() throws IOException {
return (((buf[pos++] & 0xff) << 8) |
((buf[pos++] & 0xff)));
}
@Override
public char readChar() throws IOException {
// I know: 4 bytes, but char only consumes 2,
// has to stay here for backward compatibility
return (char) readInt();
}
@Override
public int readInt() throws IOException {
final int end = pos + 4;
int ret = 0;
for (; pos < end; pos++) {
ret = (ret << 8) | (buf[pos] & 0xFF);
}
return ret;
}
@Override
public long readLong() throws IOException {
final int end = pos + 8;
long ret = 0;
for (; pos < end; pos++) {
ret = (ret << 8) | (buf[pos] & 0xFF);
}
return ret;
}
@Override
public float readFloat() throws IOException {
return Float.intBitsToFloat(readInt());
}
@Override
public double readDouble() throws IOException {
return Double.longBitsToDouble(readLong());
}
@Override
public String readLine() throws IOException {
return readUTF();
}
@Override
public String readUTF() throws IOException {
final int len = unpackInt();
char[] b = new char[len];
for (int i = 0; i < len; i++)
b[i] = (char) unpackInt();
return new String(b);
}
@Override
public int getPos() {
return pos;
}
@Override
public void setPos(int pos) {
this.pos = pos;
}
@Override
public byte[] internalByteArray() {
return buf;
}
@Override
public ByteBuffer internalByteBuffer() {
return null;
}
@Override
public void close() {
}
protected int unpackInt() throws IOException {
int offset = 0;
int result=0;
int b;
do {
b = buf[pos++];
result |= (b & 0x7F) << offset;
offset += 7;
}while((b & 0x80) != 0);
return result;
}
}
/**
* Wraps `DataInput` into `InputStream`
*/
public static class DataInputToStream extends InputStream {
protected final DataInput in;
public DataInputToStream(DataInput in) {
this.in = in;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
in.readFully(b,off,len);
return len;
}
@Override
public long skip(long n) throws IOException {
n = Math.min(n, Integer.MAX_VALUE);
return in.skipBytes((int) n);
}
@Override
public void close() throws IOException {
if(in instanceof Closeable)
((Closeable) in).close();
}
@Override
public int read() throws IOException {
return in.readUnsignedByte();
}
}
}