* Move (de)compression helpers to akka-stream #21395 * Move compression and decompression -related classes from akka-http-experimental to akka-stream * Add Compression helper object with functions to create decompressing Flows * Add a short cookbook entry * =str move compression impl classes into their own directory (and change visibility) * =str also expose gzip/deflate compression flows * Fix formatting of plural ByteStrings in cookbook * =str #21395 make compressor call Deflater.end in postStop to release resources Also simplified the creation of the flow given a compressor. * =str #21395 decompressors call Inflater.end in postStop to release resources * =str #21395 smallish Scaladoc fixes
40 lines
1.1 KiB
Scala
40 lines
1.1 KiB
Scala
/**
|
|
* Copyright (C) 2016 Lightbend Inc. <http://www.lightbend.com/>
|
|
*/
|
|
package docs.stream.cookbook
|
|
|
|
import java.io.ByteArrayOutputStream
|
|
import java.nio.charset.StandardCharsets
|
|
import java.util.zip.GZIPOutputStream
|
|
|
|
import akka.stream.impl.io.compression.GzipCompressor
|
|
import akka.stream.scaladsl.Sink
|
|
import akka.stream.scaladsl.Source
|
|
import akka.util.ByteString
|
|
|
|
import scala.annotation.tailrec
|
|
import scala.concurrent.Await
|
|
import scala.concurrent.duration._
|
|
|
|
class RecipeDecompress extends RecipeSpec {
|
|
def gzip(s: String): ByteString = {
|
|
val buf = new ByteArrayOutputStream()
|
|
val out = new GZIPOutputStream(buf)
|
|
try out.write(s.getBytes(StandardCharsets.UTF_8)) finally out.close()
|
|
ByteString(buf.toByteArray)
|
|
}
|
|
|
|
"Recipe for decompressing a Gzip stream" must {
|
|
"work" in {
|
|
val compressed = Source.single(gzip("Hello World"))
|
|
|
|
//#decompress-gzip
|
|
import akka.stream.scaladsl.Compression
|
|
val uncompressed = compressed.via(Compression.gunzip())
|
|
.map(_.utf8String)
|
|
//#decompress-gzip
|
|
|
|
Await.result(uncompressed.runWith(Sink.head), 3.seconds) should be("Hello World")
|
|
}
|
|
}
|
|
}
|