Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/FileChannel/Write.java
38828 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/*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 46981384041// This test is not suitable for automated testing at this time.42// I am commenting it out but it will be easy to manually43// test for a regression in this area. See also 4796221.44//test4(); // for bug 463836545}4647// Test to see that offset > length does not throw exception48static void test1() throws Exception {49ByteBuffer[] dsts = new ByteBuffer[4];50for (int i=0; i<4; i++)51dsts[i] = ByteBuffer.allocateDirect(10);5253File testFile = File.createTempFile("test1", null);54try {55FileOutputStream fos = new FileOutputStream(testFile);56FileChannel fc = fos.getChannel();57fc.write(dsts, 2, 1);58fos.close();59} finally {60testFile.delete();61}62}6364// Test to see that the appropriate buffers are updated65static void test2() throws Exception {66File testFile = File.createTempFile("test2", null);67testFile.delete();68ByteBuffer[] srcs = new ByteBuffer[4];69for (int i=0; i<4; i++)70srcs[i] = ByteBuffer.allocateDirect(10);7172srcs[0].put((byte)1); srcs[0].flip();73srcs[1].put((byte)2); srcs[1].flip();74srcs[2].put((byte)3); srcs[2].flip();75srcs[3].put((byte)4); srcs[3].flip();7677FileOutputStream fos = new FileOutputStream(testFile);78FileChannel fc = fos.getChannel();79try {80fc.write(srcs, 1, 2);81} finally {82fc.close();83}8485FileInputStream fis = new FileInputStream(testFile);86fc = fis.getChannel();87try {88ByteBuffer bb = ByteBuffer.allocateDirect(10);89fc.read(bb);90bb.flip();91if (bb.get() != 2)92throw new RuntimeException("Write failure");93if (bb.get() != 3)94throw new RuntimeException("Write failure");95try {96bb.get();97throw new RuntimeException("Write failure");98} catch (BufferUnderflowException bufe) {99// correct result100}101} finally {102fc.close();103}104105// eagerly clean-up106testFile.delete();107}108109// Test write to a negative position (bug 4698138).110static void test3() throws Exception {111File testFile = File.createTempFile("test1", null);112testFile.deleteOnExit();113ByteBuffer dst = ByteBuffer.allocate(10);114FileOutputStream fos = new FileOutputStream(testFile);115FileChannel fc = fos.getChannel();116try {117fc.write(dst, -1);118throw new RuntimeException("Expected IAE not thrown");119} catch (IllegalArgumentException iae) {120// Correct result121} finally {122fos.close();123}124}125126private static final int TEST4_NUM_BUFFERS = 3;127128private static final int TEST4_BUF_CAP = Integer.MAX_VALUE / 2;129130/**131* Test to see that vector write can return > Integer.MAX_VALUE132*133* Note that under certain circumstances disk space problems occur134* with this test. It typically relies upon adequate disk space and/or135* a Solaris disk space optimization where empty files take up less136* space than their logical size.137*138* Note that if this test fails it is not necessarily a violation of139* spec: the value returned by fc.write can be smaller than the number140* of bytes requested to write. It is testing an optimization that allows141* for larger return values.142*/143static void test4() throws Exception {144// Only works on 64 bit Solaris145String osName = System.getProperty("os.name");146if (!osName.startsWith("SunOS"))147return;148String dataModel = System.getProperty("sun.arch.data.model");149if (!dataModel.startsWith("64"))150return;151152File testFile = File.createTempFile("test4", null);153testFile.deleteOnExit();154155FileChannel[] fcs = new FileChannel[TEST4_NUM_BUFFERS];156157ByteBuffer[] dsts = new ByteBuffer[TEST4_NUM_BUFFERS];158// Map these buffers from a file so we don't run out of memory159for (int i=0; i<TEST4_NUM_BUFFERS; i++) {160File f = File.createTempFile("test4." + i, null);161f.deleteOnExit();162prepTest4File(f);163FileInputStream fis = new FileInputStream(f);164FileChannel fc = fis.getChannel();165MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0,166TEST4_BUF_CAP);167dsts[i] = mbb;168}169170FileOutputStream fos = new FileOutputStream(testFile);171FileChannel fc = fos.getChannel();172try {173long bytesWritten = fc.write(dsts);174if (bytesWritten < Integer.MAX_VALUE) {175// Note: this is not a violation of the spec176throw new RuntimeException("Test 4 failed but wrote " +177bytesWritten);178}179} finally {180fc.close();181fos.close();182}183}184185static void prepTest4File(File blah) throws Exception {186RandomAccessFile raf = new RandomAccessFile(blah, "rw");187FileChannel fc = raf.getChannel();188fc.write(ByteBuffer.wrap("Use the source!".getBytes()),189TEST4_BUF_CAP);190fc.close();191raf.close();192}193194}195196197