Skip to content

Commit

Permalink
Use BAOS to let the buffer grow
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Jan 24, 2024
1 parent 446573f commit e53bc75
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions modules/framework/src-js-native/RunnerCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import cats.effect.{ Ref, Sync }
import cats.syntax.all._

import sbt.testing.{ EventHandler, Logger, Task, TaskDef }
import java.io.ByteArrayOutputStream
import java.io.DataOutputStream

trait RunnerCompat[F[_]] { self: sbt.testing.Runner =>
protected val args: Array[String]
Expand Down Expand Up @@ -189,14 +191,14 @@ private[weaver] object ReadWriter {
def readDouble() = bytes.getDouble
}

class Writer(bb: ByteBuffer) {
class Writer(bb: DataOutputStream) {

def writeString(s: String) = {
bb.putInt(s.getBytes.size)
bb.put(s.getBytes())
bb.writeInt(s.getBytes.size)
bb.writeBytes(s)
}

def writeDouble(d: Double) = bb.putDouble(d)
def writeDouble(d: Double) = bb.writeDouble(d)
}

def reader[A](s: String)(f: Reader => A) = {
Expand All @@ -205,12 +207,17 @@ private[weaver] object ReadWriter {
}

def writer(f: Writer => Unit): String = {
val buf = ByteBuffer.allocate(2048)
val baos = new ByteArrayOutputStream(2048)
val dos = new DataOutputStream(baos)

f(new Writer(buf))

new String(buf.array())
try {
f(new Writer(dos))

baos.toString()
} finally {
dos.close()
baos.close()
}
}
}

Expand Down

0 comments on commit e53bc75

Please sign in to comment.