Path: blob/master/test/jdk/java/nio/channels/FileChannel/directio/PreadDirect.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 read 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 PreadDirect31* @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 read method.50*/5152public class PreadDirect {5354private static PrintStream err = System.err;5556private static Random generator = RandomFactory.getRandom();5758private static int charsPerGroup = -1;5960private static int alignment = -1;6162public static void main(String[] args) throws Exception {63if (initTests()) {64genericTest();65testNotAlignedChannelPosition();66testNegativeChannelPosition();67}68}6970private static boolean initTests() throws Exception {71Path p = DirectIOTest.createTempFile();72try {73FileStore fs = Files.getFileStore(p);74alignment = (int)fs.getBlockSize();75charsPerGroup = alignment;76} finally {77Files.delete(p);78}79return true;80}8182private static void testNegativeChannelPosition() throws Exception {83Path p = DirectIOTest.createTempFile();8485try (OutputStream fos = Files.newOutputStream(p)) {86fos.write(new byte[charsPerGroup]);87}8889try (FileChannel fc = FileChannel.open(p,90StandardOpenOption.DELETE_ON_CLOSE, ExtendedOpenOption.DIRECT)) {91try {92fc.read(ByteBuffer.allocate(charsPerGroup), -1L);93throw new RuntimeException("Expected exception not thrown");94} catch(IllegalArgumentException e) {95// Correct result96}97}98}99100private static void testNotAlignedChannelPosition() throws Exception {101Path p = DirectIOTest.createTempFile();102103try (OutputStream fos = Files.newOutputStream(p)) {104fos.write(new byte[charsPerGroup]);105}106107try (FileChannel fc = FileChannel.open(p,108StandardOpenOption.DELETE_ON_CLOSE, ExtendedOpenOption.DIRECT)) {109long pos = charsPerGroup - 1;110try {111fc.read(ByteBuffer.allocate(charsPerGroup), pos);112throw new RuntimeException("Expected exception not thrown");113} catch(IOException e) {114if (!e.getMessage().contains("Channel position (" + pos115+ ") is not a multiple of the block size (" + alignment + ")"))116throw new RuntimeException("Read test failed");117}118}119}120121private static void genericTest() throws Exception {122StringBuffer sb = new StringBuffer();123sb.setLength(2);124125Path p = DirectIOTest.createTempFile();126127initTestFile(p);128129try (FileChannel fc = FileChannel.open(p,130StandardOpenOption.DELETE_ON_CLOSE, ExtendedOpenOption.DIRECT)) {131ByteBuffer block =132ByteBuffer.allocateDirect(charsPerGroup + alignment - 1)133.alignedSlice(alignment);134for (int x = 0; x < 100; x++) {135block.clear();136long offset = generator.nextInt(100) * charsPerGroup;137long expectedResult = offset / charsPerGroup;138offset = expectedResult * charsPerGroup;139140long originalPosition = fc.position();141142int read = fc.read(block, offset);143if (read != charsPerGroup)144throw new Exception("Read failed");145146long newPosition = fc.position();147148for (int i = 0; i < 2; i++) {149byte aByte = block.get(i);150sb.setCharAt(i, (char)aByte);151}152int result = Integer.parseInt(sb.toString());153if (result != expectedResult) {154err.println("I expected "+ expectedResult);155err.println("I got "+ result);156throw new Exception("Read test failed");157}158159// Ensure that file pointer position has not changed160if (originalPosition != newPosition)161throw new Exception("File position modified");162}163}164}165166private static void initTestFile(Path p) throws Exception {167try (OutputStream fos = Files.newOutputStream(p)) {168try (BufferedWriter awriter169= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"))) {170171for (int i = 0; i < 100; i++) {172String number = new Integer(i).toString();173for (int h = 0; h < 2 - number.length(); h++)174awriter.write("0");175awriter.write(""+i);176for (int j = 0; j < (charsPerGroup - 2); j++)177awriter.write("0");178}179awriter.flush();180}181}182}183}184185186