3636 */
3737public 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
0 commit comments