Snappy-java解压缩失败以获取有效数据

时间:2014-10-18 00:32:37

标签: scala akka snappy

我正在尝试使用snappy-java

解压缩bytestring
ByteString(0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, 1, 14, 0, 0, 38, -104, 43, -49, 0, 0, 0, 6, 0, 0, 0, 0, 79, 75)

它包含两个帧,第一个具有块值0xff(流标识符)和长度6以及块类型1的第二帧(未压缩),长度为14.根据协议规范[此处]有效({{3 }})

用于解压缩的代码在这里

val c = ByteString(0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, 1, 14, 0, 0, 38, -104, 43, -49, 0, 0, 0, 6, 0, 0, 0, 0, 79, 75)
Snappy.uncompress(c.toArray)

代码抛出FAILED_TO_UNCOMPRESS错误,这是jna的一部分。我正在使用scala v2.11.3和snappy-java v1.0.5.4

Exception in thread "main" java.io.IOException: FAILED_TO_UNCOMPRESS(5)
at org.xerial.snappy.SnappyNative.throw_error(SnappyNative.java:78)
at org.xerial.snappy.SnappyNative.rawUncompress(Native Method)
at org.xerial.snappy.Snappy.rawUncompress(Snappy.java:395)
at org.xerial.snappy.Snappy.uncompress(Snappy.java:431)
at org.xerial.snappy.Snappy.uncompress(Snappy.java:407)

1 个答案:

答案 0 :(得分:3)

无法解压缩错误是因为Snappy.uncompress不支持框架输入。最近完成了框架格式,并在SnappyFramedInputStream中添加了实现。来源位于here

以下是解压缩snappy帧的代码

def decompress(contents: Array[Byte]): Array[Byte] = {
    val is = new SnappyFramedInputStream(new ByteArrayInputStream(contents))
    val os = new ByteArrayOutputStream(Snappy.uncompressedLength(contents))
    is.transferTo(os)
    os.close()
    os.toByteArray
}