Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/Unicode.java
41145 views
1
/*
2
* Copyright (c) 1999, 2005, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/* @test
25
@bug 4241440 4220470
26
@summary Test the various two-byte Unicode encodings
27
28
@run main Unicode UnicodeLittle little true
29
@run main Unicode UnicodeBig big true
30
@run main Unicode UnicodeLittleUnmarked little false
31
@run main Unicode UnicodeBigUnmarked big false
32
@run main Unicode iso-10646-ucs-2 big false
33
@run main Unicode x-utf-16be big false
34
@run main Unicode x-utf-16le little false
35
*/
36
37
import java.io.ByteArrayOutputStream;
38
39
40
public class Unicode {
41
42
static final int BOM_HIGH = 0xfe;
43
static final int BOM_LOW = 0xff;
44
45
static final int BIG = 0;
46
static final int LITTLE = 1;
47
48
49
static void fail(String enc, String msg, int e0, int e1, int b0, int b1)
50
throws Exception
51
{
52
throw new Exception(enc + ": " + msg
53
+ ": Expected "
54
+ Integer.toHexString(e0)
55
+ " " + Integer.toHexString(e1)
56
+ ", got "
57
+ Integer.toHexString(b0)
58
+ " " + Integer.toHexString(b1));
59
}
60
61
62
/* Chars to bytes */
63
static void encode(String enc, int byteOrder, boolean markExpected)
64
throws Exception
65
{
66
String s = "abc";
67
byte[] b = s.getBytes(enc);
68
int i = 0;
69
if (markExpected) {
70
int b0 = b[i++] & 0xff;
71
int b1 = b[i++] & 0xff;
72
int e0 = 0, e1 = 0;
73
if (byteOrder == BIG) {
74
e0 = BOM_HIGH;
75
e1 = BOM_LOW;
76
} else if (byteOrder == LITTLE) {
77
e0 = BOM_LOW;
78
e1 = BOM_HIGH;
79
}
80
if ((b0 != e0) || (b1 != e1))
81
fail(enc, "Incorrect or missing byte-order mark",
82
e0, e1, b0, b1);
83
}
84
for (int j = 0; j < s.length(); j++) {
85
char c = s.charAt(j);
86
int b0 = b[i++] & 0xff;
87
int b1 = b[i++] & 0xff;
88
int e0 = 0, e1 = 0;
89
if (byteOrder == BIG) {
90
e0 = c >> 8;
91
e1 = c & 0xff;
92
} else if (byteOrder == LITTLE) {
93
e0 = c & 0xff;
94
e1 = c >> 8;
95
}
96
if ((b0 != e0) || (b1 != e1))
97
fail(enc, "Incorrect content at index " + j,
98
e0, e1, b0, b1);
99
}
100
}
101
102
103
/* Bytes to chars */
104
static void decode(String enc, int byteOrder, boolean markit)
105
throws Exception
106
{
107
String s = "abc";
108
ByteArrayOutputStream bo = new ByteArrayOutputStream();
109
if (markit) {
110
if (byteOrder == BIG) {
111
bo.write(BOM_HIGH);
112
bo.write(BOM_LOW);
113
} else if (byteOrder == LITTLE) {
114
bo.write(BOM_LOW);
115
bo.write(BOM_HIGH);
116
}
117
}
118
for (int i = 0; i < s.length(); i++) {
119
char c = s.charAt(i);
120
if (byteOrder == BIG) {
121
bo.write(c >> 8);
122
bo.write(c & 0xff);
123
} else if (byteOrder == LITTLE) {
124
bo.write(c & 0xff);
125
bo.write(c >> 8);
126
}
127
}
128
byte[] b = bo.toByteArray();
129
String s2 = new String(b, enc);
130
if (!s.equals(s2))
131
throw new Exception(enc + ": Decode error");
132
}
133
134
135
public static void main(String[] args) throws Exception {
136
String enc = args[0];
137
String bos = args[1];
138
boolean markExpected = Boolean.valueOf(args[2]).booleanValue();
139
int byteOrder = -1;
140
if (bos.equals("big")) byteOrder = BIG;
141
if (bos.equals("little")) byteOrder = LITTLE;
142
143
/* We run each test twice in order to check the repeatability of
144
String.getBytes and String(byte[], String) (4220470) */
145
encode(enc, byteOrder, markExpected);
146
encode(enc, byteOrder, markExpected);
147
decode(enc, byteOrder, markExpected);
148
decode(enc, byteOrder, markExpected);
149
}
150
151
}
152
153