--- /dev/null 2017-04-27 11:02:11.338611058 -0700 +++ new/test/java/nio/channels/FileChannel/directio/WriteDirect.java 2017-08-09 14:53:28.706811196 -0700 @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2017, 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 + * @bug 8164900 + * @summary Test FileChannel write with DirectIO + */ + +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 { + Path p = Files.createTempFile(Paths.get( + System.getProperty("test.dir", ".")), "test", null); + FileChannel fc = FileChannel.open(p, StandardOpenOption.WRITE, + ExtendedOpenOption.DIRECT); + FileStore fs = Files.getFileStore(p); + int alignment = (int)fs.getBlockSize(); + ByteBuffer src = ByteBuffer.allocate(4000); + try { + fc.write(src); + throw new RuntimeException("Expected exception not thrown"); + } catch (IOException e) { + if (!e.getMessage().contains("Number of remaining bytes is " + + "not a multiple of the block size")) + throw new Exception("Write failure"); + } finally { + fc.close(); + } + + Files.delete(p); + } + + static void test2() throws Exception { + Path p = Files.createTempFile(Paths.get( + System.getProperty("test.dir", ".")), "test", null); + ByteBuffer[] srcs = new ByteBuffer[4]; + FileChannel fc = FileChannel.open(p, StandardOpenOption.WRITE, + ExtendedOpenOption.DIRECT); + FileStore fs = Files.getFileStore(p); + int alignment = (int)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(); + } + + fc = FileChannel.open(p); + 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 + Files.delete(p); + } +}