@@ -2,34 +2,27 @@ package dotty.tools.dotc.core.tasty
22
33import scala .runtime .quoted .Unpickler .Pickled
44
5+ import java .io ._
6+ import java .util .Base64
7+ import java .nio .charset .StandardCharsets .UTF_8
8+
59/** Utils for String representation of TASTY */
610object TastyString {
711
8- // Conservative encoding, each byte is encoded in a char
9- // TODO improve encoding compression
10- private final val maxStringSize = 65535 / 2
12+ // Max size of a string literal in the bytecode
13+ private final val maxStringSize = 65535
1114
1215 /** Encode TASTY bytes into an Seq of String */
13- def pickle (bytes : Array [Byte ]): Pickled =
14- bytes.sliding(maxStringSize, maxStringSize).map(bytesToString).toList
16+ def pickle (bytes : Array [Byte ]): Pickled = {
17+ val str = new String (Base64 .getEncoder().encode(bytes), UTF_8 )
18+ str.sliding(maxStringSize, maxStringSize).toList
19+ }
1520
1621 /** Decode the TASTY String into TASTY bytes */
1722 def unpickle (strings : Pickled ): Array [Byte ] = {
18- val bytes = new Array [Byte ](strings.map(_.length).sum)
19- var i = 0
20- for (str <- strings; j <- str.indices) {
21- bytes(i) = str.charAt(j).toByte
22- i += 1
23- }
24- bytes
25- }
26-
27- /** Encode bytes into a String */
28- private def bytesToString (bytes : Array [Byte ]): String = {
29- assert(bytes.length <= maxStringSize)
30- val chars = new Array [Char ](bytes.length)
31- for (i <- bytes.indices) chars(i) = (bytes(i) & 0xff ).toChar
32- new String (chars)
23+ val string = new StringBuilder
24+ strings.foreach(string.append)
25+ Base64 .getDecoder().decode(string.result().getBytes(UTF_8 ))
3326 }
3427
3528}
0 commit comments