mirror of https://github.com/sbt/sbt.git
[2.x] refactor: Use InputStream.transferTo (#9212)
This commit is contained in:
parent
4630ddfb38
commit
56cdef09f7
|
|
@ -17,7 +17,6 @@ import sjsonnew.support.scalajson.unsafe.{ CompactPrinter, Converter }
|
|||
|
||||
import java.io.{ File, FileOutputStream, InputStream, OutputStream }
|
||||
import java.net.URI
|
||||
import scala.annotation.tailrec
|
||||
|
||||
object TreeView {
|
||||
def createJson(graph: ModuleGraph): String = {
|
||||
|
|
@ -79,17 +78,6 @@ object TreeView {
|
|||
}
|
||||
}
|
||||
|
||||
def copy(from: InputStream, to: OutputStream): Unit = {
|
||||
val buffer = new Array[Byte](65536)
|
||||
|
||||
@tailrec def rec(): Unit = {
|
||||
val read = from.read(buffer)
|
||||
if (read > 0) {
|
||||
to.write(buffer, 0, read)
|
||||
rec()
|
||||
} else if (read == 0)
|
||||
throw new IllegalStateException("InputStream.read returned 0")
|
||||
}
|
||||
rec()
|
||||
}
|
||||
def copy(from: InputStream, to: OutputStream): Unit =
|
||||
from.transferTo(to)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,9 +21,7 @@ object HttpPutServer {
|
|||
val in = ex.getRequestBody
|
||||
val out = new FileOutputStream(targetFile)
|
||||
try {
|
||||
val buf = new Array[Byte](8192)
|
||||
var n = 0
|
||||
while ({ n = in.read(buf); n } != -1) out.write(buf, 0, n)
|
||||
in.transferTo(out)
|
||||
} finally {
|
||||
out.close()
|
||||
in.close()
|
||||
|
|
|
|||
|
|
@ -20,9 +20,7 @@ object HttpPutServer {
|
|||
val in = ex.getRequestBody
|
||||
val out = new FileOutputStream(targetFile)
|
||||
try {
|
||||
val buf = new Array[Byte](8192)
|
||||
var n = 0
|
||||
while ({ n = in.read(buf); n } != -1) out.write(buf, 0, n)
|
||||
in.transferTo(out)
|
||||
} finally {
|
||||
out.close()
|
||||
in.close()
|
||||
|
|
|
|||
|
|
@ -14,14 +14,7 @@ libraryDependencies += "com.chuusai" %% "shapeless" % "2.3.41" from {
|
|||
val is = url.openStream()
|
||||
val os = new java.io.FileOutputStream(f)
|
||||
|
||||
var read = -1
|
||||
val b = new Array[Byte](16*1024)
|
||||
while ({
|
||||
read = is.read(b)
|
||||
read >= 0
|
||||
}) {
|
||||
os.write(b, 0, read)
|
||||
}
|
||||
is.transferTo(os)
|
||||
|
||||
is.close()
|
||||
os.close()
|
||||
|
|
|
|||
Loading…
Reference in New Issue