me.maciejb.snappyflows.impl.SnappyFramed.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snappy-flows_2.11 Show documentation
Show all versions of snappy-flows_2.11 Show documentation
Snappy compression Akka Streams flows
The newest version!
package me.maciejb.snappyflows.impl
import akka.util.ByteString
import me.maciejb.snappyflows.SnappyChecksum
import org.xerial.snappy.Snappy
private[snappyflows] object SnappyFramed {
object Flags {
val StreamIdentifier = 0xff.toByte
val UncompressedData = 0x01.toByte
val CompressedData = 0x00.toByte
}
private val HeaderBytes =
Array[Byte](Flags.StreamIdentifier, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59)
val Header = ByteString(HeaderBytes)
def compressChunk(chunk: ByteString): ByteString = {
val chunkBytes = chunk.toArray
val compressed = Snappy.compress(chunkBytes)
val checksum = SnappyChecksum.checksum(chunkBytes)
val length = Int24.writeLE(compressed.length + 4)
ByteString.newBuilder
.putByte(SnappyFramed.Flags.CompressedData)
.append(length)
.putInt(checksum)
.putBytes(compressed)
.result()
}
}