R A
R A

Reputation: 295

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
}

Upvotes: 0

Views: 153

Answers (1)

sjrd
sjrd

Reputation: 22085

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

Upvotes: 0

Related Questions