Skip to content

Commit f2fe1d3

Browse files
committed
Update issue 28
A workaround to postpone the initialization of SnappyNativeAPI.
1 parent 2906673 commit f2fe1d3

5 files changed

Lines changed: 53 additions & 35 deletions

File tree

.hgsubstate

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
acd53350367363d747e66caae0fe53b3d575ba4a wiki
1+
0fb78fe3d92a2ff45ab3edb3d821d93619e43663 wiki

src/main/java/org/xerial/snappy/Snappy.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,20 @@
3636
*/
3737
public class Snappy
3838
{
39-
private static SnappyNativeAPI impl;
40-
4139
static {
42-
impl = SnappyLoader.load();
40+
try {
41+
impl = SnappyLoader.load();
42+
}
43+
catch (Exception e) {
44+
e.printStackTrace();
45+
}
4346
}
4447

48+
/**
49+
* An instance of SnappyNativeAPI
50+
*/
51+
private static Object impl;
52+
4553
/**
4654
* Copy bytes from source to destination
4755
*
@@ -59,7 +67,7 @@ public class Snappy
5967
*/
6068
public static void arrayCopy(Object src, int offset, int byteLength, Object dest, int dest_offset)
6169
throws IOException {
62-
impl.arrayCopy(src, offset, byteLength, dest, dest_offset);
70+
((SnappyNativeAPI) impl).arrayCopy(src, offset, byteLength, dest, dest_offset);
6371
}
6472

6573
/**
@@ -120,7 +128,8 @@ public static int compress(ByteBuffer uncompressed, ByteBuffer compressed) throw
120128
// output: compressed
121129
int uPos = uncompressed.position();
122130
int uLen = uncompressed.remaining();
123-
int compressedSize = impl.rawCompress(uncompressed, uPos, uLen, compressed, compressed.position());
131+
int compressedSize = ((SnappyNativeAPI) impl).rawCompress(uncompressed, uPos, uLen, compressed,
132+
compressed.position());
124133

125134
// pos limit
126135
// [ ......BBBBBBB.........]
@@ -173,7 +182,7 @@ public static byte[] compress(String s, String encoding) throws UnsupportedEncod
173182
* @return native library version
174183
*/
175184
public static String getNativeLibraryVersion() {
176-
return impl.nativeLibraryVersion();
185+
return ((SnappyNativeAPI) impl).nativeLibraryVersion();
177186
}
178187

179188
/**
@@ -185,7 +194,7 @@ public static String getNativeLibraryVersion() {
185194
public static boolean isValidCompressedBuffer(byte[] input, int offset, int length) throws IOException {
186195
if (input == null)
187196
throw new NullPointerException("input is null");
188-
return impl.isValidCompressedBuffer(input, offset, length);
197+
return ((SnappyNativeAPI) impl).isValidCompressedBuffer(input, offset, length);
189198
}
190199

191200
/**
@@ -205,7 +214,8 @@ public static boolean isValidCompressedBuffer(byte[] input) throws IOException {
205214
* factor of four faster than actual decompression.
206215
*/
207216
public static boolean isValidCompressedBuffer(ByteBuffer compressed) throws IOException {
208-
return impl.isValidCompressedBuffer(compressed, compressed.position(), compressed.remaining());
217+
return ((SnappyNativeAPI) impl).isValidCompressedBuffer(compressed, compressed.position(),
218+
compressed.remaining());
209219
}
210220

211221
/**
@@ -217,7 +227,7 @@ public static boolean isValidCompressedBuffer(ByteBuffer compressed) throws IOEx
217227
* @return maximum byte size of the compressed data
218228
*/
219229
public static int maxCompressedLength(int byteSize) {
220-
return impl.maxCompressedLength(byteSize);
230+
return ((SnappyNativeAPI) impl).maxCompressedLength(byteSize);
221231
}
222232

223233
/**
@@ -231,7 +241,7 @@ public static int maxCompressedLength(int byteSize) {
231241
*/
232242
public static byte[] rawCompress(Object data, int byteSize) {
233243
byte[] buf = new byte[Snappy.maxCompressedLength(byteSize)];
234-
int compressedByteSize = impl.rawCompress(data, 0, byteSize, buf, 0);
244+
int compressedByteSize = ((SnappyNativeAPI) impl).rawCompress(data, 0, byteSize, buf, 0);
235245
byte[] result = new byte[compressedByteSize];
236246
System.arraycopy(buf, 0, result, 0, compressedByteSize);
237247
return result;
@@ -259,7 +269,8 @@ public static int rawCompress(Object input, int inputOffset, int inputLength, by
259269
if (input == null || output == null)
260270
throw new NullPointerException("input or output is null");
261271

262-
int compressedSize = impl.rawCompress(input, inputOffset, inputLength, output, outputOffset);
272+
int compressedSize = ((SnappyNativeAPI) impl)
273+
.rawCompress(input, inputOffset, inputLength, output, outputOffset);
263274
return compressedSize;
264275
}
265276

@@ -290,7 +301,7 @@ public static int rawUncompress(byte[] input, int inputOffset, int inputLength,
290301
throws IOException {
291302
if (input == null || output == null)
292303
throw new NullPointerException("input or output is null");
293-
return impl.rawUncompress(input, inputOffset, inputLength, output, outputOffset);
304+
return ((SnappyNativeAPI) impl).rawUncompress(input, inputOffset, inputLength, output, outputOffset);
294305
}
295306

296307
/**
@@ -362,7 +373,8 @@ public static int uncompress(ByteBuffer compressed, ByteBuffer uncompressed) thr
362373

363374
// pos limit
364375
// [ ......UUUUUU.........]
365-
int decompressedSize = impl.rawUncompress(compressed, cPos, cLen, uncompressed, uncompressed.position());
376+
int decompressedSize = ((SnappyNativeAPI) impl).rawUncompress(compressed, cPos, cLen, uncompressed,
377+
uncompressed.position());
366378
uncompressed.limit(uncompressed.position() + decompressedSize);
367379

368380
return decompressedSize;
@@ -375,14 +387,14 @@ public static char[] uncompressCharArray(byte[] input) throws IOException {
375387
public static char[] uncompressCharArray(byte[] input, int offset, int length) throws IOException {
376388
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
377389
char[] result = new char[uncompressedLength / 2];
378-
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
390+
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, offset, length, result, 0);
379391
return result;
380392
}
381393

382394
public static double[] uncompressDoubleArray(byte[] input) throws IOException {
383395
int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length);
384396
double[] result = new double[uncompressedLength / 8];
385-
int byteSize = impl.rawUncompress(input, 0, input.length, result, 0);
397+
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, 0, input.length, result, 0);
386398
return result;
387399
}
388400

@@ -397,7 +409,7 @@ public static double[] uncompressDoubleArray(byte[] input) throws IOException {
397409
* {@link SnappyErrorCode#PARSING_ERROR}
398410
*/
399411
public static int uncompressedLength(byte[] input) throws IOException {
400-
return impl.uncompressedLength(input, 0, input.length);
412+
return ((SnappyNativeAPI) impl).uncompressedLength(input, 0, input.length);
401413
}
402414

403415
/**
@@ -416,7 +428,7 @@ public static int uncompressedLength(byte[] input, int offset, int length) throw
416428
if (input == null)
417429
throw new NullPointerException("input is null");
418430

419-
return impl.uncompressedLength(input, offset, length);
431+
return ((SnappyNativeAPI) impl).uncompressedLength(input, offset, length);
420432
}
421433

422434
/**
@@ -436,7 +448,7 @@ public static int uncompressedLength(ByteBuffer compressed) throws IOException {
436448
if (!compressed.isDirect())
437449
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer");
438450

439-
return impl.uncompressedLength(compressed, compressed.position(), compressed.remaining());
451+
return ((SnappyNativeAPI) impl).uncompressedLength(compressed, compressed.position(), compressed.remaining());
440452
}
441453

442454
public static float[] uncompressFloatArray(byte[] input) throws IOException {
@@ -446,7 +458,7 @@ public static float[] uncompressFloatArray(byte[] input) throws IOException {
446458
public static float[] uncompressFloatArray(byte[] input, int offset, int length) throws IOException {
447459
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
448460
float[] result = new float[uncompressedLength / 4];
449-
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
461+
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, offset, length, result, 0);
450462
return result;
451463
}
452464

@@ -457,7 +469,7 @@ public static int[] uncompressIntArray(byte[] input) throws IOException {
457469
public static int[] uncompressIntArray(byte[] input, int offset, int length) throws IOException {
458470
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
459471
int[] result = new int[uncompressedLength / 4];
460-
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
472+
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, offset, length, result, 0);
461473
return result;
462474
}
463475

@@ -468,7 +480,7 @@ public static long[] uncompressLongArray(byte[] input) throws IOException {
468480
public static long[] uncompressLongArray(byte[] input, int offset, int length) throws IOException {
469481
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
470482
long[] result = new long[uncompressedLength / 8];
471-
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
483+
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, offset, length, result, 0);
472484
return result;
473485
}
474486

@@ -479,7 +491,7 @@ public static short[] uncompressShortArray(byte[] input) throws IOException {
479491
public static short[] uncompressShortArray(byte[] input, int offset, int length) throws IOException {
480492
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
481493
short[] result = new short[uncompressedLength / 2];
482-
int byteSize = impl.rawUncompress(input, offset, length, result, 0);
494+
int byteSize = ((SnappyNativeAPI) impl).rawUncompress(input, offset, length, result, 0);
483495
return result;
484496
}
485497

src/main/java/org/xerial/snappy/SnappyLoader.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@
8585
*/
8686
public class SnappyLoader
8787
{
88-
public static final String SNAPPY_SYSTEM_PROPERTIES_FILE = "org-xerial-snappy.properties";
89-
public static final String KEY_SNAPPY_LIB_PATH = "org.xerial.snappy.lib.path";
90-
public static final String KEY_SNAPPY_LIB_NAME = "org.xerial.snappy.lib.name";
91-
public static final String KEY_SNAPPY_TEMPDIR = "org.xerial.snappy.tempdir";
92-
public static final String KEY_SNAPPY_USE_SYSTEMLIB = "org.xerial.snappy.use.systemlib";
93-
public static final String KEY_SNAPPY_DISABLE_BUNDLED_LIBS = "org.xerial.snappy.disable.bundled.libs"; // Depreciated, but preserved for backward compatibility
88+
public static final String SNAPPY_SYSTEM_PROPERTIES_FILE = "org-xerial-snappy.properties";
89+
public static final String KEY_SNAPPY_LIB_PATH = "org.xerial.snappy.lib.path";
90+
public static final String KEY_SNAPPY_LIB_NAME = "org.xerial.snappy.lib.name";
91+
public static final String KEY_SNAPPY_TEMPDIR = "org.xerial.snappy.tempdir";
92+
public static final String KEY_SNAPPY_USE_SYSTEMLIB = "org.xerial.snappy.use.systemlib";
93+
public static final String KEY_SNAPPY_DISABLE_BUNDLED_LIBS = "org.xerial.snappy.disable.bundled.libs"; // Depreciated, but preserved for backward compatibility
9494

95-
private static boolean isLoaded = false;
96-
private static SnappyNativeAPI api = null;
95+
private static boolean isLoaded = false;
96+
private static Object api = null;
9797

9898
/**
9999
* load system properties when configuration file of the name
@@ -205,7 +205,7 @@ private static boolean hasInjectedNativeLoader() {
205205
*
206206
* @return
207207
*/
208-
static synchronized SnappyNativeAPI load() {
208+
static synchronized Object load() {
209209

210210
if (api != null)
211211
return api;
@@ -220,7 +220,8 @@ static synchronized SnappyNativeAPI load() {
220220

221221
isLoaded = true;
222222
// Look up SnappyNative, injected to the root classloder, using reflection in order to avoid the initialization of SnappyNative class in this context class loader.
223-
api = (SnappyNativeAPI) Class.forName("org.xerial.snappy.SnappyNative").newInstance();
223+
Object nativeCode = Class.forName("org.xerial.snappy.SnappyNative").newInstance();
224+
api = nativeCode;
224225
}
225226
catch (Exception e) {
226227
e.printStackTrace();

src/main/java/org/xerial/snappy/SnappyNative.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
* SnappyNative.cpp
3434
*
3535
* <p>
36-
* <b> DO NOT USE THIS CLASS DIRECTLY since the direct use of this class might
37-
* break the native library code loading in {@link SnappyLoader}. </b>
36+
* <b> DO NOT USE THIS CLASS since the direct use of this class might break the
37+
* native library code loading in {@link SnappyLoader}. </b>
3838
* </p>
3939
*
4040
* @author leo

src/test/java/org/xerial/snappy/SnappyLoaderTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ public void load() throws Exception {
115115
_logger.debug(Snappy.getNativeLibraryVersion());
116116
}
117117

118+
@Test
119+
public void autoLoad() throws Exception {
120+
_logger.debug(Snappy.getNativeLibraryVersion());
121+
}
122+
118123
public static void main(String[] args) {
119124
// Test for loading native library specified in -Djava.library.path
120125
System.setProperty(SnappyLoader.KEY_SNAPPY_USE_SYSTEMLIB, "true");

0 commit comments

Comments
 (0)