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/US_ASCII.java
41159 views
1
/*
2
* Copyright (c) 2000, 2021, 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 jdk.internal.access.JavaLangAccess;
29
import jdk.internal.access.SharedSecrets;
30
31
import java.nio.ByteBuffer;
32
import java.nio.CharBuffer;
33
import java.nio.charset.Charset;
34
import java.nio.charset.CharsetDecoder;
35
import java.nio.charset.CharsetEncoder;
36
import java.nio.charset.CoderResult;
37
38
public class US_ASCII
39
extends Charset
40
implements HistoricallyNamedCharset
41
{
42
public static final US_ASCII INSTANCE = new US_ASCII();
43
44
public US_ASCII() {
45
super("US-ASCII", StandardCharsets.aliases_US_ASCII());
46
}
47
48
public String historicalName() {
49
return "ASCII";
50
}
51
52
public boolean contains(Charset cs) {
53
return (cs instanceof US_ASCII);
54
}
55
56
public CharsetDecoder newDecoder() {
57
return new Decoder(this);
58
}
59
60
public CharsetEncoder newEncoder() {
61
return new Encoder(this);
62
}
63
64
private static class Decoder extends CharsetDecoder {
65
66
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
67
68
private Decoder(Charset cs) {
69
super(cs, 1.0f, 1.0f);
70
}
71
72
private CoderResult decodeArrayLoop(ByteBuffer src,
73
CharBuffer dst)
74
{
75
byte[] sa = src.array();
76
int soff = src.arrayOffset();
77
int sp = soff + src.position();
78
int sl = soff + src.limit();
79
80
char[] da = dst.array();
81
int doff = dst.arrayOffset();
82
int dp = doff + dst.position();
83
int dl = doff + dst.limit();
84
85
// ASCII only loop
86
int n = JLA.decodeASCII(sa, sp, da, dp, Math.min(sl - sp, dl - dp));
87
sp += n;
88
dp += n;
89
src.position(sp - soff);
90
dst.position(dp - doff);
91
if (sp < sl) {
92
if (dp >= dl) {
93
return CoderResult.OVERFLOW;
94
}
95
return CoderResult.malformedForLength(1);
96
}
97
return CoderResult.UNDERFLOW;
98
}
99
100
private CoderResult decodeBufferLoop(ByteBuffer src,
101
CharBuffer dst)
102
{
103
int mark = src.position();
104
try {
105
while (src.hasRemaining()) {
106
byte b = src.get();
107
if (b >= 0) {
108
if (!dst.hasRemaining())
109
return CoderResult.OVERFLOW;
110
dst.put((char)b);
111
mark++;
112
continue;
113
}
114
return CoderResult.malformedForLength(1);
115
}
116
return CoderResult.UNDERFLOW;
117
} finally {
118
src.position(mark);
119
}
120
}
121
122
protected CoderResult decodeLoop(ByteBuffer src,
123
CharBuffer dst)
124
{
125
if (src.hasArray() && dst.hasArray())
126
return decodeArrayLoop(src, dst);
127
else
128
return decodeBufferLoop(src, dst);
129
}
130
}
131
132
private static class Encoder extends CharsetEncoder {
133
134
private Encoder(Charset cs) {
135
super(cs, 1.0f, 1.0f);
136
}
137
138
public boolean canEncode(char c) {
139
return c < 0x80;
140
}
141
142
public boolean isLegalReplacement(byte[] repl) {
143
return (repl.length == 1 && repl[0] >= 0) ||
144
super.isLegalReplacement(repl);
145
}
146
147
private final Surrogate.Parser sgp = new Surrogate.Parser();
148
private CoderResult encodeArrayLoop(CharBuffer src,
149
ByteBuffer dst)
150
{
151
char[] sa = src.array();
152
int sp = src.arrayOffset() + src.position();
153
int sl = src.arrayOffset() + src.limit();
154
assert (sp <= sl);
155
sp = (sp <= sl ? sp : sl);
156
byte[] da = dst.array();
157
int dp = dst.arrayOffset() + dst.position();
158
int dl = dst.arrayOffset() + dst.limit();
159
assert (dp <= dl);
160
dp = (dp <= dl ? dp : dl);
161
162
try {
163
while (sp < sl) {
164
char c = sa[sp];
165
if (c < 0x80) {
166
if (dp >= dl)
167
return CoderResult.OVERFLOW;
168
da[dp] = (byte)c;
169
sp++; dp++;
170
continue;
171
}
172
if (sgp.parse(c, sa, sp, sl) < 0)
173
return sgp.error();
174
return sgp.unmappableResult();
175
}
176
return CoderResult.UNDERFLOW;
177
} finally {
178
src.position(sp - src.arrayOffset());
179
dst.position(dp - dst.arrayOffset());
180
}
181
}
182
183
private CoderResult encodeBufferLoop(CharBuffer src,
184
ByteBuffer dst)
185
{
186
int mark = src.position();
187
try {
188
while (src.hasRemaining()) {
189
char c = src.get();
190
if (c < 0x80) {
191
if (!dst.hasRemaining())
192
return CoderResult.OVERFLOW;
193
dst.put((byte)c);
194
mark++;
195
continue;
196
}
197
if (sgp.parse(c, src) < 0)
198
return sgp.error();
199
return sgp.unmappableResult();
200
}
201
return CoderResult.UNDERFLOW;
202
} finally {
203
src.position(mark);
204
}
205
}
206
207
protected CoderResult encodeLoop(CharBuffer src,
208
ByteBuffer dst)
209
{
210
if (src.hasArray() && dst.hasArray())
211
return encodeArrayLoop(src, dst);
212
else
213
return encodeBufferLoop(src, dst);
214
}
215
216
}
217
}
218
219