Path: blob/master/test/jdk/java/nio/channels/FileChannel/GetClosedChannel.java
41154 views
/*1* Copyright (c) 2014, 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/*24* @test25* @bug 802561926* @summary Verify that a channel obtained from a closed stream is truly closed.27*/28import java.io.File;29import java.io.FileInputStream;30import java.io.FileOutputStream;31import java.io.IOException;32import java.io.RandomAccessFile;33import java.nio.channels.ClosedChannelException;34import java.nio.channels.FileChannel;3536public class GetClosedChannel {37private static final int NUM_CHANNELS = 3;38private static final int NUM_EXCEPTIONS = 2*NUM_CHANNELS;3940public static void main(String[] args) throws IOException {41int exceptions = 0;42int openChannels = 0;4344for (int i = 0; i < NUM_CHANNELS; i++) {45File f = File.createTempFile("fcbug", ".tmp");46f.deleteOnExit();4748FileChannel fc = null;49boolean shared = false;50switch (i) {51case 0:52System.out.print("FileInputStream...");53FileInputStream fis = new FileInputStream(f);54fis.close();55fc = fis.getChannel();56if (fc.isOpen()) {57System.err.println("FileInputStream channel should not be open");58openChannels++;59}60shared = true;61break;62case 1:63System.out.print("FileOutputStream...");64FileOutputStream fos = new FileOutputStream(f);65fos.close();66fc = fos.getChannel();67if (fc.isOpen()) {68System.err.println("FileOutputStream channel should not be open");69openChannels++;70}71break;72case 2:73System.out.print("RandomAccessFile...");74RandomAccessFile raf = new RandomAccessFile(f, "rw");75raf.close();76fc = raf.getChannel();77if (fc.isOpen()) {78System.err.println("RandomAccessFile channel should not be open");79openChannels++;80}81break;82default:83assert false : "Should not get here";84}8586try {87long position = fc.position();88System.err.println("Channel "+i+" position is "+position);89} catch (ClosedChannelException cce) {90exceptions++;91}9293try {94fc.tryLock(0, Long.MAX_VALUE, shared);95} catch (ClosedChannelException e) {96System.out.println("OK");97exceptions++;98} catch (Error err) {99System.err.println(err);100}101}102103if (exceptions != NUM_EXCEPTIONS || openChannels != 0) {104throw new RuntimeException("FAILED:" +105" ClosedChannelExceptions: expected: " + NUM_EXCEPTIONS +106" actual: " + exceptions + ";" + System.lineSeparator() +107" number of open channels: expected: 0 " +108" actual: " + openChannels + ".");109}110}111}112113114