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