Skip to content

Commit 6641938

Browse files
committed
Improve reading from InputStream
Solution based on http://www.gregbugaj.com/?p=283&cpage=1
1 parent 4f05fbd commit 6641938

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Contexts._, Symbols._, Types._, Names._, StdNames._, NameOps._, Scopes._,
77
import SymDenotations._, unpickleScala2.Scala2Unpickler._, Constants._, Annotations._, util.Positions._
88
import NameKinds.{ModuleClassName, DefaultGetterName}
99
import ast.tpd._
10-
import java.io.{ ByteArrayInputStream, DataInputStream, File, IOException }
10+
import java.io.{ ByteArrayInputStream, ByteArrayOutputStream, DataInputStream, File, IOException }
1111
import java.nio
1212
import java.lang.Integer.toHexString
1313
import java.net.URLClassLoader
@@ -795,11 +795,15 @@ class ClassfileParser(
795795
val path = classfile.path.stripSuffix(".class") + ".tasty"
796796
val stream = cl.getResourceAsStream(path)
797797
if (stream != null) {
798-
val tasty = Array.newBuilder[Byte]
799-
tasty.sizeHint(stream.available())
800-
while (stream.available() > 0) // TODO improve performance
801-
tasty += stream.read().toByte
802-
tasty.result()
798+
val tastyOutStream = new ByteArrayOutputStream()
799+
val buffer = new Array[Byte](1024)
800+
var read = stream.read(buffer, 0, buffer.length)
801+
while (read != -1) {
802+
tastyOutStream.write(buffer, 0, read)
803+
read = stream.read(buffer, 0, buffer.length)
804+
}
805+
tastyOutStream.flush()
806+
tastyOutStream.toByteArray
803807
} else {
804808
ctx.error(s"Could not find $path in $jar")
805809
Array.empty

0 commit comments

Comments
 (0)