Clean-up helper method to fully read InputStream

This commit is contained in:
Alexandre Archambault
2018-03-03 15:09:02 +01:00
parent ca62830d23
commit f4fe44fe2c
7 changed files with 7 additions and 23 deletions
-14
View File
@@ -1127,20 +1127,6 @@ object Cache {
var bufferSize = 1024*1024
def readFullySync(is: InputStream) = {
val buffer = new ByteArrayOutputStream()
val data = Array.ofDim[Byte](16384)
var nRead = is.read(data, 0, data.length)
while (nRead != -1) {
buffer.write(data, 0, nRead)
nRead = is.read(data, 0, data.length)
}
buffer.flush()
buffer.toByteArray
}
def withContent(is: InputStream, f: (Array[Byte], Int) => Unit): Unit = {
val data = Array.ofDim[Byte](16384)
-62
View File
@@ -1,62 +0,0 @@
package coursier
import java.io._
import java.nio.charset.Charset
import coursier.util.EitherT
import scala.language.implicitConversions
import scala.util.{Failure, Success, Try}
import scalaz.concurrent.Task
object Platform {
def readFullySync(is: InputStream) = {
val buffer = new ByteArrayOutputStream()
val data = Array.ofDim[Byte](16384)
var nRead = is.read(data, 0, data.length)
while (nRead != -1) {
buffer.write(data, 0, nRead)
nRead = is.read(data, 0, data.length)
}
buffer.flush()
buffer.toByteArray
}
private lazy val UTF_8 = Charset.forName("UTF-8")
def readFully(is: => InputStream): Task[Either[String, String]] =
Task {
val t = Try {
val is0 = is
val b =
try readFullySync(is0)
finally is0.close()
new String(b, UTF_8)
}
t match {
case Success(r) => Right(r)
case Failure(e: java.io.FileNotFoundException) if e.getMessage != null =>
Left(s"Not found: ${e.getMessage}")
case Failure(e) =>
Left(s"$e${Option(e.getMessage).fold("")(" (" + _ + ")")}")
}
}
val artifact: Fetch.Content[Task] = { artifact =>
EitherT {
val conn = Cache.urlConnection(artifact.url, artifact.authentication)
readFully(conn.getInputStream)
}
}
def fetch(
repositories: Seq[core.Repository]
): Fetch.Metadata[Task] =
Fetch.from(repositories, Platform.artifact)
}
+1 -1
View File
@@ -66,7 +66,7 @@ object FileUtil {
}
}
private def readFully(is: InputStream): Array[Byte] = {
def readFully(is: InputStream): Array[Byte] = {
val buffer = new ByteArrayOutputStream
val data = Array.ofDim[Byte](16384)