Path: blob/master/test/jdk/java/io/Unicode.java
41145 views
/*1* Copyright (c) 1999, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 4241440 422047025@summary Test the various two-byte Unicode encodings2627@run main Unicode UnicodeLittle little true28@run main Unicode UnicodeBig big true29@run main Unicode UnicodeLittleUnmarked little false30@run main Unicode UnicodeBigUnmarked big false31@run main Unicode iso-10646-ucs-2 big false32@run main Unicode x-utf-16be big false33@run main Unicode x-utf-16le little false34*/3536import java.io.ByteArrayOutputStream;373839public class Unicode {4041static final int BOM_HIGH = 0xfe;42static final int BOM_LOW = 0xff;4344static final int BIG = 0;45static final int LITTLE = 1;464748static void fail(String enc, String msg, int e0, int e1, int b0, int b1)49throws Exception50{51throw new Exception(enc + ": " + msg52+ ": Expected "53+ Integer.toHexString(e0)54+ " " + Integer.toHexString(e1)55+ ", got "56+ Integer.toHexString(b0)57+ " " + Integer.toHexString(b1));58}596061/* Chars to bytes */62static void encode(String enc, int byteOrder, boolean markExpected)63throws Exception64{65String s = "abc";66byte[] b = s.getBytes(enc);67int i = 0;68if (markExpected) {69int b0 = b[i++] & 0xff;70int b1 = b[i++] & 0xff;71int e0 = 0, e1 = 0;72if (byteOrder == BIG) {73e0 = BOM_HIGH;74e1 = BOM_LOW;75} else if (byteOrder == LITTLE) {76e0 = BOM_LOW;77e1 = BOM_HIGH;78}79if ((b0 != e0) || (b1 != e1))80fail(enc, "Incorrect or missing byte-order mark",81e0, e1, b0, b1);82}83for (int j = 0; j < s.length(); j++) {84char c = s.charAt(j);85int b0 = b[i++] & 0xff;86int b1 = b[i++] & 0xff;87int e0 = 0, e1 = 0;88if (byteOrder == BIG) {89e0 = c >> 8;90e1 = c & 0xff;91} else if (byteOrder == LITTLE) {92e0 = c & 0xff;93e1 = c >> 8;94}95if ((b0 != e0) || (b1 != e1))96fail(enc, "Incorrect content at index " + j,97e0, e1, b0, b1);98}99}100101102/* Bytes to chars */103static void decode(String enc, int byteOrder, boolean markit)104throws Exception105{106String s = "abc";107ByteArrayOutputStream bo = new ByteArrayOutputStream();108if (markit) {109if (byteOrder == BIG) {110bo.write(BOM_HIGH);111bo.write(BOM_LOW);112} else if (byteOrder == LITTLE) {113bo.write(BOM_LOW);114bo.write(BOM_HIGH);115}116}117for (int i = 0; i < s.length(); i++) {118char c = s.charAt(i);119if (byteOrder == BIG) {120bo.write(c >> 8);121bo.write(c & 0xff);122} else if (byteOrder == LITTLE) {123bo.write(c & 0xff);124bo.write(c >> 8);125}126}127byte[] b = bo.toByteArray();128String s2 = new String(b, enc);129if (!s.equals(s2))130throw new Exception(enc + ": Decode error");131}132133134public static void main(String[] args) throws Exception {135String enc = args[0];136String bos = args[1];137boolean markExpected = Boolean.valueOf(args[2]).booleanValue();138int byteOrder = -1;139if (bos.equals("big")) byteOrder = BIG;140if (bos.equals("little")) byteOrder = LITTLE;141142/* We run each test twice in order to check the repeatability of143String.getBytes and String(byte[], String) (4220470) */144encode(enc, byteOrder, markExpected);145encode(enc, byteOrder, markExpected);146decode(enc, byteOrder, markExpected);147decode(enc, byteOrder, markExpected);148}149150}151152153