forked from ivanhk/fastText_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOUtil.java
More file actions
97 lines (79 loc) · 2.89 KB
/
Copy pathIOUtil.java
File metadata and controls
97 lines (79 loc) · 2.89 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
package fasttext;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
public class IOUtil {
private static final int MAX_STRING_SIZE = 50;
public static int readInt(InputStream is) throws IOException {
byte[] bytes = new byte[4];
is.read(bytes);
return getInt(bytes);
}
public static int getInt(byte[] b) {
return (b[0] & 0xFF) << 0 | (b[1] & 0xFF) << 8 | (b[2] & 0xFF) << 16 | (b[3] & 0xFF) << 24;
}
public static long readLong(InputStream is) throws IOException {
byte[] bytes = new byte[8];
is.read(bytes);
return getLong(bytes);
}
public static long getLong(byte[] b) {
return (b[0] & 0xFFL) << 0 | (b[1] & 0xFFL) << 8 | (b[2] & 0xFFL) << 16 | (b[3] & 0xFFL) << 24
| (b[4] & 0xFFL) << 32 | (b[5] & 0xFFL) << 40 | (b[6] & 0xFFL) << 48 | (b[7] & 0xFFL) << 56;
}
public static float readFloat(InputStream is) throws IOException {
byte[] bytes = new byte[4];
is.read(bytes);
return getFloat(bytes);
}
public static float getFloat(byte[] b) {
int accum = (b[0] & 0xFF) << 0 | (b[1] & 0xFF) << 8 | (b[2] & 0xFF) << 16 | (b[3] & 0xFF) << 24;
return Float.intBitsToFloat(accum);
}
public static double readDouble(InputStream is) throws IOException {
byte[] bytes = new byte[8];
is.read(bytes);
return getDouble(bytes);
}
public static double getDouble(byte[] b) {
long accum = getLong(b);
return Double.longBitsToDouble(accum);
}
public static String readString(DataInputStream dis) throws IOException {
byte[] bytes = new byte[MAX_STRING_SIZE];
byte b = dis.readByte();
int i = -1;
StringBuilder sb = new StringBuilder();
while (b != 32 && b != 10 && b != 0) { // ascii
i++;
bytes[i] = b;
b = dis.readByte();
if (i == 49) {
sb.append(new String(bytes));
i = -1;
bytes = new byte[MAX_STRING_SIZE];
}
}
sb.append(new String(bytes, 0, i + 1));
return sb.toString();
}
public static byte[] intToByteArray(int i) {
return new byte[] { (byte) ((i >> 0) & 0xff), (byte) ((i >> 8) & 0xff), (byte) ((i >> 16) & 0xff),
(byte) ((i >> 24) & 0xff) };
}
public static byte[] longToByteArray(long l) {
return new byte[] { (byte) ((l >> 0) & 0xff), (byte) ((l >> 8) & 0xff), (byte) ((l >> 16) & 0xff),
(byte) ((l >> 24) & 0xff), (byte) ((l >> 32) & 0xff), (byte) ((l >> 40) & 0xff),
(byte) ((l >> 48) & 0xff), (byte) ((l >> 56) & 0xff) };
}
public static byte[] floatToByteArray(float f) {
int i = Float.floatToIntBits(f);
return intToByteArray(i);
}
public static byte[] doubleToByteArray(double d) {
long l = Double.doubleToRawLongBits(d);
return new byte[] { (byte) ((l >> 0) & 0xff), (byte) ((l >> 8) & 0xff), (byte) ((l >> 16) & 0xff),
(byte) ((l >> 24) & 0xff), (byte) ((l >> 32) & 0xff), (byte) ((l >> 40) & 0xff),
(byte) ((l >> 48) & 0xff), (byte) ((l >> 56) & 0xff) };
}
}