Skip to content

Instantly share code, notes, and snippets.

@acanakoglu
Created April 1, 2017 08:25
Show Gist options
  • Save acanakoglu/47721ca44b57f47685f557dc385f8e36 to your computer and use it in GitHub Desktop.
Save acanakoglu/47721ca44b57f47685f557dc385f8e36 to your computer and use it in GitHub Desktop.
import java.io._
import play.api.libs.iteratee.{Enumerator, Iteratee}
import scala.concurrent.{ExecutionContext, Future}
import ExecutionContext.Implicits.global
/**
* Created by canakoglu on /28/317.
*/
object hello extends App{
val enumeratorF: Future[Enumerator[Array[Byte]]] = Future {
Enumerator.fromFile(new File("test.txt"))
}
val pos = new PipedOutputStream()
val pis = new PipedInputStream(pos)
enumeratorF.map { (enumerator: Enumerator[Array[Byte]]) =>
val it = Iteratee.foreach { t: Array[Byte] =>
pos.write(t)
}
enumerator.onDoneEnumerating(pos.close()) |>>> it
}
def inputStreamToString(is: InputStream) = {
val rd: BufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"))
val builder = new StringBuilder()
try {
var line = rd.readLine
while (line != null) {
builder.append(line + "\n")
line = rd.readLine
}
} finally {
rd.close
}
builder.toString
}
// pis.close()
println(inputStreamToString(pis))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment