Comments (11)
@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.
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.
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.
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.
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 ByteBuffer
s. 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.
So would
sig.asInstanceOf[ArrayBuffer].toBase64
not work?
from base64.
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.
Ah! I see! I had it confused with
http://www.scala-lang.org/api/current/index.html#scala.collection.mutable.ArrayBuffer
from base64.
my fault. Should have included the imports. Sorry!
from base64.
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.
Yeah, I bumped into this same problem a few years ago. See my basen lib.
from base64.
Related Issues (13)
- def enc not tail recursive. HOT 1
- Publish the latest version HOT 4
- Using '=' for padding does not work for URLs HOT 7
- String concat not preferred HOT 1
- 0.2.5 jars are empty on maven central HOT 4
- Release with latest scalajs 1.x? HOT 4
- indexOf in decode can cause slowness HOT 15
- base64url HOT 8
- Encoding is not compatible with Browser decoding HOT 6
- Strict padding unnecessary -- causes JS interop problem HOT 19
- Document using base64Url better
- Improve performance HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from base64.