!str - 18916 - Source.file and Sink.file
* Removes `Synchronous` from the names and descriptions of File I/O as it leaks impl details * Removes the factries for FileSource and FileSink and puts them in Source and Sink respectively
This commit is contained in:
parent
7d4304fc6e
commit
20c996fe41
31 changed files with 262 additions and 242 deletions
|
|
@ -1,5 +1,7 @@
|
|||
package docs
|
||||
|
||||
import java.io.File
|
||||
|
||||
import akka.http.scaladsl.model.Uri
|
||||
import akka.stream.scaladsl._
|
||||
import akka.stream._
|
||||
|
|
@ -208,6 +210,14 @@ class MigrationsScala extends AkkaSpec {
|
|||
//#query-param
|
||||
val param: Option[String] = uri.query().get("a")
|
||||
//#query-param
|
||||
|
||||
//#file-source-sink
|
||||
val fileSrc = Source.file(new File("."))
|
||||
|
||||
val otherFileSrc = Source.file(new File("."), 1024)
|
||||
|
||||
val someFileSink = Sink.file(new File("."))
|
||||
//#file-source-sink
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ import akka.http.scaladsl.model.headers.{ Server, RawHeader }
|
|||
import akka.http.scaladsl.server.RouteResult.{ Complete, Rejected }
|
||||
import akka.http.scaladsl.server._
|
||||
import akka.stream.ActorMaterializer
|
||||
import akka.stream.io.SynchronousFileSource
|
||||
import akka.stream.scaladsl.{ Sink, Source }
|
||||
import akka.util.ByteString
|
||||
|
||||
|
|
@ -174,7 +173,7 @@ class BasicDirectivesExamplesSpec extends RoutingSpec {
|
|||
path("sample") {
|
||||
complete {
|
||||
// internally uses the configured fileIODispatcher:
|
||||
val source = SynchronousFileSource(new File("example.json"))
|
||||
val source = Source.file(new File("example.json"))
|
||||
HttpResponse(entity = HttpEntity(ContentTypes.`application/json`, source))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ package docs.stream.io
|
|||
import java.io.File
|
||||
|
||||
import akka.stream._
|
||||
import akka.stream.io.SynchronousFileSink
|
||||
import akka.stream.io.SynchronousFileSource
|
||||
import akka.stream.scaladsl.Sink
|
||||
import akka.stream.scaladsl.Source
|
||||
import akka.stream.testkit.Utils._
|
||||
|
|
@ -48,7 +46,7 @@ class StreamFileDocSpec extends AkkaSpec(UnboundedMailboxConfig) {
|
|||
|
||||
//#file-source
|
||||
|
||||
val foreach: Future[Long] = SynchronousFileSource(file)
|
||||
val foreach: Future[Long] = Source.file(file)
|
||||
.to(Sink.ignore)
|
||||
.run()
|
||||
//#file-source
|
||||
|
|
@ -56,16 +54,8 @@ class StreamFileDocSpec extends AkkaSpec(UnboundedMailboxConfig) {
|
|||
|
||||
"configure dispatcher in code" in {
|
||||
//#custom-dispatcher-code
|
||||
SynchronousFileSink(file)
|
||||
Sink.file(file)
|
||||
.withAttributes(ActorAttributes.dispatcher("custom-blocking-io-dispatcher"))
|
||||
//#custom-dispatcher-code
|
||||
}
|
||||
|
||||
"show Implicits" in {
|
||||
//#source-sink-implicits
|
||||
import akka.stream.io.Implicits._
|
||||
|
||||
Source.synchronousFile(file) to Sink.outputStream(() ⇒ System.out)
|
||||
//#source-sink-implicits
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,10 +24,10 @@ To serve files from a classpath directory use :ref:`-getFromResourceDirectory-`
|
|||
Note that it's not required to wrap this directive with ``get`` as this directive will only respond to ``GET`` requests.
|
||||
|
||||
.. note::
|
||||
The file's contents will be read using an Akka Streams :class:`SynchronousFileSource` which *automatically uses
|
||||
The file's contents will be read using an Akka Streams `Source` which *automatically uses
|
||||
a pre-configured dedicated blocking io dispatcher*, which separates the blocking file operations from the rest of the stream.
|
||||
|
||||
Note also that thanks to using Akka Streams internally, the file will be served at the highest spead reachable by
|
||||
Note also that thanks to using Akka Streams internally, the file will be served at the highest speed reachable by
|
||||
the client, and not faster – i.e. the file will *not* end up being loaded in full into memory before writing it to
|
||||
the client.
|
||||
|
||||
|
|
|
|||
|
|
@ -24,10 +24,10 @@ To serve files from a classpath directory use :ref:`-getFromResourceDirectory-`
|
|||
Note that it's not required to wrap this directive with ``get`` as this directive will only respond to ``GET`` requests.
|
||||
|
||||
.. note::
|
||||
The file's contents will be read using an Akka Streams :class:`SynchronousFileSource` which *automatically uses
|
||||
The file's contents will be read using an Akka Streams `Source` which *automatically uses
|
||||
a pre-configured dedicated blocking io dispatcher*, which separates the blocking file operations from the rest of the stream.
|
||||
|
||||
Note also that thanks to using Akka Streams internally, the file will be served at the highest spead reachable by
|
||||
Note also that thanks to using Akka Streams internally, the file will be served at the highest speed reachable by
|
||||
the client, and not faster – i.e. the file will *not* end up being loaded in full into memory before writing it to
|
||||
the client.
|
||||
|
||||
|
|
|
|||
|
|
@ -450,3 +450,34 @@ And use of query parameters from ``Uri`` that looked like this:
|
|||
should be replaced by:
|
||||
|
||||
.. includecode:: code/docs/MigrationsScala.scala#query-param
|
||||
|
||||
SynchronousFileSource and SynchronousFileSink
|
||||
============================================
|
||||
|
||||
Both have been replaced by `Source.file(…)` and `Sink.file(…)` due to discoverability issues
|
||||
paired with names which leaked internal implementation details.
|
||||
|
||||
Update procedure
|
||||
----------------
|
||||
|
||||
Replace `SynchronousFileSource(` and `SynchronousFileSource.apply(` with `Source.file(`
|
||||
|
||||
Replace `SynchronousFileSink(` and `SynchronousFileSink.apply(` with `Sink.file(`
|
||||
|
||||
Example
|
||||
^^^^^^^
|
||||
|
||||
::
|
||||
|
||||
// This no longer works!
|
||||
val fileSrc = SynchronousFileSource(new File("."))
|
||||
|
||||
// This no longer works!
|
||||
val otherFileSrc = SynchronousFileSource(new File("."), 1024)
|
||||
|
||||
// This no longer works!
|
||||
val someFileSink = SynchronousFileSink(new File("."))
|
||||
|
||||
should be replaced by
|
||||
|
||||
.. includecode:: code/docs/MigrationsScala.scala#file-source-sink
|
||||
|
|
|
|||
|
|
@ -110,7 +110,7 @@ on files.
|
|||
Once Akka is free to require JDK8 (from ``2.4.x``) these implementations will be updated to make use of the
|
||||
new NIO APIs (i.e. :class:`AsynchronousFileChannel`).
|
||||
|
||||
Streaming data from a file is as easy as defining a `SynchronousFileSource` given a target file, and an optional
|
||||
Streaming data from a file is as easy as creating a `Source.file` given a target file, and an optional
|
||||
``chunkSize`` which determines the buffer size determined as one "element" in such stream:
|
||||
|
||||
.. includecode:: code/docs/stream/io/StreamFileDocSpec.scala#file-source
|
||||
|
|
@ -122,9 +122,3 @@ dispatcher for file IO operations globally, you can do so by changing the ``akka
|
|||
or for a specific stage by specifying a custom Dispatcher in code, like this:
|
||||
|
||||
.. includecode:: code/docs/stream/io/StreamFileDocSpec.scala#custom-dispatcher-code
|
||||
|
||||
If you would like to keep all sink and source factories defined on the :class:`Source` and :class:`Sink` objects
|
||||
instead of using the separate objects contained in ``akka.stream.io`` to create these you can import an *implicit
|
||||
coversion* that makes these operations available as shown below:
|
||||
|
||||
.. includecode:: code/docs/stream/io/StreamFileDocSpec.scala#source-sink-implicits
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue