1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 8164900
  26  * @summary Test positional read method of FileChannel with DirectIO
  27  * (use -Dseed=X to set PRNG seed)
  28  * @library .. /test/lib
  29  * @build jdk.test.lib.RandomFactory
  30  * @run main PreadDirect
  31  * @key randomness
  32  */
  33 
  34 import java.io.*;
  35 import java.nio.ByteBuffer;
  36 import java.nio.CharBuffer;
  37 import java.nio.channels.*;
  38 import java.nio.channels.FileChannel;
  39 import java.util.Random;
  40 import java.nio.file.Paths;
  41 import java.nio.file.Path;
  42 import java.nio.file.Files;
  43 import java.nio.file.FileStore;
  44 import com.sun.nio.file.ExtendedOpenOption;
  45 
  46 import jdk.test.lib.RandomFactory;
  47 
  48 /**
  49  * Testing FileChannel's positional read method.
  50  */
  51 
  52 public class PreadDirect {
  53 
  54     private static PrintStream err = System.err;
  55 
  56     private static Random generator = RandomFactory.getRandom();
  57 
  58     private static int CHARS_PER_GROUP = 4096;
  59 
  60     public static void main(String[] args) throws Exception {
  61         genericTest1();
  62         genericTest2();
  63         testNegativePosition();
  64     }
  65 
  66     private static void testNegativePosition() throws Exception {
  67         Path p = Files.createTempFile(Paths.get(
  68                 System.getProperty("test.dir", ".")), "test", null);
  69         OutputStream fos = Files.newOutputStream(p);
  70         fos.write(new byte[4096]);
  71         fos.close();
  72 
  73         FileChannel fc = FileChannel.open(p, ExtendedOpenOption.DIRECT);
  74 
  75         try {
  76             fc.read(ByteBuffer.allocate(4096), -1L);
  77             throw new RuntimeException("Expected exception not thrown");
  78         } catch(IllegalArgumentException e) {
  79             // Correct result
  80         } finally {
  81             fc.close();
  82             Files.delete(p);
  83         }
  84     }
  85 
  86     private static void genericTest2() throws Exception {
  87         Path p = Files.createTempFile(Paths.get(
  88                 System.getProperty("test.dir", ".")), "test", null);
  89         OutputStream fos = Files.newOutputStream(p);
  90         fos.write(new byte[4096]);
  91         fos.close();
  92 
  93         FileChannel fc = FileChannel.open(p, ExtendedOpenOption.DIRECT);
  94         try {
  95             fc.read(ByteBuffer.allocate(4096), 4000);
  96             throw new RuntimeException("Expected exception not thrown");
  97         } catch(IOException e) {
  98             if (!e.getMessage().contains("Channel position is not "
  99                                          + "a multiple of the block size"))
 100                 throw new RuntimeException("Read test failed");
 101         } finally {
 102             fc.close();
 103             Files.delete(p);
 104         }
 105     }
 106 
 107     private static void genericTest1() throws Exception {
 108         StringBuffer sb = new StringBuffer();
 109         sb.setLength(2);
 110 
 111         Path p = Files.createTempFile(Paths.get(
 112                 System.getProperty("test.dir", ".")), "test", null);
 113         initTestFile(p);
 114 
 115         FileChannel c = FileChannel.open(p, ExtendedOpenOption.DIRECT);
 116         FileStore fs = Files.getFileStore(p);
 117         int alignment = (int)fs.getBlockSize();
 118 
 119         for (int x=0; x<100; x++) {
 120             long offset = generator.nextInt(100) * 4096;
 121             long expectedResult = offset / CHARS_PER_GROUP;
 122             offset = expectedResult * CHARS_PER_GROUP;
 123             ByteBuffer block = ByteBuffer.allocateDirect(4096 + alignment - 1)
 124                                          .alignedSlice(alignment);
 125 
 126             long originalPosition = c.position();
 127 
 128             int totalRead = 0;
 129             while (totalRead < 4096) {
 130                 int read = c.read(block, offset);
 131                 if (read < 0)
 132                     throw new Exception("Read failed");
 133                 totalRead += read;
 134             }
 135 
 136             long newPosition = c.position();
 137 
 138             for (int i=0; i<2; i++) {
 139                 byte aByte = block.get(i);
 140                 sb.setCharAt(i, (char)aByte);
 141             }
 142             int result = Integer.parseInt(sb.toString());
 143             if (result != expectedResult) {
 144                 err.println("I expected "+ expectedResult);
 145                 err.println("I got "+ result);
 146                 throw new Exception("Read test failed");
 147             }
 148 
 149             // Ensure that file pointer position has not changed
 150             if (originalPosition != newPosition)
 151                 throw new Exception("File position modified");
 152         }
 153 
 154         c.close();
 155         Files.delete(p);
 156     }
 157 
 158     private static void initTestFile(Path p) throws Exception {
 159         OutputStream fos = Files.newOutputStream(p);
 160         BufferedWriter awriter
 161             = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
 162 
 163         for(int i=0; i<100; i++) {
 164             String number = new Integer(i).toString();
 165             for (int h=0; h<2-number.length(); h++)
 166                 awriter.write("0");
 167             awriter.write(""+i);
 168             for (int j=0; j < 4094; j++)
 169                 awriter.write("0");
 170         }
 171        awriter.flush();
 172        awriter.close();
 173     }
 174 }