本文整理汇总了Scala中scala.scalajs.js.typedarray.ArrayBuffer类的典型用法代码示例。如果您正苦于以下问题:Scala ArrayBuffer类的具体用法?Scala ArrayBuffer怎么用?Scala ArrayBuffer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ArrayBuffer类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: TorrentStreamApi
//设置package包名称以及导入依赖的类
package com.karasiq.torrentstream.frontend
import boopickle.Default._
import org.scalajs.dom.ext.Ajax
import org.scalajs.dom.raw.XMLHttpRequest
import scala.concurrent.Future
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
import scala.scalajs.js.typedarray.{ArrayBuffer, TypedArrayBuffer}
object TorrentStreamApi {
private def readBinary[T: Pickler](xhr: XMLHttpRequest): T = {
Unpickle[T].fromBytes(TypedArrayBuffer.wrap(xhr.response.asInstanceOf[ArrayBuffer]))
}
def upload(data: Ajax.InputData): Future[TorrentInfo] = {
Ajax.post("/upload", data, responseType = "arraybuffer")
.map(readBinary[TorrentInfo])
}
def uploaded(): Future[Int] = {
Ajax.get("/info", responseType = "arraybuffer")
.map(readBinary[Int])
}
def info(offset: Int, count: Int): Future[Vector[TorrentInfo]] = {
Ajax.get(s"/info?offset=$offset&count=$count", responseType = "arraybuffer")
.map(readBinary[Vector[TorrentInfo]])
}
def remove(infoHash: String): Future[Unit] = {
Ajax.delete(s"/torrent?hash=$infoHash").map(_ ? ())
}
}
示例2: WebWorkerTransport
//设置package包名称以及导入依赖的类
package suzaku.platform.web
import java.nio.ByteBuffer
import suzaku.platform.Transport
import org.scalajs.dom
import org.scalajs.dom.webworkers.{DedicatedWorkerGlobalScope, Worker}
import scala.scalajs.js
import scala.scalajs.js.typedarray.TypedArrayBufferOps._
import scala.scalajs.js.typedarray.{ArrayBuffer, TypedArrayBuffer}
abstract class WebWorkerTransport extends Transport {
private val emptyHandler = (data: ByteBuffer) => ()
private var dataHandler: ByteBuffer => Unit = emptyHandler
override def subscribe(handler: ByteBuffer => Unit): () => Unit = {
dataHandler = handler
// return an unsubscribing function
() =>
{
// restore our empty handler when called
dataHandler = emptyHandler
}
}
def receive(data: ArrayBuffer): Unit = dataHandler(TypedArrayBuffer.wrap(data))
}
class WorkerTransport(worker: DedicatedWorkerGlobalScope) extends WebWorkerTransport {
override def send(data: ByteBuffer): Unit = {
assert(data.hasTypedArray())
val out = data.typedArray.buffer.slice(0, data.limit())
worker.postMessage(out, js.Array(out).asInstanceOf[js.UndefOr[js.Array[dom.raw.Transferable]]])
}
}
class WorkerClientTransport(worker: Worker) extends WebWorkerTransport {
override def send(data: ByteBuffer): Unit = {
assert(data.hasTypedArray())
val out = data.typedArray.buffer.slice(0, data.limit())
worker.postMessage(out, js.Array(out).asInstanceOf[js.UndefOr[js.Array[dom.raw.Transferable]]])
}
}
示例3: WebDemoUIEntry
//设置package包名称以及导入依赖的类
package webdemo
import suzaku.platform.web.{WebWorkerTransport, WorkerClientTransport}
import org.scalajs.dom
import org.scalajs.dom.raw.Worker
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import scala.scalajs.js.typedarray.{ArrayBuffer, TypedArrayBuffer}
@JSExportTopLevel("WebDemoUIEntry")
object WebDemoUIEntry {
var transport: WebWorkerTransport = _
@JSExport
def entry(workerScript: String): Unit = {
// create the worker to run our application in
val worker = new Worker(workerScript)
// create the transport
transport = new WorkerClientTransport(worker)
// listen to messages from worker
worker.onmessage = onMessage _
new WebDemoUI(transport)
}
private def debugPrint(data: Array[Byte]): Unit = {
data.grouped(16).foreach { d =>
val hex = d.map(c => "%02X " format (c & 0xFF)).mkString
val str = d
.collect {
case ascii if ascii >= 0x20 && ascii < 0x80 => ascii
case _ => '.'.toByte
}
.map(_.toChar)
.mkString
println(hex.padTo(16 * 3, ' ') + str)
}
}
def onMessage(msg: dom.MessageEvent) = {
msg.data match {
case buffer: ArrayBuffer =>
transport.receive(buffer)
case _ => // ignore other messages
}
}
}
示例4: WebDemoAppEntry
//设置package包名称以及导入依赖的类
package webdemo
import suzaku.platform.web.{WebWorkerTransport, WorkerTransport}
import org.scalajs.dom
import org.scalajs.dom.raw.DedicatedWorkerGlobalScope
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import scala.scalajs.js.typedarray.{ArrayBuffer, TypedArrayBuffer}
@JSExportTopLevel("WebDemoAppEntry")
object WebDemoAppEntry {
import DedicatedWorkerGlobalScope.self
var transport: WebWorkerTransport = _
@JSExport
def entry(): Unit = {
// create transport
transport = new WorkerTransport(self)
// receive WebWorker messages
self.onmessage = onMessage _
// create the actual application
val app = new WebDemoApp(transport)
}
private def debugPrint(data: Array[Byte]): Unit = {
data.grouped(16).foreach { d =>
val hex = d.map(c => "%02X " format (c & 0xFF)).mkString
val str = d
.collect {
case ascii if ascii >= 0x20 && ascii < 0x80 => ascii
case _ => '.'.toByte
}
.map(_.toChar)
.mkString
println(hex.padTo(16 * 3, ' ') + str)
}
}
def onMessage(msg: dom.MessageEvent) = {
msg.data match {
case buffer: ArrayBuffer =>
transport.receive(buffer)
case _ => // ignore other messages
}
}
}
示例5: PerfUIEntry
//设置package包名称以及导入依赖的类
package perftest
import org.scalajs.dom
import org.scalajs.dom.raw.Worker
import suzaku.platform.web.{WebWorkerTransport, WorkerClientTransport}
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import scala.scalajs.js.typedarray.ArrayBuffer
@JSExportTopLevel("PerfUIEntry")
object PerfUIEntry {
var transport: WebWorkerTransport = _
@JSExport
def entry(workerScript: String): Unit = {
// create the worker to run our application in
val worker = new Worker(workerScript)
// create the transport
transport = new WorkerClientTransport(worker)
// listen to messages from worker
worker.onmessage = onMessage _
new PerfUI(transport)
}
private def debugPrint(data: Array[Byte]): Unit = {
data.grouped(16).foreach { d =>
val hex = d.map(c => "%02X " format (c & 0xFF)).mkString
val str = d
.collect {
case ascii if ascii >= 0x20 && ascii < 0x80 => ascii
case _ => '.'.toByte
}
.map(_.toChar)
.mkString
println(hex.padTo(16 * 3, ' ') + str)
}
}
def onMessage(msg: dom.MessageEvent) = {
msg.data match {
case buffer: ArrayBuffer =>
transport.receive(buffer)
case _ => // ignore other messages
}
}
}
示例6: PerfAppEntry
//设置package包名称以及导入依赖的类
package perftest
import org.scalajs.dom
import org.scalajs.dom.raw.DedicatedWorkerGlobalScope
import suzaku.platform.web.{WebWorkerTransport, WorkerTransport}
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import scala.scalajs.js.typedarray.ArrayBuffer
@JSExportTopLevel("PerfAppEntry")
object PerfAppEntry {
import DedicatedWorkerGlobalScope.self
var transport: WebWorkerTransport = _
@JSExport
def entry(): Unit = {
// create transport
transport = new WorkerTransport(self)
// receive WebWorker messages
self.onmessage = onMessage _
// create the actual application
val app = new PerfApp(transport)
}
private def debugPrint(data: Array[Byte]): Unit = {
data.grouped(16).foreach { d =>
val hex = d.map(c => "%02X " format (c & 0xFF)).mkString
val str = d
.collect {
case ascii if ascii >= 0x20 && ascii < 0x80 => ascii
case _ => '.'.toByte
}
.map(_.toChar)
.mkString
println(hex.padTo(16 * 3, ' ') + str)
}
}
def onMessage(msg: dom.MessageEvent) = {
msg.data match {
case buffer: ArrayBuffer =>
transport.receive(buffer)
case _ => // ignore other messages
}
}
}
示例7: Converters
//设置package包名称以及导入依赖的类
package fr.hmil.roshttp
import java.nio.ByteBuffer
import fr.hmil.roshttp.node.buffer.Buffer
import scala.scalajs.js
import scala.scalajs.js.typedarray.{ArrayBuffer, Int8Array, TypedArrayBuffer, Uint8Array}
import scala.scalajs.js.JSConverters._
import js.typedarray.TypedArrayBufferOps._
private object Converters {
def byteArrayToUint8Array(arr: Array[Byte]): Uint8Array = {
js.Dynamic.newInstance(js.Dynamic.global.Uint8Array)(arr.toJSArray).asInstanceOf[Uint8Array]
}
def byteBufferToNodeBuffer(buffer: ByteBuffer): Buffer = {
if (buffer.isDirect) {
js.Dynamic.newInstance(js.Dynamic.global.Buffer)(buffer.arrayBuffer).asInstanceOf[Buffer]
} else if (buffer.hasArray) {
js.Dynamic.newInstance(js.Dynamic.global.Buffer)(byteArrayToUint8Array(buffer.array())).asInstanceOf[Buffer]
} else {
val arr = new Int8Array(buffer.limit)
var i = 0
while (i < arr.length) {
arr(i) = buffer.get(i)
i += 1
}
js.Dynamic.newInstance(js.Dynamic.global.Buffer)(arr).asInstanceOf[Buffer]
}
}
def nodeBufferToByteBuffer(buffer: Buffer): ByteBuffer = {
TypedArrayBuffer.wrap(buffer.asInstanceOf[ArrayBuffer])
}
def arrayBufferToByteBuffer(buffer: ArrayBuffer): ByteBuffer = {
TypedArrayBuffer.wrap(buffer)
}
}
示例8: Buffer
//设置package包名称以及导入依赖的类
package sharednodejsapis
import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobal
import scala.scalajs.js.typedarray.ArrayBuffer
@js.native
@JSGlobal
abstract class Buffer extends js.typedarray.Uint8Array(0) {
def toString(encoding: String = "utf8", start: Int = 0, end: Int = this.length): String = js.native
}
@js.native
@JSGlobal
object Buffer extends js.Object {
def from(s: String, encoding: String = "utf8"): Buffer = js.native
def from(msg: ArrayBuffer): Buffer = js.native
}
示例9: NativeUtils
//设置package包名称以及导入依赖的类
package akkaviz.frontend
import scala.scalajs.js
import scala.scalajs.js.typedarray.{ArrayBufferView, Uint8Array, ArrayBuffer}
object NativeUtils {
val encoder = new TextEncoder("utf-8")
val decoder = new TextDecoder("utf-8")
def ab2str(ab: ArrayBuffer): String = {
decoder.decode(new Uint8Array(ab))
}
def str2ab(str: String): Uint8Array = {
encoder.encode(str)
}
}
@js.native
class TextEncoder(utfLabel: js.UndefOr[String]) extends js.Object {
def encode(buffer: String): Uint8Array = js.native
}
@js.native
class TextDecoder(utfLabel: js.UndefOr[String]) extends js.Object {
def decode(buffer: Uint8Array): String = js.native
}
示例10: BinaryAjax
//设置package包名称以及导入依赖的类
package io.plasmap.js
import java.nio.ByteBuffer
import io.plasmap.querymodel.PMQuery
import org.scalajs.dom
import org.scalajs.dom.ext.AjaxException
import scala.scalajs.js.typedarray.TypedArrayBufferOps._
import scala.scalajs.js.typedarray.{ArrayBuffer, TypedArrayBuffer}
import scala.concurrent.Promise
object BinaryAjax {
//Binary data support for Scala.js DOM 0.6.2
def post(url:String,
data:ByteBuffer,
timeout:Int = 0,
headers:Map[String, String] = Map.empty,
withCredentials: Boolean = false,
responseType: String = "" ) = {
val req = new dom.XMLHttpRequest()
val promise = Promise[dom.XMLHttpRequest]()
req.onreadystatechange = { (e:dom.Event) ?
if (req.readyState == 4) {
if ((req.status >= 200 && req.status < 300) || req.status == 304)
promise success req
else
promise failure AjaxException(req)
}
}
req.open("POST", url)
req.responseType = responseType
req.timeout = timeout
req.withCredentials = withCredentials
for((header, value) ? headers) req.setRequestHeader(header, value)
req.send(data.typedArray())
promise.future
}
}
示例11: assetsEndpoint
//设置package包名称以及导入依赖的类
package endpoints.xhr
import endpoints.algebra
import org.scalajs.dom.XMLHttpRequest
import scala.scalajs.js
import scala.scalajs.js.typedarray.ArrayBuffer
def assetsEndpoint(url: Url[AssetPath]): Endpoint[AssetRequest, AssetResponse] =
endpoint(arrayBufferGet(url), arrayBufferResponse)
private def arrayBufferGet(url: Url[AssetPath]): Request[AssetRequest] =
(assetRequest: AssetRequest) => {
val xhr = new XMLHttpRequest
xhr.open("GET", url.encode(assetRequest))
xhr.responseType = "arraybuffer"
(xhr, None)
}
private def arrayBufferResponse: Response[ArrayBuffer] =
(xhr: XMLHttpRequest) => {
if (xhr.status < 300) {
try {
Right(xhr.response.asInstanceOf[ArrayBuffer])
} catch {
case exn: Exception => Left(exn)
}
} else Left(new Exception("Resource not found"))
}
}