Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider structures other than Array[Byte] #8

Open
marklister opened this issue Nov 13, 2015 · 11 comments
Open

Consider structures other than Array[Byte] #8

marklister opened this issue Nov 13, 2015 · 11 comments

Comments

@marklister
Copy link
Owner

From #6 (@bblfish)

Perhaps there is an extra improvement you can add. Is it possible to abstract somewhat from an array? For example the JS seems to return a ArrayBuffer which I think can be turned into a java.nio.ByteBuffer using TypedArrayBufferBridge.

So it would be useful to have a method that could work with those elements and not just arrays. I am not yet quite sure what the best abstraction here would be... Certainly given that you don't need to change the source object, an Array is too precise.

There is some example code here using a ByteBuffer https://gist.github.com/mseddon/1cfcb0970272cac40497

@marklister
Copy link
Owner Author

@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]

@bblfish
Copy link

bblfish commented Nov 13, 2015

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.

marklister pushed a commit that referenced this issue Nov 13, 2015
@marklister
Copy link
Owner Author

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?

@bblfish
Copy link

bblfish commented Nov 13, 2015

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

@bblfish
Copy link

bblfish commented Nov 16, 2015

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.

@marklister
Copy link
Owner Author

So would

sig.asInstanceOf[ArrayBuffer].toBase64

not work?

@bblfish
Copy link

bblfish commented Nov 16, 2015

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.

@marklister
Copy link
Owner Author

@bblfish
Copy link

bblfish commented Nov 16, 2015

my fault. Should have included the imports. Sorry!

@bblfish
Copy link

bblfish commented Nov 17, 2015

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

@marklister
Copy link
Owner Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants