Skip to content

Commit dda6777

Browse files
committed
Speed improvements, changed NameFormat to AlphaFormat
1 parent afdc0c2 commit dda6777

File tree

7 files changed

+160
-158
lines changed

7 files changed

+160
-158
lines changed

src/main/java/com/mapcode/DataAccess.java

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.mapcode;
1818

19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
1922
import java.io.ByteArrayOutputStream;
2023
import java.io.IOException;
2124
import java.io.InputStream;
@@ -28,61 +31,61 @@
2831
* This class contains the module that reads the Mapcode areas into memory and processes them.
2932
*/
3033
class DataAccess {
34+
private static final Logger LOG = LoggerFactory.getLogger(DataAccess.class);
3135

32-
private static final byte[] FILE_DATA;
36+
private static final int[] FILE_DATA;
3337
private static final String FILE_NAME = "/com/mapcode/mminfo.dat";
3438

3539
// Read data only once in static initializer.
3640
static {
37-
final InputStream inputStream = DataAccess.class.getResourceAsStream(FILE_NAME);
38-
try {
39-
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
40-
for (int readBytes = inputStream.read(); readBytes >= 0; readBytes = inputStream.read()) {
41-
outputStream.write(readBytes);
42-
}
41+
LOG.info("DataAccess: reading regions from file: {}", FILE_NAME);
42+
final int bufferSize = 100000;
43+
final byte[] readBuffer = new byte[bufferSize];
44+
int total = 0;
45+
try (final InputStream inputStream = DataAccess.class.getResourceAsStream(FILE_NAME)) {
46+
try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
47+
int nrBytes = inputStream.read(readBuffer);
48+
while (nrBytes >= 0) {
49+
total += nrBytes;
50+
outputStream.write(readBuffer, 0, nrBytes);
51+
nrBytes = inputStream.read(readBuffer);
52+
}
53+
54+
// Copy stream as unsigned bytes (ints).
55+
final byte[] bytes = outputStream.toByteArray();
56+
assert total == bytes.length;
57+
FILE_DATA = new int[total];
58+
for (int i = 0; i < total; ++i) {
59+
FILE_DATA[i] = (bytes[i] < 0) ? (bytes[i] + 256) : bytes[i];
4360

44-
FILE_DATA = outputStream.toByteArray();
45-
inputStream.close();
46-
outputStream.close();
47-
} catch (final IOException e) {
48-
throw new ExceptionInInitializerError("Cannot initialize static data structure from: " + FILE_NAME +
49-
", exception=" + e);
50-
} finally {
51-
try {
52-
if (inputStream != null) {
53-
inputStream.close();
5461
}
55-
} catch (final IOException ignored) {
56-
// Ignore.
5762
}
63+
} catch (final IOException e) {
64+
throw new ExceptionInInitializerError("Cannot initialize static data structure from: " +
65+
FILE_NAME + ", exception=" + e);
5866
}
67+
LOG.info("DataAccess: regions initialized, read {} bytes", total);
5968
}
6069

6170
private DataAccess() {
6271
// Empty.
6372
}
6473

65-
private static int asUnsignedByte(final int i) {
66-
int u = FILE_DATA[i];
67-
if (u < 0) {
68-
u += 256;
69-
}
70-
return u;
71-
}
72-
7374
static int dataFlags(final int i) {
74-
return asUnsignedByte((i * 20) + 16) + (asUnsignedByte((i * 20) + 17) * 256);
75+
return FILE_DATA[(i * 20) + 16] +
76+
(FILE_DATA[(i * 20) + 17] * 256);
7577
}
7678

7779
static int asLong(final int i) {
78-
return asUnsignedByte(i) +
79-
(asUnsignedByte(i + 1) << 8) +
80-
(asUnsignedByte(i + 2) << 16) +
81-
(asUnsignedByte(i + 3) << 24);
80+
return FILE_DATA[i] +
81+
(FILE_DATA[i + 1] << 8) +
82+
(FILE_DATA[i + 2] << 16) +
83+
(FILE_DATA[i + 3] << 24);
8284
}
8385

8486
static int smartDiv(final int i) {
85-
return asUnsignedByte((i * 20) + 18) + (asUnsignedByte((i * 20) + 19) * 256);
87+
return FILE_DATA[(i * 20) + 18] +
88+
(FILE_DATA[(i * 20) + 19] * 256);
8689
}
8790

8891
private final static int[] DATA_START = {

0 commit comments

Comments
 (0)