Path: blob/master/test/jdk/java/nio/channels/FileChannel/directio/WriteDirect.java
41161 views
/*1* Copyright (c) 2017, 2021, 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 816490026* @summary Test FileChannel write with DirectIO27* @library .. /test/lib28* @build DirectIOTest29* @run main/othervm WriteDirect30*/3132import java.io.*;33import java.nio.*;34import java.nio.channels.*;35import java.nio.file.Files;36import java.nio.file.FileStore;37import java.nio.file.Path;38import java.nio.file.Paths;39import java.nio.file.StandardOpenOption;40import com.sun.nio.file.ExtendedOpenOption;4142public class WriteDirect {4344private static int charsPerGroup = -1;4546private static int alignment = -1;4748private static boolean initTests() throws Exception {49Path p = DirectIOTest.createTempFile();50try {51FileStore fs = Files.getFileStore(p);52alignment = (int)fs.getBlockSize();53charsPerGroup = alignment;54} finally {55Files.delete(p);56}57return true;58}5960public static void main(String[] args) throws Exception {61if (initTests()) {62testWithNotAlignedBuffer();63testWithNotAlignedBufferOffset();64testWithArrayOfBuffer();65}66}6768static void testWithNotAlignedBuffer() throws Exception {69Path p = DirectIOTest.createTempFile();70try (FileChannel fc = FileChannel.open(p,71StandardOpenOption.WRITE, StandardOpenOption.DELETE_ON_CLOSE,72ExtendedOpenOption.DIRECT)) {73int bufferSize = charsPerGroup - 1;74ByteBuffer src = ByteBuffer.allocate(bufferSize);75try {76fc.write(src);77throw new RuntimeException("Expected exception not thrown");78} catch (IOException e) {79if (!e.getMessage().contains("Number of remaining bytes ("80+ bufferSize + ") is not a multiple of the block size ("81+ alignment + ")"))82throw new Exception("Write failure");83}84}85}8687private static void testWithNotAlignedBufferOffset() throws Exception {88int bufferSize = charsPerGroup * 2;89int pos = alignment - 1;9091Path p = DirectIOTest.createTempFile();9293try (FileChannel fc = FileChannel.open(p,94StandardOpenOption.WRITE, StandardOpenOption.DELETE_ON_CLOSE,95ExtendedOpenOption.DIRECT)) {96ByteBuffer block = ByteBuffer.allocateDirect(bufferSize);97block.position(pos);98block.limit(bufferSize - 1);99try {100fc.write(block);101throw new RuntimeException("Expected exception not thrown");102} catch (IOException e) {103if (!e.getMessage().contains("Current location of the bytebuffer "104+ "(" + pos + ") is not a multiple of the block size ("105+ alignment + ")"))106throw new Exception("Write test failed");107}108}109}110111static void testWithArrayOfBuffer() throws Exception {112Path p = DirectIOTest.createTempFile();113ByteBuffer[] srcs = new ByteBuffer[4];114try (FileChannel fc = FileChannel.open(p,115StandardOpenOption.WRITE, ExtendedOpenOption.DIRECT)) {116for (int i = 0; i < 4; i++) {117srcs[i] = ByteBuffer.allocateDirect(charsPerGroup + alignment - 1)118.alignedSlice(alignment);119for (int j = 0; j < charsPerGroup; j++) {120srcs[i].put((byte)i);121}122srcs[i].flip();123}124125fc.write(srcs, 1, 2);126}127128try (FileChannel fc = FileChannel.open(p,129StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE)) {130ByteBuffer bb = ByteBuffer.allocateDirect(charsPerGroup * 2);131fc.read(bb);132bb.flip();133for (int k = 0; k < charsPerGroup; k++) {134if (bb.get() != 1)135throw new RuntimeException("Write failure");136}137for (int m = 0; m < charsPerGroup; m++) {138if (bb.get() != 2)139throw new RuntimeException("Write failure");140}141try {142bb.get();143throw new RuntimeException("Write failure");144} catch (BufferUnderflowException bufe) {145// correct result146}147}148}149}150151152