2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2016-2019 Lightbend Inc. <https://www.lightbend.com>
|
2016-11-17 23:42:37 +02:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.stream.javadsl.cookbook;
|
2016-11-17 23:42:37 +02:00
|
|
|
|
|
|
|
|
import akka.NotUsed;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.stream.javadsl.Compression;
|
|
|
|
|
import akka.stream.javadsl.Source;
|
2017-03-17 03:02:47 +08:00
|
|
|
import akka.testkit.javadsl.TestKit;
|
2016-11-17 23:42:37 +02:00
|
|
|
import akka.util.ByteString;
|
2019-05-16 14:02:49 +02:00
|
|
|
import static akka.util.ByteString.emptyByteString;
|
2016-11-17 23:42:37 +02:00
|
|
|
import org.junit.AfterClass;
|
2017-12-05 16:46:54 +01:00
|
|
|
import org.junit.Assert;
|
2016-11-17 23:42:37 +02:00
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
public class RecipeDecompress extends RecipeTest {
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
static ActorSystem system;
|
|
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
|
public static void setup() {
|
|
|
|
|
system = ActorSystem.create("RecipeDecompress");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
|
public static void tearDown() {
|
|
|
|
|
TestKit.shutdownActorSystem(system);
|
|
|
|
|
system = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void parseLines() throws Exception {
|
|
|
|
|
final Source<ByteString, NotUsed> dataStream =
|
|
|
|
|
Source.single(ByteString.fromString("Hello World"));
|
|
|
|
|
|
|
|
|
|
final Source<ByteString, NotUsed> compressedStream = dataStream.via(Compression.gzip());
|
|
|
|
|
|
|
|
|
|
// #decompress-gzip
|
|
|
|
|
final Source<ByteString, NotUsed> decompressedStream =
|
|
|
|
|
compressedStream.via(Compression.gunzip(100));
|
|
|
|
|
// #decompress-gzip
|
|
|
|
|
|
|
|
|
|
ByteString decompressedData =
|
|
|
|
|
decompressedStream
|
2019-08-23 18:19:27 +02:00
|
|
|
.runFold(emptyByteString(), ByteString::concat, system)
|
2019-01-12 04:00:53 +08:00
|
|
|
.toCompletableFuture()
|
|
|
|
|
.get(1, TimeUnit.SECONDS);
|
|
|
|
|
String decompressedString = decompressedData.utf8String();
|
|
|
|
|
Assert.assertEquals("Hello World", decompressedString);
|
|
|
|
|
}
|
2016-11-17 23:42:37 +02:00
|
|
|
}
|