|
Play Framework/Scala example source code file (PlayIO.scala)
The PlayIO.scala Play Framework example source code/* * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com> */ package play.utils import java.io._ import scala.io.Codec import java.net.URL import play.api.Logger /** * IO utilites for internal use by Play projects. * * This is intentionally not public API. */ private[play] object PlayIO { /** * Read the given stream into a byte array. * * Closes the stream. */ private def readStream(stream: InputStream): Array[Byte] = { try { val buffer = new Array[Byte](8192) var len = stream.read(buffer) val out = new ByteArrayOutputStream() // Doesn't need closing while (len != -1) { out.write(buffer, 0, len) len = stream.read(buffer) } out.toByteArray } finally closeQuietly(stream) } /** * Read the file into a byte array. */ def readFile(file: File): Array[Byte] = { readStream(new FileInputStream(file)) } /** * Read the given stream into a String. * * Closes the stream. */ def readStreamAsString(stream: InputStream)(implicit codec: Codec): String = { new String(readStream(stream), codec.name) } /** * Read the URL as a String. */ def readUrlAsString(url: URL)(implicit codec: Codec): String = { readStreamAsString(url.openStream()) } /** * Read the file as a String. */ def readFileAsString(file: File)(implicit codec: Codec): String = { readStreamAsString(new FileInputStream(file)) } /** * Close the given closeable quietly. * * Logs any IOExceptions encountered. */ def closeQuietly(closeable: Closeable) = { try { if (closeable != null) { closeable.close() } } catch { case e: IOException => play.api.Play.logger.warn("Error closing stream", e) } } } Other Play Framework source code examplesHere is a short list of links related to this Play Framework PlayIO.scala source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.