Giter VIP home page Giter VIP logo

Comments (11)

marklister avatar marklister commented on July 21, 2024

@bblfish How does this look?

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val a= ArrayBuffer[Byte]() ++ "abc".getBytes
a: scala.collection.mutable.ArrayBuffer[Byte] = ArrayBuffer(97, 98, 99)

scala> a.toBase64
res0: String = YWJj

It works on a Seq[Byte]

from base64.

bblfish avatar bblfish commented on July 21, 2024

That probably helps in many situations. But I don't know if it helps with java.nio.ByteBuffer, other than having to convert that to that to an ArrayBuffer. Is there a way to do that efficiently? ( ie, really one wants to be allocating no new memory )

We were discussing this today on the scala-js gitter.

from base64.

marklister avatar marklister commented on July 21, 2024

The latest commit (and I published it to bintray) supports a Seq[Byte] ArrayBuffer [Byte] etc.

java.nio.ByteBuffer has an array() method. @bblfish, if I read your initial post correctly you're deriving ByteBuffer from ArrayBuffer so I don't think we need to support directly?

from base64.

bblfish avatar bblfish commented on July 21, 2024

yes, no need to support ArrayBuffer as that is a JS view on something that is very similar to java.nio.ByteBuffer. I suppose that is why there is a transformation in ScalaJS from ArrayBuffer to a java.nio.ByteBuffer since scala-js 0.6.1.

I can try the array() method on ByteBuffer though my guess is that is not the most efficient way to do things. For my purposes efficiency is not paramount at the moment. But as you start working on the efficiency of your library you'll want to use its features directly. It should not be too hard then to get something extremely efficient. perhaps there is something to learn from @mseddon 's https://gist.github.com/mseddon/1cfcb0970272cac40497

from base64.

bblfish avatar bblfish commented on July 21, 2024

This is my current code

   import com.github.marklister.base64.Base64._
   val bb = TypedArrayBuffer.wrap(sig.asInstanceOf[ArrayBuffer])
   val arraybuf: Array[Byte] = new Array[Byte](bb.remaining())
   bb.get(arraybuf)
   val hashedSig = arraybuf.toBase64

TypedArrayBuffer.wrap(sig.asInstanceOf[ArrayBuffer]) returns a java.nio.ByteBuffer .
I am not sure if there are read only ByteBuffers. If so you could write a wrapper that wraps those using the few lines of code above, and that could be used like this:

bb.toBase64

Then later when you have time you could optimise away the copy.

from base64.

marklister avatar marklister commented on July 21, 2024

So would

sig.asInstanceOf[ArrayBuffer].toBase64

not work?

from base64.

bblfish avatar bblfish commented on July 21, 2024

Well that would make it a pure scala-js function. ArrayBuffer's full signature is scala.scalajs.js.typedarray.ArrayBuffer, and I don't think you need to write something that is scala-js specific like that. Libraries should be producing the nio classes.

from base64.

marklister avatar marklister commented on July 21, 2024

Ah! I see! I had it confused with
http://www.scala-lang.org/api/current/index.html#scala.collection.mutable.ArrayBuffer

from base64.

bblfish avatar bblfish commented on July 21, 2024

my fault. Should have included the imports. Sorry!

from base64.

bblfish avatar bblfish commented on July 21, 2024

Got my code working! I had a bug on the server with encoding/decoding BigIntegers from hexadecimal. read-write-web/rww-play@de8655e

from base64.

marklister avatar marklister commented on July 21, 2024

Yeah, I bumped into this same problem a few years ago. See my basen lib.

from base64.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.