Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/nio/cs/UnicodeDecoder.java
41159 views
1
/*
2
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.nio.cs;
27
28
import java.nio.ByteBuffer;
29
import java.nio.CharBuffer;
30
import java.nio.charset.Charset;
31
import java.nio.charset.CharsetDecoder;
32
import java.nio.charset.CoderResult;
33
import java.nio.charset.CharacterCodingException;
34
import java.nio.charset.MalformedInputException;
35
36
37
abstract class UnicodeDecoder extends CharsetDecoder {
38
39
protected static final char BYTE_ORDER_MARK = (char) 0xfeff;
40
protected static final char REVERSED_MARK = (char) 0xfffe;
41
42
protected static final int NONE = 0;
43
protected static final int BIG = 1;
44
protected static final int LITTLE = 2;
45
46
private final int expectedByteOrder;
47
private int currentByteOrder;
48
private int defaultByteOrder = BIG;
49
50
public UnicodeDecoder(Charset cs, int bo) {
51
super(cs, 0.5f, 1.0f);
52
expectedByteOrder = currentByteOrder = bo;
53
}
54
55
public UnicodeDecoder(Charset cs, int bo, int defaultBO) {
56
this(cs, bo);
57
defaultByteOrder = defaultBO;
58
}
59
60
private char decode(int b1, int b2) {
61
if (currentByteOrder == BIG)
62
return (char)((b1 << 8) | b2);
63
else
64
return (char)((b2 << 8) | b1);
65
}
66
67
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
68
int mark = src.position();
69
70
try {
71
while (src.remaining() > 1) {
72
int b1 = src.get() & 0xff;
73
int b2 = src.get() & 0xff;
74
75
// Byte Order Mark interpretation
76
if (currentByteOrder == NONE) {
77
char c = (char)((b1 << 8) | b2);
78
if (c == BYTE_ORDER_MARK) {
79
currentByteOrder = BIG;
80
mark += 2;
81
continue;
82
} else if (c == REVERSED_MARK) {
83
currentByteOrder = LITTLE;
84
mark += 2;
85
continue;
86
} else {
87
currentByteOrder = defaultByteOrder;
88
// FALL THROUGH to process b1, b2 normally
89
}
90
}
91
92
char c = decode(b1, b2);
93
94
// Surrogates
95
if (Character.isSurrogate(c)) {
96
if (Character.isHighSurrogate(c)) {
97
if (src.remaining() < 2)
98
return CoderResult.UNDERFLOW;
99
char c2 = decode(src.get() & 0xff, src.get() & 0xff);
100
if (!Character.isLowSurrogate(c2))
101
return CoderResult.malformedForLength(4);
102
if (dst.remaining() < 2)
103
return CoderResult.OVERFLOW;
104
mark += 4;
105
dst.put(c);
106
dst.put(c2);
107
continue;
108
}
109
// Unpaired low surrogate
110
return CoderResult.malformedForLength(2);
111
}
112
113
if (!dst.hasRemaining())
114
return CoderResult.OVERFLOW;
115
mark += 2;
116
dst.put(c);
117
118
}
119
return CoderResult.UNDERFLOW;
120
121
} finally {
122
src.position(mark);
123
}
124
}
125
126
protected void implReset() {
127
currentByteOrder = expectedByteOrder;
128
}
129
130
}
131
132