0N/A/*
157N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
157N/A * published by the Free Software Foundation.
0N/A *
157N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
157N/A * questions.
157N/A */
157N/A
0N/A/* @test
0N/A * @summary Check that array-crossing surrogate pairs are handled properly
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.nio.*;
0N/Aimport java.nio.charset.*;
0N/A
0N/A
0N/Apublic class Surrogates {
0N/A
0N/A static PrintStream log = System.err;
0N/A
0N/A static char[] input;
0N/A static byte[] output;
0N/A
0N/A static final int LEN = 128;
0N/A
0N/A static void initData() throws IOException {
0N/A StringBuffer sb = new StringBuffer();
0N/A for (int i = 0; i < LEN; i++) {
0N/A int c = Character.MIN_SUPPLEMENTARY_CODE_POINT + 1;
0N/A sb.append(Character.toChars(c));
0N/A }
0N/A input = sb.toString().toCharArray();
0N/A ByteArrayOutputStream bos = new ByteArrayOutputStream();
0N/A OutputStreamWriter osw
0N/A = new OutputStreamWriter(bos, Charset.forName("UTF-8"));
0N/A osw.write(input);
0N/A osw.close();
0N/A output = bos.toByteArray();
0N/A }
0N/A
0N/A static void testLeftovers(boolean doMalformed)
0N/A throws Exception
0N/A {
0N/A log.print("Leftover surrogates, doMalformed = " + doMalformed);
0N/A ByteArrayOutputStream bos = new ByteArrayOutputStream();
0N/A OutputStreamWriter osw
0N/A = new OutputStreamWriter(bos, Charset.forName("UTF-8"));
0N/A for (int i = 0; i < input.length; i += 7)
0N/A osw.write(input, i, Math.min(input.length - i, 7));
0N/A if (doMalformed)
0N/A osw.write(input, 0, 1);
0N/A osw.close();
0N/A byte[] result = bos.toByteArray();
0N/A
0N/A // Ignore a trailing '?' if we wrote a malformed final surrogate
0N/A int rl = result.length + (doMalformed ? -1 : 0);
0N/A
0N/A if (rl != output.length)
0N/A throw new Exception("Incorrect result length "
0N/A + rl + ", expected " + output.length);
0N/A for (int i = 0; i < output.length; i++)
0N/A if (result[i] != output[i])
0N/A throw new Exception("Unexpected result value at index " + i);
0N/A log.println(": Passed");
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A initData();
0N/A testLeftovers(false);
0N/A testLeftovers(true);
0N/A }
0N/A
}