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
|
|
|
|
2015-03-05 12:21:17 +01:00
|
|
|
Await.result(linesStream.grouped(10).runWith(Sink.head), 3.seconds) should be(List(
|
2014-12-08 17:29:40 +01:00
|
|
|
"Hello World\r!",
|
|
|
|
|
"Hello Akka!",
|
|
|
|
|
"Hello Streams!",
|
|
|
|
|
""))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|