-
Notifications
You must be signed in to change notification settings - Fork 4
/
D64.scala
73 lines (66 loc) · 1.92 KB
/
D64.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package io.epiphanous.flinkrunner.model
import java.nio.ByteBuffer
import scala.annotation.switch
import scala.collection.mutable
/** A copy-pastable, url friendly, ascii embeddable, lexicographically
* sortable binary encoding
*
* Adapted from javascript code at https://github.com/dominictarr/d64
*/
object D64 {
final val chars =
".PYFGCRLAOEUIDHTNSQJKXBMWVZ_pyfgcrlaoeuidhtnsqjkxbmwvz1234567890"
.split("")
.sorted
.mkString("")
final val codeToIndex = Array.fill[Int](128)(0)
chars.zipWithIndex.foreach { case (c, i) => codeToIndex(c.toInt) = i }
/** encode binary data to string */
def encode(data: Array[Byte]): String = {
val sb = new mutable.StringBuilder("")
val len = data.length
var hang = 0
data.zipWithIndex.foreach { case (v, i) =>
val v2 = if (v < 0) v + 0x100 else v
(i % 3: @switch) match {
case 0 =>
sb += chars(v2 >> 2)
hang = (v2 & 3) << 4
case 1 =>
sb += chars(hang | v2 >> 4)
hang = (v2 & 0xf) << 2
case 2 =>
sb += chars(hang | v2 >> 6)
sb += chars(v2 & 0x3f)
hang = 0
}
}
if ((len % 3) > 0) sb += chars(hang)
sb.mkString
}
/** decode an encoded string back to binary data */
def decode(str: String): Array[Byte] = {
val len = str.length
val bytes = ByteBuffer.allocate(Math.floor((len / 4d) * 3d).toInt)
var hang = 0
var j = 0
str.zipWithIndex.foreach { case (_, i) =>
val v = codeToIndex(str.codePointAt(i))
(i % 4: @switch) match {
case 0 => hang = v << 2
case 1 =>
bytes.put(j, (hang | v >> 4).toByte)
j += 1
hang = (v << 4) & 0xff
case 2 =>
bytes.put(j, (hang | v >> 2).toByte)
j += 1
hang = (v << 6) & 0xff
case 3 =>
bytes.put(j, (hang | v).toByte)
j += 1
}
}
bytes.array()
}
}