Skip to content

Commit cb4834d

Browse files
committed
fix 'Process' stdout interop as this stream would not exhaust before process completion
1 parent df5ede1 commit cb4834d

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

io/shared/src/main/scala/fs2/io/io.scala

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,13 @@ package object io extends ioplatform {
8282
read: (InputStream, Array[Byte], Int) => F[Int]
8383
)(implicit
8484
F: Sync[F]
85-
): F[Option[Chunk[Byte]]] =
86-
read(is, buf, offset).flatMap { numBytes =>
87-
if (numBytes < 0) {
88-
if (offset == 0) F.pure(None)
89-
else F.pure(Some(Chunk.array(buf, 0, offset)))
90-
} else {
91-
if (offset + numBytes == buf.size) F.pure(Some(Chunk.array(buf)))
92-
else readBytesFromInputStream(is, buf, offset + numBytes)(read)
85+
): F[Option[(Chunk[Byte], Option[(Array[Byte], Int)])]] =
86+
read(is, buf, offset).map { numBytes =>
87+
if (numBytes < 0) None
88+
else if (numBytes == 0) Some(Chunk.empty -> Some(buf -> offset))
89+
else {
90+
if (offset + numBytes == buf.size) Some(Chunk.array(buf, offset, numBytes) -> None)
91+
else Some(Chunk.array(buf, offset, numBytes) -> Some(buf -> (offset + numBytes)))
9392
}
9493
}
9594

@@ -98,12 +97,10 @@ package object io extends ioplatform {
9897
buf: F[Array[Byte]],
9998
closeAfterUse: Boolean
10099
)(read: (InputStream, Array[Byte], Int) => F[Int])(implicit F: Sync[F]): Stream[F, Byte] = {
101-
def useIs(is: InputStream) =
102-
Stream
103-
.eval(buf.flatMap(b => readBytesFromInputStream(is, b, 0)(read)))
104-
.repeat
105-
.unNoneTerminate
106-
.flatMap(c => Stream.chunk(c))
100+
def useIs(is: InputStream) = Stream.unfoldChunkEval(Option.empty[(Array[Byte], Int)]) {
101+
case None => buf.flatMap(b => readBytesFromInputStream(is, b, 0)(read))
102+
case Some((b, offset)) => readBytesFromInputStream(is, b, offset)(read)
103+
}
107104

108105
if (closeAfterUse)
109106
Stream.bracket(fis)(is => Sync[F].blocking(is.close())).flatMap(useIs)

0 commit comments

Comments
 (0)