2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2015-2019 Lightbend Inc. <https://www.lightbend.com>
|
2016-01-13 16:25:24 +01:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.stream.io;
|
2016-01-13 16:25:24 +01:00
|
|
|
|
2016-04-25 19:25:26 +10:00
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.Paths;
|
2016-01-13 16:25:24 +01:00
|
|
|
import java.io.IOException;
|
2016-01-21 16:37:26 +01:00
|
|
|
import java.util.concurrent.CompletionStage;
|
2016-01-13 16:25:24 +01:00
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
import akka.Done;
|
2018-09-24 23:50:27 +08:00
|
|
|
import akka.NotUsed;
|
2016-01-13 16:25:24 +01:00
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.stream.ActorAttributes;
|
|
|
|
|
import akka.stream.javadsl.Sink;
|
|
|
|
|
import akka.stream.javadsl.FileIO;
|
2018-09-24 23:50:27 +08:00
|
|
|
import akka.stream.javadsl.Source;
|
2017-03-16 09:30:00 +01:00
|
|
|
import jdocs.AbstractJavaTest;
|
|
|
|
|
import jdocs.stream.SilenceSystemOut;
|
2017-03-17 03:02:47 +08:00
|
|
|
import akka.testkit.javadsl.TestKit;
|
2016-01-13 16:25:24 +01:00
|
|
|
import org.junit.AfterClass;
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import akka.stream.*;
|
|
|
|
|
import akka.util.ByteString;
|
|
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
public class StreamFileDocTest extends AbstractJavaTest {
|
2016-01-13 16:25:24 +01:00
|
|
|
|
|
|
|
|
static ActorSystem system;
|
|
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
static Materializer mat;
|
|
|
|
|
|
2016-01-13 16:25:24 +01:00
|
|
|
@BeforeClass
|
|
|
|
|
public static void setup() {
|
|
|
|
|
system = ActorSystem.create("StreamFileDocTest");
|
2016-02-11 16:39:25 +01:00
|
|
|
mat = ActorMaterializer.create(system);
|
2016-01-13 16:25:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
|
public static void tearDown() {
|
2017-03-17 03:02:47 +08:00
|
|
|
TestKit.shutdownActorSystem(system);
|
2016-01-13 16:25:24 +01:00
|
|
|
system = null;
|
2016-02-11 16:39:25 +01:00
|
|
|
mat = null;
|
2016-01-13 16:25:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final SilenceSystemOut.System System = SilenceSystemOut.get();
|
|
|
|
|
|
|
|
|
|
{
|
2018-09-24 23:50:27 +08:00
|
|
|
// Using 4 spaces here to align with code in try block below.
|
|
|
|
|
//#file-source
|
|
|
|
|
final Path file = Paths.get("example.csv");
|
|
|
|
|
//#file-source
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
//#file-sink
|
|
|
|
|
final Path file = Paths.get("greeting.txt");
|
|
|
|
|
//#file-sink
|
2016-01-13 16:25:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void demonstrateMaterializingBytesWritten() throws IOException {
|
2016-04-25 19:25:26 +10:00
|
|
|
final Path file = Files.createTempFile(getClass().getName(), ".tmp");
|
2016-01-13 16:25:24 +01:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
//#file-source
|
2016-01-21 16:37:26 +01:00
|
|
|
Sink<ByteString, CompletionStage<Done>> printlnSink =
|
|
|
|
|
Sink.<ByteString> foreach(chunk -> System.out.println(chunk.utf8String()));
|
2016-01-13 16:25:24 +01:00
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
CompletionStage<IOResult> ioResult =
|
2016-04-25 19:25:26 +10:00
|
|
|
FileIO.fromPath(file)
|
2016-01-13 16:25:24 +01:00
|
|
|
.to(printlnSink)
|
|
|
|
|
.run(mat);
|
|
|
|
|
//#file-source
|
|
|
|
|
} finally {
|
2016-04-25 19:25:26 +10:00
|
|
|
Files.delete(file);
|
2016-01-13 16:25:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void demonstrateSettingDispatchersInCode() throws IOException {
|
2016-04-25 19:25:26 +10:00
|
|
|
final Path file = Files.createTempFile(getClass().getName(), ".tmp");
|
2016-01-13 16:25:24 +01:00
|
|
|
|
|
|
|
|
try {
|
2016-01-21 16:37:26 +01:00
|
|
|
Sink<ByteString, CompletionStage<IOResult>> fileSink =
|
2016-01-13 16:25:24 +01:00
|
|
|
//#custom-dispatcher-code
|
2016-04-25 19:25:26 +10:00
|
|
|
FileIO.toPath(file)
|
2016-01-13 16:25:24 +01:00
|
|
|
.withAttributes(ActorAttributes.dispatcher("custom-blocking-io-dispatcher"));
|
|
|
|
|
//#custom-dispatcher-code
|
|
|
|
|
} finally {
|
2016-04-25 19:25:26 +10:00
|
|
|
Files.delete(file);
|
2016-01-13 16:25:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-24 23:50:27 +08:00
|
|
|
@Test
|
|
|
|
|
public void demontrateFileIOWriting() throws IOException {
|
|
|
|
|
final Path file = Files.createTempFile(getClass().getName(), ".tmp");
|
2016-01-13 16:25:24 +01:00
|
|
|
|
2018-09-24 23:50:27 +08:00
|
|
|
try {
|
|
|
|
|
//#file-sink
|
|
|
|
|
Sink<ByteString, CompletionStage<IOResult>> fileSink = FileIO.toPath(file);
|
|
|
|
|
Source<String, NotUsed> textSource = Source.single("Hello Akka Stream!");
|
|
|
|
|
|
|
|
|
|
CompletionStage<IOResult> ioResult = textSource
|
|
|
|
|
.map(ByteString::fromString)
|
|
|
|
|
.runWith(fileSink, mat);
|
|
|
|
|
//#file-sink
|
|
|
|
|
} finally {
|
|
|
|
|
Files.delete(file);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-13 16:25:24 +01:00
|
|
|
}
|