Path: blob/master/test/jdk/java/nio/charset/coders/IOCoders.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* @summary Unit test for ISR/OSW constructors that take coders25*/2627import java.io.*;28import java.nio.*;29import java.nio.charset.*;303132public class IOCoders {3334static Charset ascii = Charset.forName("US-ASCII");3536static void isrPositive() throws Exception {37ByteArrayInputStream bis38= new ByteArrayInputStream(new byte[] { (byte)'h', (byte)'i' });39InputStreamReader isr40= new InputStreamReader(bis,41ascii.newDecoder()42.onMalformedInput(CodingErrorAction.REPORT)43.onUnmappableCharacter(CodingErrorAction.REPORT));44BufferedReader br = new BufferedReader(isr);45if (!br.readLine().equals("hi"))46throw new Exception();47}4849static void isrNegative() throws Exception {50ByteArrayInputStream bis51= new ByteArrayInputStream(new byte[] { (byte)0xff, (byte)0xff });52InputStreamReader isr53= new InputStreamReader(bis,54ascii.newDecoder()55.onMalformedInput(CodingErrorAction.REPORT)56.onUnmappableCharacter(CodingErrorAction.REPORT));57BufferedReader br = new BufferedReader(isr);58try {59br.readLine();60} catch (MalformedInputException x) {61return;62}63throw new Exception();64}6566static void oswPositive() throws Exception {67ByteArrayOutputStream bos = new ByteArrayOutputStream();68OutputStreamWriter osw69= new OutputStreamWriter(bos,70ascii.newEncoder()71.onMalformedInput(CodingErrorAction.REPORT)72.onUnmappableCharacter(CodingErrorAction.REPORT));73osw.write("hi");74osw.close();75if (!ascii.decode(ByteBuffer.wrap(bos.toByteArray()))76.toString().equals("hi"))77throw new Exception();78}7980static void oswNegative() throws Exception {81ByteArrayOutputStream bos = new ByteArrayOutputStream();82OutputStreamWriter osw83= new OutputStreamWriter(bos,84ascii.newEncoder()85.onMalformedInput(CodingErrorAction.REPORT)86.onUnmappableCharacter(CodingErrorAction.REPORT));87try {88osw.write("\u00A0\u00A1");89} catch (UnmappableCharacterException x) {90return;91}92throw new Exception();93}9495public static void main(String[] args) throws Exception {96isrPositive();97isrNegative();98oswPositive();99oswNegative();100}101102}103104105