Path: blob/master/test/jdk/java/nio/channels/FileChannel/Mode.java
41154 views
/*1* Copyright (c) 2001, 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 446229825* @summary Test FileChannel maps with different accesses26* @run main/othervm Mode27*/2829import java.nio.channels.*;30import java.nio.MappedByteBuffer;31import java.io.*;323334public class Mode {35private static File testFile;3637public static void main(String[] args) throws Exception {38testFile = File.createTempFile("testFile", null);39testFile.deleteOnExit();40testReadable();41testWritable();42}4344private static void testReadable() throws IOException {45FileInputStream is = new FileInputStream(testFile);46FileChannel channel = is.getChannel();47try {48MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_WRITE,490, 8);50throw new RuntimeException("Exception expected, none thrown");51} catch (NonWritableChannelException e) {52// correct result53}54is.close();55}5657private static void testWritable() throws IOException {58FileOutputStream is = new FileOutputStream(testFile);59FileChannel channel = is.getChannel();60try {61MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY,620, 8);63throw new RuntimeException("Exception expected, none thrown");64} catch (NonReadableChannelException e) {65// correct result66}67is.close();68}6970}717273