Path: blob/master/test/jdk/java/nio/charset/coders/IsLegalReplacement.java
41153 views
/*1* Copyright (c) 2010, 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 482128625* @summary Check correctness of CharsetEncoder.isLegalReplacement(byte[])26*/2728import java.io.*;29import java.nio.*;30import java.nio.charset.*;31import java.util.*;323334public class IsLegalReplacement {3536static PrintStream out = System.err;37static int errors = 0;3839static String toString(byte[] ba) {40StringBuffer sb = new StringBuffer();41for (int i = 0; i < ba.length; i++) {42byte b = ba[i];43if (i > 0)44sb.append(' ');45sb.append(Integer.toHexString((b >> 4) & 0xf));46sb.append(Integer.toHexString((b >> 0) & 0xf));47}48return sb.toString();49}5051static CoderResult ilr(String csn, byte[] repl) {52CharsetDecoder dec = Charset.forName(csn).newDecoder();53dec.onMalformedInput(CodingErrorAction.REPORT);54dec.onUnmappableCharacter(CodingErrorAction.REPORT);55ByteBuffer bb = ByteBuffer.wrap(repl);56CharBuffer cb = CharBuffer.allocate((int)(bb.remaining()57* dec.maxCharsPerByte()));58return dec.decode(bb, cb, true);59}6061static void test(String csn, byte[] repl, boolean expected)62throws Exception63{64CharsetEncoder enc = Charset.forName(csn).newEncoder();65out.print(csn + ": " + toString(repl) + ": ");66if (enc.isLegalReplacement(repl) == expected) {67out.print("Okay");68} else {69out.print("Wrong: Expected " + expected);70errors++;71}72out.println(" (" + ilr(csn, repl) + ")");73}7475public static void main(String[] args) throws Exception {7677test("UTF-16", new byte [] { (byte)0xd8, 0, (byte)0xdc, 0 }, true);78test("UTF-16", new byte [] { (byte)0xdc, 0, (byte)0xd8, 0 }, false);79test("UTF-16", new byte [] { (byte)0xd8, 0 }, false);80test("UTF-16BE", new byte [] { (byte)0xd8, 0, (byte)0xdc, 0 }, true);81test("UTF-16BE", new byte [] { (byte)0xdc, 0, (byte)0xd8, 0 }, false);82test("UTF-16BE", new byte [] { (byte)0xd8, 0 }, false);83test("UTF-16LE", new byte [] { 0, (byte)0xd8, 0, (byte)0xdc }, true);84test("UTF-16LE", new byte [] { 0, (byte)0xdc, 0, (byte)0xd8 }, false);85test("UTF-16LE", new byte [] { 0, (byte)0xd8 }, false);8687if (errors > 0)88throw new Exception(errors + " error(s) occurred");8990}9192}939495