@Test public void parseLines() throws Exception { final Source<ByteString, NotUsed> compressed = Source.single(gzip("Hello World")); // #decompress-gzip final Source<String, NotUsed> uncompressed = compressed.via(Compression.gunzip(100)).map(b -> b.utf8String()); // #decompress-gzip uncompressed.runWith(Sink.head(), mat).toCompletableFuture().get(1, TimeUnit.SECONDS); }
public void streamFile() throws IOException, FileNotFoundException, InterruptedException, ExecutionException { String url = "http://example.com"; // #stream-to-file File file = File.createTempFile("stream-to-file-", ".txt"); FileOutputStream outputStream = new FileOutputStream(file); // Make the request CompletionStage<StreamedResponse> futureResponse = ws.url(url).setMethod("GET").stream(); CompletionStage<File> downloadedFile = futureResponse.thenCompose( res -> { Source<ByteString, ?> responseBody = res.getBody(); // The sink that writes to the output stream Sink<ByteString, scala.concurrent.Future<scala.runtime.BoxedUnit>> outputWriter = Sink.<ByteString>foreach(bytes -> outputStream.write(bytes.toArray())); // Converts the materialized Scala Future into a Java8 `CompletionStage` Sink<ByteString, CompletionStage<?>> convertedOutputWriter = outputWriter.mapMaterializedValue(FutureConverters::toJava); // materialize and run the stream CompletionStage<File> result = responseBody .runWith(convertedOutputWriter, materializer) .whenComplete( (value, error) -> { // Close the output stream whether there was an error or not try { outputStream.close(); } catch (IOException e) { } }) .thenApply(v -> file); return result; }); // #stream-to-file downloadedFile.toCompletableFuture().get(); file.delete(); }
@Test public void demonstrateFilterAndMap() { final SilenceSystemOut.System System = SilenceSystemOut.get(); // #first-sample // #authors-filter-map final Source<Author, NotUsed> authors = tweets.filter(t -> t.hashtags().contains(AKKA)).map(t -> t.author); // #first-sample // #authors-filter-map new Object() { // #authors-collect JavaPartialFunction<Tweet, Author> collectFunction = new JavaPartialFunction<Tweet, Author>() { public Author apply(Tweet t, boolean isCheck) { if (t.hashtags().contains(AKKA)) { if (isCheck) return null; // to spare the expensive or side-effecting code return t.author; } else { throw noMatch(); } } }; final Source<Author, NotUsed> authors = tweets.collect(collectFunction); // #authors-collect }; // #first-sample // #authors-foreachsink-println authors.runWith(Sink.foreach(a -> System.out.println(a)), mat); // #first-sample // #authors-foreachsink-println // #authors-foreach-println authors.runForeach(a -> System.out.println(a), mat); // #authors-foreach-println }
public void streamSimpleRequest() { String url = "http://example.com"; // #stream-count-bytes // Make the request CompletionStage<StreamedResponse> futureResponse = ws.url(url).setMethod("GET").stream(); CompletionStage<Long> bytesReturned = futureResponse.thenCompose( res -> { Source<ByteString, ?> responseBody = res.getBody(); // Count the number of bytes returned Sink<ByteString, scala.concurrent.Future<Long>> bytesSum = Sink.fold(0L, (total, bytes) -> total + bytes.length()); // Converts the materialized Scala Future into a Java8 `CompletionStage` Sink<ByteString, CompletionStage<Long>> convertedBytesSum = bytesSum.mapMaterializedValue(FutureConverters::toJava); return responseBody.runWith(convertedBytesSum, materializer); }); // #stream-count-bytes }