bijection icon indicating copy to clipboard operation
bijection copied to clipboard

DeflaterBijection

Open sritchie opened this issue 11 years ago • 1 comments

Bijection that moves between deflated and inflated bytes.

Here's the idea (please forgive my lack of wrapper classes):

import java.util.zip.{ Deflater, Inflater, InflaterInputStream, DeflaterOutputStream }
import java.io.{ ByteArrayInputStream, ByteArrayOutputStream }

class DeflaterBijection(deflater: Deflater, inflater: Inflater) extends AbstractBijection[Array[Byte], Array[Byte]] {
  def apply(bytes: Array[Byte]) = {
    val baos = new ByteArrayOutputStream
    val dos = new DeflaterOutputStream(baos, deflater)
    dos.write(bytes)
    dos.close
    baos.toByteArray
  }
  override def invert(bytes: Array[Byte]) = {
    val baos = new ByteArrayOutputStream
    StreamUtils.copy(new InflaterInputStream(new ByteArrayInputStream(bytes), inflater), baos)
    baos.toByteArray
  }
}

sritchie avatar Apr 14 '13 01:04 sritchie

Looks fine, but I think we should wrap this:

https://github.com/dain/snappy/tree/master/src/main/java/org/iq80/snappy

Much better performance (much faster), pure Java, Apache license. We should encourage using snappy when we want performance.

johnynek avatar Apr 15 '13 15:04 johnynek