[2.x] refactor: Use InputStream.transferTo (#9212)

This commit is contained in:
kenji yoshida 2026-05-14 13:36:20 +09:00 committed by GitHub
parent 4630ddfb38
commit 56cdef09f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 5 additions and 28 deletions

View File

@ -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)
}

View File

@ -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()

View File

@ -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()

View File

@ -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()