Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/nio/cs/StreamEncoderClose.java
38839 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 from26error recovery after the underneath OutputStream failed to27close the first time.28*/2930import java.io.*;31public class StreamEncoderClose {32public static void main( String arg[] ) throws Exception {33byte[] expected = {(byte)0x1b,(byte)0x24,(byte)0x42,34(byte)0x30,(byte)0x6c,35(byte)0x1b,(byte)0x28,(byte)0x42};36ByteArrayOutputStream baos = new ByteArrayOutputStream();37MyBufferedOutputStream mbos = new MyBufferedOutputStream(baos);38PrintWriter pw = new PrintWriter(new OutputStreamWriter(mbos, "ISO-2022-JP"));39mbos.dontClose();40pw.write("\u4e00");41pw.close(); // 1st PrintWriter Close42mbos.canClose();43pw.close(); // 2nd PrintWriter Close4445//double check, probably not necessary46byte[] out = baos.toByteArray();47if (out.length != expected.length) {48throw new IOException("Failed");49}50for (int i = 0; i < out.length; i++) {51//System.out.printf("(byte)0x%x,", out[i] & 0xff);52if (out[i] != expected[i])53throw new IOException("Failed");54}55}5657static class MyBufferedOutputStream extends BufferedOutputStream {58MyBufferedOutputStream(OutputStream os) {59super(os);60}61private boolean status;62public void dontClose() {63status = false;64}65public void canClose() {66status = true;67}68public void close() throws IOException {69if ( status == false ) {70throw new IOException("Can't close ");71}72super.close();73}74}75}767778