1/*
2 * Copyright (c) 2010, 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 * @summary Unit test for ISR/OSW constructors that take coders
26 */
27
28import java.io.*;
29import java.nio.*;
30import java.nio.charset.*;
31
32
33public class IOCoders {
34
35    static Charset ascii = Charset.forName("US-ASCII");
36
37    static void isrPositive() throws Exception {
38        ByteArrayInputStream bis
39            = new ByteArrayInputStream(new byte[] { (byte)'h', (byte)'i' });
40        InputStreamReader isr
41            = new InputStreamReader(bis,
42                                    ascii.newDecoder()
43                                    .onMalformedInput(CodingErrorAction.REPORT)
44                                    .onUnmappableCharacter(CodingErrorAction.REPORT));
45        BufferedReader br = new BufferedReader(isr);
46        if (!br.readLine().equals("hi"))
47            throw new Exception();
48    }
49
50    static void isrNegative() throws Exception {
51        ByteArrayInputStream bis
52            = new ByteArrayInputStream(new byte[] { (byte)0xff, (byte)0xff });
53        InputStreamReader isr
54            = new InputStreamReader(bis,
55                                    ascii.newDecoder()
56                                    .onMalformedInput(CodingErrorAction.REPORT)
57                                    .onUnmappableCharacter(CodingErrorAction.REPORT));
58        BufferedReader br = new BufferedReader(isr);
59        try {
60            br.readLine();
61        } catch (MalformedInputException x) {
62            return;
63        }
64        throw new Exception();
65    }
66
67    static void oswPositive() throws Exception {
68        ByteArrayOutputStream bos = new ByteArrayOutputStream();
69        OutputStreamWriter osw
70            = new OutputStreamWriter(bos,
71                                     ascii.newEncoder()
72                                     .onMalformedInput(CodingErrorAction.REPORT)
73                                     .onUnmappableCharacter(CodingErrorAction.REPORT));
74        osw.write("hi");
75        osw.close();
76        if (!ascii.decode(ByteBuffer.wrap(bos.toByteArray()))
77            .toString().equals("hi"))
78            throw new Exception();
79    }
80
81    static void oswNegative() throws Exception {
82        ByteArrayOutputStream bos = new ByteArrayOutputStream();
83        OutputStreamWriter osw
84            = new OutputStreamWriter(bos,
85                                     ascii.newEncoder()
86                                     .onMalformedInput(CodingErrorAction.REPORT)
87                                     .onUnmappableCharacter(CodingErrorAction.REPORT));
88        try {
89            osw.write("\u00A0\u00A1");
90        } catch (UnmappableCharacterException x) {
91            return;
92        }
93        throw new Exception();
94    }
95
96    public static void main(String[] args) throws Exception {
97        isrPositive();
98        isrNegative();
99        oswPositive();
100        oswNegative();
101    }
102
103}
104