From 50f649c3c9a77b9aed414ae482f4502474a7aab2 Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Wed, 21 Oct 2020 13:38:49 -0700 Subject: [PATCH] Fix SimpleInputStream reads into array When the read methods of InputStream that take an Array[Byte] as input are called, they are supposed to return -1 if there are no bytes available due to an EOF. Previously it was incorrectly writing the -1 as a byte in the array and returning 1. Now it correctly returns -1. The condition for closing the WriteableInputStream was also incorrect. We should only close the input stream if it returns -1 in raw mode. Fixes #5999 --- .../src/main/scala/sbt/internal/util/Terminal.scala | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/internal/util-logging/src/main/scala/sbt/internal/util/Terminal.scala b/internal/util-logging/src/main/scala/sbt/internal/util/Terminal.scala index 4c6d48a5d..0c336fa2d 100644 --- a/internal/util-logging/src/main/scala/sbt/internal/util/Terminal.scala +++ b/internal/util-logging/src/main/scala/sbt/internal/util/Terminal.scala @@ -486,7 +486,7 @@ object Terminal { val _ = readQueue.take val b = in.read buffer.put(b) - if (Thread.interrupted() || (b != -1 && !isRaw.get)) closed.set(true) + if (Thread.interrupted() || (b == -1 && isRaw.get)) closed.set(true) else impl() } try impl() @@ -596,9 +596,12 @@ object Terminal { private[sbt] trait SimpleInputStream extends InputStream { override def read(b: Array[Byte]): Int = read(b, 0, b.length) override def read(b: Array[Byte], off: Int, len: Int): Int = { - val byte = read() - b(off) = byte.toByte - 1 + read() match { + case -1 => -1 + case byte => + b(off) = byte.toByte + 1 + } } } private[this] object proxyInputStream extends SimpleInputStream {