Created
February 13, 2015 11:40
-
-
Save hamnis/65066659f9850ff88314 to your computer and use it in GitHub Desktop.
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
def string[T](req: HttpRequest[T])(implicit codec: Codec = Codec.UTF8) = { | |
loan(new InputStreamReader(req.inputStream, codec.charSet)) { reader => | |
val writer = new java.io.StringWriter | |
val ca = new Array[Char](4096) | |
@scala.annotation.tailrec def read() { | |
val len = reader.read(ca) | |
if (len > 0) writer.write(ca, 0, len) | |
if (len >= 0) read | |
} | |
read() | |
writer.toString | |
} | |
} | |
def loan[A <: java.io.Closeable, B](c: => A)(f: (A) => B): B = { | |
val cached = c | |
try { f(cached)} finally { cached.close() } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment