Last active
August 29, 2015 14:26
-
-
Save fiadliel/45787aac12aaca86a0b4 to your computer and use it in GitHub Desktop.
OutputStream that writes to a scalaz-stream mutable Queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.OutputStream | |
import scalaz.stream._ | |
import scodec.bits.ByteVector | |
class QueueOutputStream(q: async.mutable.Queue[ByteVector]) extends OutputStream { | |
override def write(b: Int): Unit = | |
q.enqueueOne(ByteVector(b & 0xff)).run | |
override def write(b: Array[Byte], off: Int, len: Int): Unit = { | |
val bv = ByteVector.view(b).drop(off).take(len).copy | |
q.enqueueOne(bv).run | |
} | |
override def close(): Unit = | |
q.close.run | |
// feels dangerous. Perhaps it should be a no-op instead. | |
override def flush(): Unit = | |
q.size.discrete.find(_ == 0).run.run | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use with
q.dequeue
,q.dequeueBatch
, orq.dequeueAvailable
as a source to a scalaz-stream pipeline.