--- /dev/null 2016-11-14 12:26:28.251338568 -0800 +++ new/test/java/nio/channels/FileChannel/WriteDirect.java 2016-11-16 10:28:22.127497937 -0800 @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary Test FileChannel write with DirectIO + * @run main/othervm Write + */ + +import java.nio.channels.*; +import java.nio.*; +import java.io.*; +import java.nio.file.Paths; +import java.nio.file.Path; +import java.nio.file.Files; +import java.nio.file.FileStore; +import java.nio.file.StandardOpenOption; +import com.sun.nio.file.ExtendedOpenOption; + +public class WriteDirect { + + public static void main(String[] args) throws Exception { + test1(); + test2(); + } + + static void test1() throws Exception { + File testFile = File.createTempFile("test1", null); + testFile.deleteOnExit(); + + String path = testFile.getAbsolutePath(); + Path p = Paths.get(path); + FileChannel fc = FileChannel.open(p, StandardOpenOption.WRITE, ExtendedOpenOption.DIRECT); + FileStore fs = Files.getFileStore(p); + int alignment = fs.getBlockSize(); + ByteBuffer src = ByteBuffer.allocate(4000); + try { + fc.write(src); + throw new RuntimeException("Expected exception not thrown"); + } catch (IllegalArgumentException e) { + if (!e.getMessage().contains("IO size or position is not aligned")) + throw new Exception("Write failure"); + } finally { + fc.close(); + } + + testFile.delete(); + } + + // Test to see that the appropriate buffers are updated + static void test2() throws Exception { + File testFile = File.createTempFile("test2", null); + testFile.deleteOnExit(); + ByteBuffer[] srcs = new ByteBuffer[4]; + String path = testFile.getAbsolutePath(); + Path p = Paths.get(path); + FileChannel fc = FileChannel.open(p, StandardOpenOption.WRITE, ExtendedOpenOption.DIRECT); + FileStore fs = Files.getFileStore(p); + int alignment = fs.getBlockSize(); + + for (int i=0; i<4; i++) { + srcs[i] = ByteBuffer.allocateDirect(4096 + alignment -1).alignedSlice(alignment); + for (int j=0; j<4096; j++) { + srcs[i].put((byte)i); + } + srcs[i].flip(); + } + + try { + fc.write(srcs, 1, 2); + } finally { + fc.close(); + } + + FileInputStream fis = new FileInputStream(testFile); + fc = fis.getChannel(); + try { + ByteBuffer bb = ByteBuffer.allocateDirect(8192); + fc.read(bb); + bb.flip(); + for (int k=0; k<4096; k++) { + if (bb.get() != 1) + throw new RuntimeException("Write failure"); + } + for (int m=0; m<4096; m++) { + if (bb.get() != 2) + throw new RuntimeException("Write failure"); + } + try { + bb.get(); + throw new RuntimeException("Write failure"); + } catch (BufferUnderflowException bufe) { + // correct result + } + } finally { + fc.close(); + } + + // eagerly clean-up + testFile.delete(); + } +}