-
Notifications
You must be signed in to change notification settings - Fork 982
/
ISUtil.java
94 lines (74 loc) · 3.04 KB
/
ISUtil.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
package org.nocrala.tools.gis.data.esri.shapefile.util;
import org.nocrala.tools.gis.data.esri.shapefile.exception.DataStreamEOFException;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
public class ISUtil {
private static final byte[] BUFFER = new byte[8];
private static final ByteBuffer BYTE_BUFFER = ByteBuffer.wrap(BUFFER);
// Big endian int
public static synchronized int readBeIntMaybeEOF(final InputStream is)
throws DataStreamEOFException, IOException {
readIntoBufferMaybeEOF(is, 4);
return IntSerializer.deserializeBigEndian(BYTE_BUFFER);
}
public static synchronized int readBeInt(final InputStream is)
throws IOException {
readIntoBuffer(is, 4);
return IntSerializer.deserializeBigEndian(BYTE_BUFFER);
}
// Big endian double
public static synchronized double readBeDoubleMaybeEOF(final InputStream is)
throws DataStreamEOFException, IOException {
readIntoBufferMaybeEOF(is, 8);
return DoubleSerializer.deserializeBigEndian(BYTE_BUFFER);
}
public static synchronized double readBeDouble(final InputStream is)
throws IOException {
readIntoBuffer(is, 8);
return DoubleSerializer.deserializeBigEndian(BYTE_BUFFER);
}
// Little endian int
public static synchronized int readLeIntMaybeEOF(final InputStream is)
throws DataStreamEOFException, IOException {
readIntoBufferMaybeEOF(is, 4);
return IntSerializer.deserializeLittleEndian(BYTE_BUFFER);
}
public static synchronized int readLeInt(final InputStream is)
throws IOException {
readIntoBuffer(is, 4);
// System.out.println("--> " + HexaUtil.byteArrayToString(BUFFER));
return IntSerializer.deserializeLittleEndian(BYTE_BUFFER);
}
// Little endian double
public static synchronized double readLeDoubleMaybeEOF(final InputStream is)
throws DataStreamEOFException, IOException {
readIntoBufferMaybeEOF(is, 8);
return DoubleSerializer.deserializeLittleEndian(BYTE_BUFFER);
}
public static synchronized double readLeDouble(final InputStream is)
throws IOException {
readIntoBuffer(is, 8);
return DoubleSerializer.deserializeLittleEndian(BYTE_BUFFER);
}
// Utils
private static void readIntoBufferMaybeEOF(final InputStream is,
final int length) throws DataStreamEOFException, IOException {
try {
int read = is.read(BUFFER, 0, length);
if (read != length) {
throw new DataStreamEOFException();
}
} catch (EOFException e) {
throw new DataStreamEOFException();
}
}
private static void readIntoBuffer(final InputStream is, final int length)
throws IOException {
int read = is.read(BUFFER, 0, length);
if (read != length) {
throw new EOFException();
}
}
}