* Sink.seq (Scala DSL) now returns immutable.Seq rather than Seq * Sink.seq will not silently truncate when incoming elements is > Int.MAX_VALUE * minor doc fixes * replacing various grouped(n) / Sink.head with limit(n) / Sink.seq in various tests * fix inconsistent indentation in RequestParserSpec
39 lines
972 B
Scala
39 lines
972 B
Scala
package docs.stream.cookbook
|
|
|
|
import akka.stream.scaladsl.Sink
|
|
import akka.stream.scaladsl.Source
|
|
import akka.util.ByteString
|
|
|
|
import scala.annotation.tailrec
|
|
import scala.concurrent.Await
|
|
import scala.concurrent.duration._
|
|
|
|
class RecipeParseLines extends RecipeSpec {
|
|
|
|
"Recipe for parsing line from bytes" must {
|
|
|
|
"work" in {
|
|
val rawData = Source(List(
|
|
ByteString("Hello World"),
|
|
ByteString("\r"),
|
|
ByteString("!\r"),
|
|
ByteString("\nHello Akka!\r\nHello Streams!"),
|
|
ByteString("\r\n\r\n")))
|
|
|
|
//#parse-lines
|
|
import akka.stream.io.Framing
|
|
val linesStream = rawData.via(Framing.delimiter(
|
|
ByteString("\r\n"), maximumFrameLength = 100, allowTruncation = true))
|
|
.map(_.utf8String)
|
|
//#parse-lines
|
|
|
|
Await.result(linesStream.limit(10).runWith(Sink.seq), 3.seconds) should be(List(
|
|
"Hello World\r!",
|
|
"Hello Akka!",
|
|
"Hello Streams!",
|
|
""))
|
|
}
|
|
|
|
}
|
|
|
|
}
|