1/*
2 * Copyright (c) 2008, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25 * @bug 4122961
26 * @summary Verify that converters don't drop characters on buffer boundaries
27
28 * This is a slightly modified version of the attachment supplied with the
29 * bug report.
30 * @modules jdk.charsets
31 */
32import java.io.*;
33
34public class TestConverterDroppedCharacters {
35    public static void main(String args[])
36        throws java.io.IOException, java.io.UnsupportedEncodingException,
37               java.io.FileNotFoundException
38    {
39        /* Try misc. encodings, many are broken. */
40        tryEncoding("Big5");
41        tryEncoding("CNS11643");
42        tryEncoding("Cp1006");
43        tryEncoding("Cp1381");
44        tryEncoding("Cp33722");
45        tryEncoding("GB2312");
46        tryEncoding("KSC5601");
47        tryEncoding("SJIS");
48        tryEncoding("UTF8");
49    }
50
51    static void tryEncoding(String encoding)
52        throws java.io.IOException, java.io.UnsupportedEncodingException,
53               java.io.FileNotFoundException
54    {
55        String filename = "OUTPUT";
56        int goesBadAfter = 8193;
57        int i;
58        char data[] = new char[goesBadAfter+1];
59
60        System.out.println("Testing " + encoding);
61
62        /* Create some data */
63        for(i = 0; i < goesBadAfter; i++) {
64            data[i] = (char)((i % 0x7f) + 1);
65        }
66
67        /* Write the data out to a file. */
68        FileOutputStream fout = new FileOutputStream(filename);
69        OutputStreamWriter ow = new OutputStreamWriter(fout, encoding);
70        BufferedWriter fd     = new BufferedWriter(ow);
71        fd.write(data,0,goesBadAfter);
72        fd.close();
73
74        /* Now read it back with the same encoding. */
75        char buf[] = new char[goesBadAfter+1];
76        FileInputStream fin = new FileInputStream("OUTPUT");
77        InputStreamReader ir = new InputStreamReader(fin, encoding);
78        ir.read(buf,0,goesBadAfter);
79        ir.close();
80
81        /* And check to see if what we wrote is what we got back. */
82        for(i = 0; i < goesBadAfter; i++) {
83            if (data[i] != buf[i]) {
84                System.out.println("ERROR with encoding " + encoding
85                                   + ": Data wrong at position " + i + "   "
86                                   + "in: " + (int)data[i] + "   "
87                                   + "out: " + (int)buf[i]);
88                throw new RuntimeException();
89            }
90        }
91        System.out.println("Successfully tested " + encoding);
92    }
93}
94