Path: blob/master/test/jdk/java/nio/channels/FileChannel/ClosedChannelTransfer.java
41154 views
/*1* Copyright (c) 2002, 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* @bug 4659408 465941025* @summary Verify transfers with closed channels throws ClosedChannelException26*/2728import java.nio.*;29import java.nio.channels.*;30import java.io.*;3132public class ClosedChannelTransfer {33public static void main (String args []) throws Exception {34File file = File.createTempFile("test1", null);35file.deleteOnExit();36FileChannel channel = (new RandomAccessFile("aaa","rw")).getChannel();37test1(channel);38test2(channel);39channel.close();40file.delete();41}4243static void test1(FileChannel channel) throws Exception {44ByteArrayInputStream istr = new ByteArrayInputStream(45new byte [] {1, 2, 3, 4}46);47ReadableByteChannel rbc = Channels.newChannel(istr);48rbc.close();49try {50channel.transferFrom(rbc, 0, 2);51throw new Exception("Test1: No ClosedChannelException was thrown");52} catch (ClosedChannelException cce) {53// Correct result54}55}5657static void test2(FileChannel channel) throws Exception {58ByteArrayOutputStream istr = new ByteArrayOutputStream(4);59WritableByteChannel wbc = Channels.newChannel(istr);60wbc.close();61try {62channel.transferTo(0, 2, wbc);63throw new Exception("Test2: No ClosedChannelException was thrown");64} catch (ClosedChannelException cce) {65// Correct result66}67}68}697071