2015-12-08 18:47:58 +01:00
|
|
|
/**
|
2016-01-25 10:16:14 +01:00
|
|
|
* Copyright (C) 2009-2016 Typesafe Inc. <http://www.typesafe.com>
|
2015-12-08 18:47:58 +01:00
|
|
|
*/
|
|
|
|
|
package akka.stream.scaladsl
|
|
|
|
|
|
2016-02-11 17:04:29 +03:00
|
|
|
import java.io.File
|
|
|
|
|
import java.nio.file.StandardOpenOption
|
|
|
|
|
import java.nio.file.StandardOpenOption._
|
2015-12-08 18:47:58 +01:00
|
|
|
|
|
|
|
|
import akka.stream.ActorAttributes
|
|
|
|
|
import akka.stream.impl.Stages.DefaultAttributes
|
|
|
|
|
import akka.stream.impl.io._
|
2016-02-16 16:56:06 +01:00
|
|
|
import akka.stream.IOResult
|
2015-12-08 18:47:58 +01:00
|
|
|
import akka.util.ByteString
|
|
|
|
|
|
|
|
|
|
import scala.concurrent.Future
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Java API: Factories to create sinks and sources from files
|
|
|
|
|
*/
|
|
|
|
|
object FileIO {
|
|
|
|
|
|
|
|
|
|
import Sink.{ shape ⇒ sinkShape }
|
2016-02-11 17:04:29 +03:00
|
|
|
import Source.{ shape ⇒ sourceShape }
|
2015-12-08 18:47:58 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a Source from a Files contents.
|
|
|
|
|
* Emitted elements are `chunkSize` sized [[akka.util.ByteString]] elements,
|
|
|
|
|
* except the final element, which will be up to `chunkSize` in size.
|
|
|
|
|
*
|
|
|
|
|
* You can configure the default dispatcher for this Source by changing the `akka.stream.blocking-io-dispatcher` or
|
|
|
|
|
* set it for a given Source by using [[ActorAttributes]].
|
|
|
|
|
*
|
2016-01-21 18:06:42 +02:00
|
|
|
* It materializes a [[Future]] of [[IOResult]] containing the number of bytes read from the source file upon completion,
|
|
|
|
|
* and a possible exception if IO operation was not completed successfully.
|
2015-12-08 18:47:58 +01:00
|
|
|
*
|
2016-02-11 17:04:29 +03:00
|
|
|
* @param f the File to read from
|
2015-12-08 18:47:58 +01:00
|
|
|
* @param chunkSize the size of each read operation, defaults to 8192
|
|
|
|
|
*/
|
2016-01-21 18:06:42 +02:00
|
|
|
def fromFile(f: File, chunkSize: Int = 8192): Source[ByteString, Future[IOResult]] =
|
2015-12-08 18:47:58 +01:00
|
|
|
new Source(new FileSource(f, chunkSize, DefaultAttributes.fileSource, sourceShape("FileSource")))
|
|
|
|
|
|
|
|
|
|
/**
|
2016-02-11 17:04:29 +03:00
|
|
|
* Creates a Sink which writes incoming [[ByteString]] elements to the given file. Overwrites existing files by default.
|
2015-12-08 18:47:58 +01:00
|
|
|
*
|
2016-01-21 18:06:42 +02:00
|
|
|
* Materializes a [[Future]] of [[IOResult]] that will be completed with the size of the file (in bytes) at the streams completion,
|
|
|
|
|
* and a possible exception if IO operation was not completed successfully.
|
2015-12-08 18:47:58 +01:00
|
|
|
*
|
|
|
|
|
* This source is backed by an Actor which will use the dedicated `akka.stream.blocking-io-dispatcher`,
|
|
|
|
|
* unless configured otherwise by using [[ActorAttributes]].
|
2016-02-11 17:04:29 +03:00
|
|
|
*
|
|
|
|
|
* @param f the File to write to
|
|
|
|
|
* @param options File open options, defaults to Set(WRITE, CREATE)
|
2015-12-08 18:47:58 +01:00
|
|
|
*/
|
2016-02-11 17:04:29 +03:00
|
|
|
def toFile(f: File, options: Set[StandardOpenOption] = Set(WRITE, CREATE)): Sink[ByteString, Future[IOResult]] =
|
|
|
|
|
new Sink(new FileSink(f, options, DefaultAttributes.fileSink, sinkShape("FileSink")))
|
2016-01-21 18:06:42 +02:00
|
|
|
}
|