Path: blob/master/test/jdk/java/nio/channels/FileChannel/Write.java
41154 views
/*1* Copyright (c) 2001, 2020, 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 4475533 4698138 4638365 479622126* @summary Test FileChannel write27* @run main/othervm Write28*/2930import java.nio.channels.*;31import java.nio.*;32import java.io.*;3334public class Write {3536public static void main(String[] args) throws Exception {37test1(); // for bug 447553338test2();39test3(); // for bug 469813840}4142// Test to see that offset > length does not throw exception43static void test1() throws Exception {44ByteBuffer[] dsts = new ByteBuffer[4];45for (int i=0; i<4; i++)46dsts[i] = ByteBuffer.allocateDirect(10);4748File testFile = File.createTempFile("test1", null);49try {50FileOutputStream fos = new FileOutputStream(testFile);51FileChannel fc = fos.getChannel();52fc.write(dsts, 2, 1);53fos.close();54} finally {55testFile.delete();56}57}5859// Test to see that the appropriate buffers are updated60static void test2() throws Exception {61File testFile = File.createTempFile("test2", null);62testFile.delete();63ByteBuffer[] srcs = new ByteBuffer[4];64for (int i=0; i<4; i++)65srcs[i] = ByteBuffer.allocateDirect(10);6667srcs[0].put((byte)1); srcs[0].flip();68srcs[1].put((byte)2); srcs[1].flip();69srcs[2].put((byte)3); srcs[2].flip();70srcs[3].put((byte)4); srcs[3].flip();7172FileOutputStream fos = new FileOutputStream(testFile);73FileChannel fc = fos.getChannel();74try {75fc.write(srcs, 1, 2);76} finally {77fc.close();78}7980FileInputStream fis = new FileInputStream(testFile);81fc = fis.getChannel();82try {83ByteBuffer bb = ByteBuffer.allocateDirect(10);84fc.read(bb);85bb.flip();86if (bb.get() != 2)87throw new RuntimeException("Write failure");88if (bb.get() != 3)89throw new RuntimeException("Write failure");90try {91bb.get();92throw new RuntimeException("Write failure");93} catch (BufferUnderflowException bufe) {94// correct result95}96} finally {97fc.close();98}99100// eagerly clean-up101testFile.delete();102}103104// Test write to a negative position (bug 4698138).105static void test3() throws Exception {106File testFile = File.createTempFile("test1", null);107testFile.deleteOnExit();108ByteBuffer dst = ByteBuffer.allocate(10);109FileOutputStream fos = new FileOutputStream(testFile);110FileChannel fc = fos.getChannel();111try {112fc.write(dst, -1);113throw new RuntimeException("Expected IAE not thrown");114} catch (IllegalArgumentException iae) {115// Correct result116} finally {117fos.close();118}119}120}121122123