Path: blob/master/test/jdk/sun/nio/cs/StreamEncoderClose.java
41149 views
/*1* Copyright (c) 2008, 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* @bug 500542625* @summary Check if StreamEncoder close() method works correctly from26* error recovery after the underneath OutputStream failed to27* close the first time.28* @modules jdk.charsets29*/3031import java.io.*;32public class StreamEncoderClose {33public static void main( String arg[] ) throws Exception {34byte[] expected = {(byte)0x1b,(byte)0x24,(byte)0x42,35(byte)0x30,(byte)0x6c,36(byte)0x1b,(byte)0x28,(byte)0x42};37ByteArrayOutputStream baos = new ByteArrayOutputStream();38MyBufferedOutputStream mbos = new MyBufferedOutputStream(baos);39PrintWriter pw = new PrintWriter(new OutputStreamWriter(mbos, "ISO-2022-JP"));40mbos.dontClose();41pw.write("\u4e00");42pw.close(); // 1st PrintWriter Close43mbos.canClose();44pw.close(); // 2nd PrintWriter Close4546//double check, probably not necessary47byte[] out = baos.toByteArray();48if (out.length != expected.length) {49throw new IOException("Failed");50}51for (int i = 0; i < out.length; i++) {52//System.out.printf("(byte)0x%x,", out[i] & 0xff);53if (out[i] != expected[i])54throw new IOException("Failed");55}56}5758static class MyBufferedOutputStream extends BufferedOutputStream {59MyBufferedOutputStream(OutputStream os) {60super(os);61}62private boolean status;63public void dontClose() {64status = false;65}66public void canClose() {67status = true;68}69public void close() throws IOException {70if ( status == false ) {71throw new IOException("Can't close ");72}73super.close();74}75}76}777879