{"action":"created","comment":{"url":"https://api.github.com/repos/akka/akka/pulls/comments/30110152","id":30110152,"diff_hunk":"@@ -0,0 +1,182 @@\n+package akka.stream.io\n+\n+import java.nio.ByteOrder\n+\n+import akka.stream.{ ActorFlowMaterializer, ActorFlowMaterializerSettings }\n+import akka.stream.scaladsl.{ Sink, Source, Flow }\n+import akka.stream.stage.{ TerminationDirective, SyncDirective, Context, PushPullStage }\n+import akka.stream.testkit.AkkaSpec\n+import akka.util.{ ByteString, ByteStringBuilder }\n+\n+import scala.collection.immutable\n+import scala.concurrent.Await\n+import scala.concurrent.duration._\n+import scala.concurrent.forkjoin.ThreadLocalRandom\n+\n+class FramingSpec extends AkkaSpec {\n+\n+  val settings = ActorFlowMaterializerSettings(system)\n+  implicit val materializer = ActorFlowMaterializer(settings)\n+\n+  class Rechunker extends PushPullStage[ByteString, ByteString] {\n+    private var rechunkBuffer = ByteString.empty\n+\n+    override def onPush(chunk: ByteString, ctx: Context[ByteString]): SyncDirective = {\n+      rechunkBuffer ++= chunk\n+      rechunk(ctx)\n+    }\n+\n+    override def onPull(ctx: Context[ByteString]): SyncDirective = {\n+      rechunk(ctx)\n+    }\n+\n+    override def onUpstreamFinish(ctx: Context[ByteString]): TerminationDirective = {\n+      if (rechunkBuffer.isEmpty) ctx.finish()\n+      else ctx.absorbTermination()\n+    }\n+\n+    private def rechunk(ctx: Context[ByteString]): SyncDirective = {\n+      if (!ctx.isFinishing && ThreadLocalRandom.current().nextBoolean()) ctx.pull()\n+      else {\n+        val nextChunkSize =\n+          if (rechunkBuffer.isEmpty) 0\n+          else ThreadLocalRandom.current().nextInt(0, rechunkBuffer.size + 1)\n+        val newChunk = rechunkBuffer.take(nextChunkSize)\n+        rechunkBuffer = rechunkBuffer.drop(nextChunkSize)\n+        if (ctx.isFinishing && rechunkBuffer.isEmpty) ctx.pushAndFinish(newChunk)\n+        else ctx.push(newChunk)\n+      }\n+    }\n+  }\n+\n+  val rechunk = Flow[ByteString].transform(() ⇒ new Rechunker).named(\"rechunker\")\n+\n+  \"Delimiter bytes based framing\" must {\n+\n+    val delimiterBytes = List(\"\\n\", \"\\r\\n\", \"FOO\").map(ByteString(_))\n+    val baseTestSequences = List(\"\", \"foo\", \"hello world\").map(ByteString(_))\n+\n+    def completeTestSequences(delimiter: ByteString): immutable.Iterable[ByteString] =\n+      for (prefix ← 0 until delimiter.size; s ← baseTestSequences)\n+        yield delimiter.take(prefix) ++ s\n+\n+    \"work with various delimiters and test sequences\" in {\n+      for (delimiter ← delimiterBytes; _ ← 1 to 100) {\n+        val f = Source(completeTestSequences(delimiter))\n+          .map(_ ++ delimiter)\n+          .via(rechunk)\n+          .via(Framing.delimiter(delimiter, 256))\n+          .grouped(1000)\n+          .runWith(Sink.head)\n+\n+        Await.result(f, 3.seconds) should be(completeTestSequences(delimiter))\n+      }\n+    }\n+\n+    \"Respect maximum line settings\" in {\n+      // The buffer will contain more than 1 bytes, but the individual frames are less\n+      Await.result(\n+        Source.single(ByteString(\"a\\nb\\nc\\nd\\n\")).via(Framing.lines(\"\\n\", 1)).grouped(100).runWith(Sink.head),\n+        3.seconds) should ===(List(\"a\", \"b\", \"c\", \"d\"))\n+\n+      an[IllegalStateException] should be thrownBy {","path":"akka-stream-tests/src/test/scala/akka/stream/io/FramingSpec.scala","position":82,"original_position":82,"commit_id":"1055c513fc74b61a8651cf01b1fa700f79608da4","original_commit_id":"1055c513fc74b61a8651cf01b1fa700f79608da4","user":{"login":"rkuhn","id":470469,"avatar_url":"https://avatars.githubusercontent.com/u/470469?v=3","gravatar_id":"","url":"https://api.github.com/users/rkuhn","html_url":"https://github.com/rkuhn","followers_url":"https://api.github.com/users/rkuhn/followers","following_url":"https://api.github.com/users/rkuhn/following{/other_user}","gists_url":"https://api.github.com/users/rkuhn/gists{/gist_id}","starred_url":"https://api.github.com/users/rkuhn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rkuhn/subscriptions","organizations_url":"https://api.github.com/users/rkuhn/orgs","repos_url":"https://api.github.com/users/rkuhn/repos","events_url":"https://api.github.com/users/rkuhn/events{/privacy}","received_events_url":"https://api.github.com/users/rkuhn/received_events","type":"User","site_admin":false},"body":"My point was just that I needed to write down the difference between these two exception types for my own sanity’s sake—I think any kind of parsing error should be its own exception type (there might already be some data format errors defined in the JDK).","created_at":"2015-05-12T07:26:03Z","updated_at":"2015-05-12T07:26:03Z","html_url":"https://github.com/akka/akka/pull/17446#discussion_r30110152","pull_request_url":"https://api.github.com/repos/akka/akka/pulls/17446","_links":{"self":{"href":"https://api.github.com/repos/akka/akka/pulls/comments/30110152"},"html":{"href":"https://github.com/akka/akka/pull/17446#discussion_r30110152"},"pull_request":{"href":"https://api.github.com/repos/akka/akka/pulls/17446"}}},"pull_request":{"url":"https://api.github.com/repos/akka/akka/pulls/17446","id":35121616,"html_url":"https://github.com/akka/akka/pull/17446","diff_url":"https://github.com/akka/akka/pull/17446.diff","patch_url":"https://github.com/akka/akka/pull/17446.patch","issue_url":"https://api.github.com/repos/akka/akka/issues/17446","number":17446,"state":"open","locked":false,"title":"+str #17310: Basic framing support","user":{"login":"drewhk","id":932285,"avatar_url":"https://avatars.githubusercontent.com/u/932285?v=3","gravatar_id":"","url":"https://api.github.com/users/drewhk","html_url":"https://github.com/drewhk","followers_url":"https://api.github.com/users/drewhk/followers","following_url":"https://api.github.com/users/drewhk/following{/other_user}","gists_url":"https://api.github.com/users/drewhk/gists{/gist_id}","starred_url":"https://api.github.com/users/drewhk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drewhk/subscriptions","organizations_url":"https://api.github.com/users/drewhk/orgs","repos_url":"https://api.github.com/users/drewhk/repos","events_url":"https://api.github.com/users/drewhk/events{/privacy}","received_events_url":"https://api.github.com/users/drewhk/received_events","type":"User","site_admin":false},"body":"(Still needs scaladocs and java tests)","created_at":"2015-05-11T12:16:49Z","updated_at":"2015-05-12T07:26:03Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5928f324765260a14047608347023bff3902ecf0","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/akka/akka/pulls/17446/commits","review_comments_url":"https://api.github.com/repos/akka/akka/pulls/17446/comments","review_comment_url":"https://api.github.com/repos/akka/akka/pulls/comments{/number}","comments_url":"https://api.github.com/repos/akka/akka/issues/17446/comments","statuses_url":"https://api.github.com/repos/akka/akka/statuses/1055c513fc74b61a8651cf01b1fa700f79608da4","head":{"label":"drewhk:wip-17310-basic-framing-support-drewhk","ref":"wip-17310-basic-framing-support-drewhk","sha":"1055c513fc74b61a8651cf01b1fa700f79608da4","user":{"login":"drewhk","id":932285,"avatar_url":"https://avatars.githubusercontent.com/u/932285?v=3","gravatar_id":"","url":"https://api.github.com/users/drewhk","html_url":"https://github.com/drewhk","followers_url":"https://api.github.com/users/drewhk/followers","following_url":"https://api.github.com/users/drewhk/following{/other_user}","gists_url":"https://api.github.com/users/drewhk/gists{/gist_id}","starred_url":"https://api.github.com/users/drewhk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drewhk/subscriptions","organizations_url":"https://api.github.com/users/drewhk/orgs","repos_url":"https://api.github.com/users/drewhk/repos","events_url":"https://api.github.com/users/drewhk/events{/privacy}","received_events_url":"https://api.github.com/users/drewhk/received_events","type":"User","site_admin":false},"repo":{"id":4322312,"name":"akka","full_name":"drewhk/akka","owner":{"login":"drewhk","id":932285,"avatar_url":"https://avatars.githubusercontent.com/u/932285?v=3","gravatar_id":"","url":"https://api.github.com/users/drewhk","html_url":"https://github.com/drewhk","followers_url":"https://api.github.com/users/drewhk/followers","following_url":"https://api.github.com/users/drewhk/following{/other_user}","gists_url":"https://api.github.com/users/drewhk/gists{/gist_id}","starred_url":"https://api.github.com/users/drewhk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drewhk/subscriptions","organizations_url":"https://api.github.com/users/drewhk/orgs","repos_url":"https://api.github.com/users/drewhk/repos","events_url":"https://api.github.com/users/drewhk/events{/privacy}","received_events_url":"https://api.github.com/users/drewhk/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/drewhk/akka","description":"Akka Project","fork":true,"url":"https://api.github.com/repos/drewhk/akka","forks_url":"https://api.github.com/repos/drewhk/akka/forks","keys_url":"https://api.github.com/repos/drewhk/akka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/drewhk/akka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/drewhk/akka/teams","hooks_url":"https://api.github.com/repos/drewhk/akka/hooks","issue_events_url":"https://api.github.com/repos/drewhk/akka/issues/events{/number}","events_url":"https://api.github.com/repos/drewhk/akka/events","assignees_url":"https://api.github.com/repos/drewhk/akka/assignees{/user}","branches_url":"https://api.github.com/repos/drewhk/akka/branches{/branch}","tags_url":"https://api.github.com/repos/drewhk/akka/tags","blobs_url":"https://api.github.com/repos/drewhk/akka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/drewhk/akka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/drewhk/akka/git/refs{/sha}","trees_url":"https://api.github.com/repos/drewhk/akka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/drewhk/akka/statuses/{sha}","languages_url":"https://api.github.com/repos/drewhk/akka/languages","stargazers_url":"https://api.github.com/repos/drewhk/akka/stargazers","contributors_url":"https://api.github.com/repos/drewhk/akka/contributors","subscribers_url":"https://api.github.com/repos/drewhk/akka/subscribers","subscription_url":"https://api.github.com/repos/drewhk/akka/subscription","commits_url":"https://api.github.com/repos/drewhk/akka/commits{/sha}","git_commits_url":"https://api.github.com/repos/drewhk/akka/git/commits{/sha}","comments_url":"https://api.github.com/repos/drewhk/akka/comments{/number}","issue_comment_url":"https://api.github.com/repos/drewhk/akka/issues/comments{/number}","contents_url":"https://api.github.com/repos/drewhk/akka/contents/{+path}","compare_url":"https://api.github.com/repos/drewhk/akka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/drewhk/akka/merges","archive_url":"https://api.github.com/repos/drewhk/akka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/drewhk/akka/downloads","issues_url":"https://api.github.com/repos/drewhk/akka/issues{/number}","pulls_url":"https://api.github.com/repos/drewhk/akka/pulls{/number}","milestones_url":"https://api.github.com/repos/drewhk/akka/milestones{/number}","notifications_url":"https://api.github.com/repos/drewhk/akka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/drewhk/akka/labels{/name}","releases_url":"https://api.github.com/repos/drewhk/akka/releases{/id}","created_at":"2012-05-14T09:57:34Z","updated_at":"2015-02-17T12:40:08Z","pushed_at":"2015-05-11T12:36:30Z","git_url":"git://github.com/drewhk/akka.git","ssh_url":"git@github.com:drewhk/akka.git","clone_url":"https://github.com/drewhk/akka.git","svn_url":"https://github.com/drewhk/akka","homepage":"http://akka.io","size":162399,"stargazers_count":1,"watchers_count":1,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"akka:release-2.3-dev","ref":"release-2.3-dev","sha":"a538abf0b0a9771acd037bb9da41c6f68ce8e54f","user":{"login":"akka","id":1496237,"avatar_url":"https://avatars.githubusercontent.com/u/1496237?v=3","gravatar_id":"","url":"https://api.github.com/users/akka","html_url":"https://github.com/akka","followers_url":"https://api.github.com/users/akka/followers","following_url":"https://api.github.com/users/akka/following{/other_user}","gists_url":"https://api.github.com/users/akka/gists{/gist_id}","starred_url":"https://api.github.com/users/akka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akka/subscriptions","organizations_url":"https://api.github.com/users/akka/orgs","repos_url":"https://api.github.com/users/akka/repos","events_url":"https://api.github.com/users/akka/events{/privacy}","received_events_url":"https://api.github.com/users/akka/received_events","type":"Organization","site_admin":false},"repo":{"id":130013,"name":"akka","full_name":"akka/akka","owner":{"login":"akka","id":1496237,"avatar_url":"https://avatars.githubusercontent.com/u/1496237?v=3","gravatar_id":"","url":"https://api.github.com/users/akka","html_url":"https://github.com/akka","followers_url":"https://api.github.com/users/akka/followers","following_url":"https://api.github.com/users/akka/following{/other_user}","gists_url":"https://api.github.com/users/akka/gists{/gist_id}","starred_url":"https://api.github.com/users/akka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akka/subscriptions","organizations_url":"https://api.github.com/users/akka/orgs","repos_url":"https://api.github.com/users/akka/repos","events_url":"https://api.github.com/users/akka/events{/privacy}","received_events_url":"https://api.github.com/users/akka/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/akka/akka","description":"Akka Project","fork":false,"url":"https://api.github.com/repos/akka/akka","forks_url":"https://api.github.com/repos/akka/akka/forks","keys_url":"https://api.github.com/repos/akka/akka/keys{/key_id}","collaborators_url":"https://api.github.com/repos/akka/akka/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/akka/akka/teams","hooks_url":"https://api.github.com/repos/akka/akka/hooks","issue_events_url":"https://api.github.com/repos/akka/akka/issues/events{/number}","events_url":"https://api.github.com/repos/akka/akka/events","assignees_url":"https://api.github.com/repos/akka/akka/assignees{/user}","branches_url":"https://api.github.com/repos/akka/akka/branches{/branch}","tags_url":"https://api.github.com/repos/akka/akka/tags","blobs_url":"https://api.github.com/repos/akka/akka/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/akka/akka/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/akka/akka/git/refs{/sha}","trees_url":"https://api.github.com/repos/akka/akka/git/trees{/sha}","statuses_url":"https://api.github.com/repos/akka/akka/statuses/{sha}","languages_url":"https://api.github.com/repos/akka/akka/languages","stargazers_url":"https://api.github.com/repos/akka/akka/stargazers","contributors_url":"https://api.github.com/repos/akka/akka/contributors","subscribers_url":"https://api.github.com/repos/akka/akka/subscribers","subscription_url":"https://api.github.com/repos/akka/akka/subscription","commits_url":"https://api.github.com/repos/akka/akka/commits{/sha}","git_commits_url":"https://api.github.com/repos/akka/akka/git/commits{/sha}","comments_url":"https://api.github.com/repos/akka/akka/comments{/number}","issue_comment_url":"https://api.github.com/repos/akka/akka/issues/comments{/number}","contents_url":"https://api.github.com/repos/akka/akka/contents/{+path}","compare_url":"https://api.github.com/repos/akka/akka/compare/{base}...{head}","merges_url":"https://api.github.com/repos/akka/akka/merges","archive_url":"https://api.github.com/repos/akka/akka/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/akka/akka/downloads","issues_url":"https://api.github.com/repos/akka/akka/issues{/number}","pulls_url":"https://api.github.com/repos/akka/akka/pulls{/number}","milestones_url":"https://api.github.com/repos/akka/akka/milestones{/number}","notifications_url":"https://api.github.com/repos/akka/akka/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/akka/akka/labels{/name}","releases_url":"https://api.github.com/repos/akka/akka/releases{/id}","created_at":"2009-02-16T12:51:54Z","updated_at":"2015-05-12T03:23:55Z","pushed_at":"2015-05-12T07:02:04Z","git_url":"git://github.com/akka/akka.git","ssh_url":"git@github.com:akka/akka.git","clone_url":"https://github.com/akka/akka.git","svn_url":"https://github.com/akka/akka","homepage":"http://akka.io","size":250801,"stargazers_count":3931,"watchers_count":3931,"language":"Scala","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1133,"mirror_url":null,"open_issues_count":657,"forks":1133,"open_issues":657,"watchers":3931,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/akka/akka/pulls/17446"},"html":{"href":"https://github.com/akka/akka/pull/17446"},"issue":{"href":"https://api.github.com/repos/akka/akka/issues/17446"},"comments":{"href":"https://api.github.com/repos/akka/akka/issues/17446/comments"},"review_comments":{"href":"https://api.github.com/repos/akka/akka/pulls/17446/comments"},"review_comment":{"href":"https://api.github.com/repos/akka/akka/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/akka/akka/pulls/17446/commits"},"statuses":{"href":"https://api.github.com/repos/akka/akka/statuses/1055c513fc74b61a8651cf01b1fa700f79608da4"}}}}