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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
package java.nio.charset
import java.nio.{ByteBuffer, CharBuffer}
import scala.scalajs.js
abstract class Charset protected (canonicalName: String,
aliases: Array[String]) extends AnyRef with Comparable[Charset] {
final def name(): String = canonicalName
override final def equals(that: Any): Boolean = that match {
case that: Charset => this.name == that.name
case _ => false
}
override final def toString(): String = name()
override final def hashCode(): Int = name.##
override final def compareTo(that: Charset): Int =
name.compareToIgnoreCase(that.name)
def contains(cs: Charset): Boolean
def newDecoder(): CharsetDecoder
def newEncoder(): CharsetEncoder
def canEncode(): Boolean = true
private lazy val cachedDecoder = {
this.newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
}
private lazy val cachedEncoder = {
this.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
}
final def decode(bb: ByteBuffer): CharBuffer =
cachedDecoder.decode(bb)
final def encode(cb: CharBuffer): ByteBuffer =
cachedEncoder.encode(cb)
final def encode(str: String): ByteBuffer =
encode(CharBuffer.wrap(str))
def displayName(): String = name
}
object Charset {
import StandardCharsets._
def defaultCharset(): Charset =
UTF_8
def forName(charsetName: String): Charset =
CharsetMap.getOrElse(charsetName.toLowerCase,
throw new UnsupportedCharsetException(charsetName))
def isSupported(charsetName: String): Boolean =
CharsetMap.contains(charsetName.toLowerCase)
private lazy val CharsetMap = {
val m = js.Dictionary.empty[Charset]
// All these lists where obtained by experimentation on the JDK
for (s <- Seq("iso-8859-1", "iso8859-1", "iso_8859_1", "iso8859_1",
"iso_8859-1", "8859_1", "iso_8859-1:1987",
"latin1", "csisolatin1", "l1",
"ibm-819", "ibm819", "cp819", "819",
"iso-ir-100"))
m(s) = ISO_8859_1
for (s <- Seq("us-ascii", "ascii7", "ascii", "csascii",
"default",
"cp367", "ibm367",
"iso646-us", "646", "iso_646.irv:1983", "iso_646.irv:1991",
"ansi_x3.4-1986", "ansi_x3.4-1968",
"iso-ir-6"))
m(s) = US_ASCII
for (s <- Seq("utf-8", "utf_8", "utf8", "unicode-1-1-utf-8"))
m(s) = UTF_8
for (s <- Seq("utf-16be", "utf_16be", "x-utf-16be",
"iso-10646-ucs-2", "unicodebigunmarked"))
m(s) = UTF_16BE
for (s <- Seq("utf-16le", "utf_16le", "x-utf-16le",
"unicodelittleunmarked"))
m(s) = UTF_16LE
for (s <- Seq("utf-16", "utf_16", "unicode", "unicodebig"))
m(s) = UTF_16
m
}
}
|