From da9cf2d8c9a49e3bf353dde0607d056d3c6c7b5d Mon Sep 17 00:00:00 2001 From: "Taro L. Saito" Date: Sat, 4 Jun 2011 11:36:52 +0900 Subject: [PATCH] Add a pure-java code for GetUncompressedLength() --- src/main/java/org/xerial/snappy/Snappy.java | 59 ++++++++++++++++++ .../native/Windows/amd64/snappyjava.dll | Bin 644608 -> 644608 bytes .../snappy/native/Windows/x86/snappyjava.dll | Bin 598528 -> 598528 bytes 3 files changed, 59 insertions(+) diff --git a/src/main/java/org/xerial/snappy/Snappy.java b/src/main/java/org/xerial/snappy/Snappy.java index ea6b41e..256a5b4 100755 --- a/src/main/java/org/xerial/snappy/Snappy.java +++ b/src/main/java/org/xerial/snappy/Snappy.java @@ -384,9 +384,68 @@ public class Snappy public static int uncompressedLength(byte[] input, int offset, int length) throws SnappyException { if (input == null) throw new NullPointerException("input is null"); + return SnappyNative.uncompressedLength(input, offset, length); } + public static class CompressedDataLength + { + public final int cursor; + public final int uncompressedLength; + + public CompressedDataLength(int cursor, int uncompressedLength) { + this.cursor = cursor; + this.uncompressedLength = uncompressedLength; + } + } + + public static CompressedDataLength getUncompressedLength(byte[] input, int offset, int limit) + throws SnappyException { + if (input == null) + throw new NullPointerException("input is null"); + + long b = 0; + long result = 0; + int cursor = offset; + if (cursor >= limit) + return null; + for (;;) { + b = input[cursor++]; + result = b & 127; + if (b < 128) + break; + if (cursor >= limit) + return null; + b = input[cursor++]; + result |= (b & 127) << 7; + if (b < 128) + break; + if (cursor >= limit) + return null; + b = input[cursor++]; + result |= (b & 127) << 14; + if (b < 128) + break; + if (cursor >= limit) + return null; + b = input[cursor++]; + result |= (b & 127) << 21; + if (b < 128) + break; + if (cursor >= limit) + return null; + b = input[cursor++]; + result |= (b & 127) << 28; + if (b < 16) + break; + return null; // Value is too long to be a varint32 + } + if (result > Integer.MAX_VALUE) + throw new IllegalStateException("cannot uncompress byte array longer than 2^31-1: " + result); + + return new CompressedDataLength(cursor, (int) result); + } + /** * Get the uncompressed byte size of the given compressed input. This * operation taks O(1) time. diff --git a/src/main/resources/org/xerial/snappy/native/Windows/amd64/snappyjava.dll b/src/main/resources/org/xerial/snappy/native/Windows/amd64/snappyjava.dll index c4da58e2139b75baf38175ba8211f4a6a07540f0..ab4dad2c99fcb360d5ad3b4ffa311f271efa624f 100755 GIT binary patch delta 59 zcmV-B0L1@*tS5l1Cyw(&Z+5v^y0)^TGh1vv#+69H$28G%Oh1v+U R+6gMJ1BH|6w+FBZ`6X(n9GCzA delta 59 zcmV-B0L1@*tS5l1CyrIx{o2;!j*;;S1Z@tN(y&t4# KJNp5SpNRn80vZAU delta 55 zcmZoTqSA0gWkLrt>#P?OyL}l?H{NW$$=G_6sr4pv>rIx{o2;!j*;;S1Z@tN(y&t4# KJNp5SpNRm_gc+Iu