scala.js

Scala.js - How to convert ArrayBuffer to Array[Byte]?


DOM API returns ArrayBuffer but what I need is Array[Byte]. How can I do a conversion?

import scala.scalajs.js.typedarray.ArrayBuffer

def toScalaArray(input: ArrayBuffer): Array[Byte] = {
  // code in question
}


Solution

  • Wrap it in an Int8Array, then call the extension method .toArray defined in the typedarray package:

    import scala.scalajs.js.typedarray._
    
    def toScalaArray(input: ArrayBuffer): Array[Byte] =
      new Int8Array(input).toArray