Path: blob/master/test/jdk/java/nio/channels/FileChannel/directio/PwriteDirect.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/* @test24* @bug 816490025* @summary Test positional write method of FileChannel with DirectIO26* (use -Dseed=X to set PRNG seed)27* @library .. /test/lib28* @build jdk.test.lib.RandomFactory29* DirectIOTest30* @run main/othervm PwriteDirect31* @key randomness32*/3334import java.io.*;35import java.nio.ByteBuffer;36import java.nio.CharBuffer;37import java.nio.channels.*;38import java.nio.file.Files;39import java.nio.file.FileStore;40import java.nio.file.Path;41import java.nio.file.Paths;42import java.nio.file.StandardOpenOption;43import java.util.Random;44import com.sun.nio.file.ExtendedOpenOption;4546import jdk.test.lib.RandomFactory;4748/**49* Testing FileChannel's positional write method.50*/51public class PwriteDirect {5253private static Random generator = RandomFactory.getRandom();5455private static int charsPerGroup = -1;5657private static int alignment = -1;5859private static boolean initTests() throws Exception {60Path p = DirectIOTest.createTempFile();61try {62FileStore fs = Files.getFileStore(p);63alignment = (int)fs.getBlockSize();64charsPerGroup = alignment;65} finally {66Files.delete(p);67}68return true;69}7071public static void main(String[] args) throws Exception {72if (initTests()) {73genericTest();74TestWithNotAlignedChannelPosition();75testUnwritableChannel();76}77}7879private static void testUnwritableChannel() throws Exception {80Path p = DirectIOTest.createTempFile();8182try (FileChannel fc = FileChannel.open(p,83StandardOpenOption.DELETE_ON_CLOSE, ExtendedOpenOption.DIRECT)) {84try {85fc.write(ByteBuffer.allocate(charsPerGroup), 0);86throw new RuntimeException("Expected exception not thrown");87} catch(NonWritableChannelException e) {88// Correct result89}90}91}9293private static void TestWithNotAlignedChannelPosition() throws Exception {94Path p = DirectIOTest.createTempFile();9596try (FileChannel fc = FileChannel.open(p,97StandardOpenOption.WRITE, StandardOpenOption.DELETE_ON_CLOSE, ExtendedOpenOption.DIRECT)) {98int bufferSize = charsPerGroup;99long position = charsPerGroup - 1;100try {101fc.write(ByteBuffer.allocate(bufferSize), position);102throw new RuntimeException("Expected exception not thrown");103} catch(IOException e) {104if (!e.getMessage().contains("Channel position (" + position + ")"105+ " is not a multiple of the block size (" + alignment + ")"))106throw new RuntimeException("Write test failed");107}108}109}110111private static void genericTest() throws Exception {112Path p = DirectIOTest.createTempFile();113114initTestFile(p);115116try (FileChannel fc = FileChannel.open(p,117StandardOpenOption.READ, StandardOpenOption.WRITE,118StandardOpenOption.DELETE_ON_CLOSE, ExtendedOpenOption.DIRECT)) {119ByteBuffer block =120ByteBuffer.allocateDirect(charsPerGroup + alignment - 1)121.alignedSlice(alignment);122for (int x = 0; x < 100; x++) {123block.clear();124long offset = generator.nextInt(100) * charsPerGroup;125126// Write known sequence out127for (int i = 0; i < charsPerGroup; i++) {128block.put(i, (byte)'a');129}130long originalPosition = fc.position();131132int written = fc.write(block, offset);133if (written < 0)134throw new Exception("Write failed");135136long newPosition = fc.position();137138// Ensure that file pointer position has not changed139if (originalPosition != newPosition)140throw new Exception("File position modified");141142// Attempt to read sequence back in143originalPosition = fc.position();144145block.rewind();146int read = fc.read(block, offset);147if (read != charsPerGroup)148throw new Exception("Read failed");149150newPosition = fc.position();151152// Ensure that file pointer position has not changed153if (originalPosition != newPosition)154throw new Exception("File position modified");155156for (int j = 0; j < charsPerGroup; j++) {157if (block.get(j) != (byte)'a')158throw new Exception("Write test failed");159}160}161}162}163164private static void initTestFile(Path p) throws Exception {165try (OutputStream fos = Files.newOutputStream(p)) {166try (BufferedWriter awriter167= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"))) {168for (int i = 0; i < 100; i++) {169String number = new Integer(i).toString();170for (int h = 0; h < 4 - number.length(); h++)171awriter.write("0");172awriter.write("" + i);173for (int j = 0; j < 4092; j++)174awriter.write("0");175}176awriter.flush();177}178}179}180}181182183