pekko/akka-docs/rst/scala/code/docs/stream/cookbook/RecipeParseLines.scala

40 lines
972 B
Scala
Raw Normal View History

2014-12-08 17:29:40 +01:00
package docs.stream.cookbook
2014-12-22 16:18:26 +01:00
import akka.stream.scaladsl.Sink
import akka.stream.scaladsl.Source
2014-12-08 17:29:40 +01:00
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")))
2015-05-05 15:02:11 +02:00
//#parse-lines
import akka.stream.io.Framing
2015-07-08 10:23:10 +03:00
val linesStream = rawData.via(Framing.delimiter(
ByteString("\r\n"), maximumFrameLength = 100, allowTruncation = true))
.map(_.utf8String)
2015-05-05 15:02:11 +02:00
//#parse-lines
2014-12-08 17:29:40 +01:00
Await.result(linesStream.limit(10).runWith(Sink.seq), 3.seconds) should be(List(
2014-12-08 17:29:40 +01:00
"Hello World\r!",
"Hello Akka!",
"Hello Streams!",
""))
}
}
}