Surrogates.java revision 2115:946b30073247
1/*
2 * Copyright 2010 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @summary Check that array-crossing surrogate pairs are handled properly
26 */
27
28import java.io.*;
29import java.nio.*;
30import java.nio.charset.*;
31
32
33public class Surrogates {
34
35    static PrintStream log = System.err;
36
37    static char[] input;
38    static byte[] output;
39
40    static final int LEN = 128;
41
42    static void initData() throws IOException {
43        StringBuffer sb = new StringBuffer();
44        for (int i = 0; i < LEN; i++) {
45            int c = Surrogate.UCS4_SURROGATE_MIN + 1;
46            sb.append(Surrogate.high(c));
47            sb.append(Surrogate.low(c));
48        }
49        input = sb.toString().toCharArray();
50        ByteArrayOutputStream bos = new ByteArrayOutputStream();
51        OutputStreamWriter osw
52            = new OutputStreamWriter(bos, Charset.forName("UTF-8"));
53        osw.write(input);
54        osw.close();
55        output = bos.toByteArray();
56    }
57
58    static void testLeftovers(boolean doMalformed)
59        throws Exception
60    {
61        log.print("Leftover surrogates, doMalformed = " + doMalformed);
62        ByteArrayOutputStream bos = new ByteArrayOutputStream();
63        OutputStreamWriter osw
64            = new OutputStreamWriter(bos, Charset.forName("UTF-8"));
65        for (int i = 0; i < input.length; i += 7)
66            osw.write(input, i, Math.min(input.length - i, 7));
67        if (doMalformed)
68            osw.write(input, 0, 1);
69        osw.close();
70        byte[] result = bos.toByteArray();
71
72        // Ignore a trailing '?' if we wrote a malformed final surrogate
73        int rl = result.length + (doMalformed ? -1 : 0);
74
75        if (rl != output.length)
76            throw new Exception("Incorrect result length "
77                                + rl + ", expected " + output.length);
78        for (int i = 0; i < output.length; i++)
79            if (result[i] != output[i])
80                throw new Exception("Unexpected result value at index " + i);
81        log.println(": Passed");
82    }
83
84    public static void main(String[] args) throws Exception {
85        initData();
86        testLeftovers(false);
87        testLeftovers(true);
88    }
89
90}
91