2023-01-08 17:13:31 +08:00
|
|
|
/*
|
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
* license agreements; and to You under the Apache License, version 2.0:
|
|
|
|
|
*
|
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* This file is part of the Apache Pekko project, derived from Akka.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2022-02-04 12:36:44 +01:00
|
|
|
* Copyright (C) 2015-2022 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
|
|
|
|
2022-11-12 10:21:24 +01:00
|
|
|
import org.apache.pekko.Done;
|
|
|
|
|
import org.apache.pekko.NotUsed;
|
|
|
|
|
import org.apache.pekko.actor.ActorSystem;
|
|
|
|
|
import org.apache.pekko.stream.ActorAttributes;
|
|
|
|
|
import org.apache.pekko.stream.javadsl.Sink;
|
|
|
|
|
import org.apache.pekko.stream.javadsl.FileIO;
|
|
|
|
|
import org.apache.pekko.stream.javadsl.Source;
|
2017-03-16 09:30:00 +01:00
|
|
|
import jdocs.AbstractJavaTest;
|
|
|
|
|
import jdocs.stream.SilenceSystemOut;
|
2022-11-12 10:21:24 +01:00
|
|
|
import org.apache.pekko.testkit.javadsl.TestKit;
|
2016-01-13 16:25:24 +01:00
|
|
|
import org.junit.AfterClass;
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
2022-11-12 10:21:24 +01:00
|
|
|
import org.apache.pekko.stream.*;
|
|
|
|
|
import org.apache.pekko.util.ByteString;
|
2016-01-13 16:25:24 +01:00
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
public class StreamFileDocTest extends AbstractJavaTest {
|
2016-01-13 16:25:24 +01:00
|
|
|
|
|
|
|
|
static ActorSystem system;
|
|
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
|
public static void setup() {
|
|
|
|
|
system = ActorSystem.create("StreamFileDocTest");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final SilenceSystemOut.System System = SilenceSystemOut.get();
|
|
|
|
|
|
|
|
|
|
{
|
2019-01-12 04:00:53 +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
|
2018-09-24 23:50:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2019-01-12 04:00:53 +08:00
|
|
|
// #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 {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #file-source
|
2016-01-21 16:37:26 +01:00
|
|
|
Sink<ByteString, CompletionStage<Done>> printlnSink =
|
2019-01-12 04:00:53 +08:00
|
|
|
Sink.<ByteString>foreach(chunk -> System.out.println(chunk.utf8String()));
|
2016-01-13 16:25:24 +01:00
|
|
|
|
2019-08-23 18:19:27 +02:00
|
|
|
CompletionStage<IOResult> ioResult = FileIO.fromPath(file).to(printlnSink).run(system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// #file-source
|
2016-01-13 16:25:24 +01:00
|
|
|
} 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 =
|
2019-01-12 04:00:53 +08:00
|
|
|
// #custom-dispatcher-code
|
|
|
|
|
FileIO.toPath(file)
|
|
|
|
|
.withAttributes(ActorAttributes.dispatcher("custom-blocking-io-dispatcher"));
|
|
|
|
|
// #custom-dispatcher-code
|
2016-01-13 16:25:24 +01:00
|
|
|
} 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 {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #file-sink
|
2018-09-24 23:50:27 +08:00
|
|
|
Sink<ByteString, CompletionStage<IOResult>> fileSink = FileIO.toPath(file);
|
2023-01-18 08:13:01 +01:00
|
|
|
Source<String, NotUsed> textSource = Source.single("Hello Pekko Stream!");
|
2018-09-24 23:50:27 +08:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
CompletionStage<IOResult> ioResult =
|
2019-08-23 18:19:27 +02:00
|
|
|
textSource.map(ByteString::fromString).runWith(fileSink, system);
|
2019-01-12 04:00:53 +08:00
|
|
|
// #file-sink
|
2018-09-24 23:50:27 +08:00
|
|
|
} finally {
|
|
|
|
|
Files.delete(file);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-13 16:25:24 +01:00
|
|
|
}
|