Clean-up helper method to fully read InputStream

This commit is contained in:
Alexandre Archambault 2018-02-27 22:54:50 +01:00
parent ca62830d23
commit f4fe44fe2c
7 changed files with 7 additions and 23 deletions

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)

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)

View File

@ -82,7 +82,7 @@ object Bootstrap extends CaseApp[BootstrapOptions] {
val bootstrapJar =
Option(Thread.currentThread().getContextClassLoader.getResourceAsStream("bootstrap.jar")) match {
case Some(is) => Cache.readFullySync(is)
case Some(is) => FileUtil.readFully(is)
case None =>
Console.err.println(s"Error: bootstrap JAR not found")
sys.exit(1)
@ -165,7 +165,7 @@ object Bootstrap extends CaseApp[BootstrapOptions] {
entry.setTime(f.lastModified())
outputZip.putNextEntry(entry)
outputZip.write(Cache.readFullySync(new FileInputStream(f)))
outputZip.write(FileUtil.readFully(new FileInputStream(f)))
outputZip.closeEntry()
}

View File

@ -40,7 +40,7 @@ object Scaladex {
val b = try {
conn = new java.net.URL(url).openConnection().asInstanceOf[HttpURLConnection]
coursier.Platform.readFullySync(conn.getInputStream)
coursier.internal.FileUtil.readFully(conn.getInputStream)
} finally {
if (conn != null)
coursier.Cache.closeConn(conn)

View File

@ -2,8 +2,6 @@ package coursier.cli.util
import java.util.zip.{ZipEntry, ZipInputStream}
import coursier.Platform
object Zip {
def zipEntries(zipStream: ZipInputStream): Iterator[(ZipEntry, Array[Byte])] =
@ -17,7 +15,7 @@ object Zip {
def hasNext = nextEntry.nonEmpty
def next() = {
val ent = nextEntry.get
val data = Platform.readFullySync(zipStream)
val data = coursier.internal.FileUtil.readFully(zipStream)
update()

View File

@ -22,7 +22,7 @@ class CliBootstrapIntegrationTest extends FlatSpec with CliTestLib {
if (e == null)
throw new NoSuchElementException(s"Entry $path in zip file")
else if (e.getName == path)
coursier.Platform.readFullySync(zis)
coursier.internal.FileUtil.readFully(zis)
else
zipEntryContent(zis, path)
}
@ -53,7 +53,7 @@ class CliBootstrapIntegrationTest extends FlatSpec with CliTestLib {
val content = try {
fis = new FileInputStream(bootstrapFile)
coursier.Platform.readFullySync(fis)
coursier.internal.FileUtil.readFully(fis)
} finally {
if (fis != null) fis.close()
}