This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
Unicode.java
151 lines (134 loc) · 4.95 KB
/
Unicode.java
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (c) 1999, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
@bug 4241440 4220470
@summary Test the various two-byte Unicode encodings
@run main Unicode UnicodeLittle little true
@run main Unicode UnicodeBig big true
@run main Unicode UnicodeLittleUnmarked little false
@run main Unicode UnicodeBigUnmarked big false
@run main Unicode iso-10646-ucs-2 big false
@run main Unicode x-utf-16be big false
@run main Unicode x-utf-16le little false
*/
import java.io.ByteArrayOutputStream;
public class Unicode {
static final int BOM_HIGH = 0xfe;
static final int BOM_LOW = 0xff;
static final int BIG = 0;
static final int LITTLE = 1;
static void fail(String enc, String msg, int e0, int e1, int b0, int b1)
throws Exception
{
throw new Exception(enc + ": " + msg
+ ": Expected "
+ Integer.toHexString(e0)
+ " " + Integer.toHexString(e1)
+ ", got "
+ Integer.toHexString(b0)
+ " " + Integer.toHexString(b1));
}
/* Chars to bytes */
static void encode(String enc, int byteOrder, boolean markExpected)
throws Exception
{
String s = "abc";
byte[] b = s.getBytes(enc);
int i = 0;
if (markExpected) {
int b0 = b[i++] & 0xff;
int b1 = b[i++] & 0xff;
int e0 = 0, e1 = 0;
if (byteOrder == BIG) {
e0 = BOM_HIGH;
e1 = BOM_LOW;
} else if (byteOrder == LITTLE) {
e0 = BOM_LOW;
e1 = BOM_HIGH;
}
if ((b0 != e0) || (b1 != e1))
fail(enc, "Incorrect or missing byte-order mark",
e0, e1, b0, b1);
}
for (int j = 0; j < s.length(); j++) {
char c = s.charAt(j);
int b0 = b[i++] & 0xff;
int b1 = b[i++] & 0xff;
int e0 = 0, e1 = 0;
if (byteOrder == BIG) {
e0 = c >> 8;
e1 = c & 0xff;
} else if (byteOrder == LITTLE) {
e0 = c & 0xff;
e1 = c >> 8;
}
if ((b0 != e0) || (b1 != e1))
fail(enc, "Incorrect content at index " + j,
e0, e1, b0, b1);
}
}
/* Bytes to chars */
static void decode(String enc, int byteOrder, boolean markit)
throws Exception
{
String s = "abc";
ByteArrayOutputStream bo = new ByteArrayOutputStream();
if (markit) {
if (byteOrder == BIG) {
bo.write(BOM_HIGH);
bo.write(BOM_LOW);
} else if (byteOrder == LITTLE) {
bo.write(BOM_LOW);
bo.write(BOM_HIGH);
}
}
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (byteOrder == BIG) {
bo.write(c >> 8);
bo.write(c & 0xff);
} else if (byteOrder == LITTLE) {
bo.write(c & 0xff);
bo.write(c >> 8);
}
}
byte[] b = bo.toByteArray();
String s2 = new String(b, enc);
if (!s.equals(s2))
throw new Exception(enc + ": Decode error");
}
public static void main(String[] args) throws Exception {
String enc = args[0];
String bos = args[1];
boolean markExpected = Boolean.valueOf(args[2]).booleanValue();
int byteOrder = -1;
if (bos.equals("big")) byteOrder = BIG;
if (bos.equals("little")) byteOrder = LITTLE;
/* We run each test twice in order to check the repeatability of
String.getBytes and String(byte[], String) (4220470) */
encode(enc, byteOrder, markExpected);
encode(enc, byteOrder, markExpected);
decode(enc, byteOrder, markExpected);
decode(enc, byteOrder, markExpected);
}
}