mirror of
https://github.com/sbt/sbt.git
synced 2026-09-08 11:13:23 +02:00
Clean-up helper method to fully read InputStream
This commit is contained in:
-14
@@ -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
@@ -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
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user