1 /*
   2  * Copyright (c) 2000, 2010, 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 4862382
  26  * @summary Test positional read method of FileChannel with DirectIO
  27  * @key randomness
  28  */
  29 
  30 import java.io.*;
  31 import java.nio.ByteBuffer;
  32 import java.nio.CharBuffer;
  33 import java.nio.channels.*;
  34 import java.nio.channels.FileChannel;
  35 import java.util.Random;
  36 import java.nio.file.Paths;
  37 import java.nio.file.Path;
  38 import java.nio.file.Files;
  39 import java.nio.file.FileStore;
  40 import com.sun.nio.file.ExtendedOpenOption;
  41 
  42 /**
  43  * Testing FileChannel's positional read method.
  44  */
  45 
  46 public class PreadDirect {
  47 
  48     private static PrintStream err = System.err;
  49 
  50     private static Random generator = new Random();
  51 
  52     private static int CHARS_PER_GROUP = 4096;
  53 
  54     public static void main(String[] args) throws Exception {
  55         genericTest1();
  56         genericTest2();
  57         testNegativePosition(); // This test for bug 4862382
  58     }
  59 
  60     // This test for bug 4862382
  61     private static void testNegativePosition() throws Exception {
  62         File blah = File.createTempFile("blah1", null);
  63         blah.deleteOnExit();
  64         FileOutputStream fos = new FileOutputStream(blah);
  65         fos.write(new byte[4096]);
  66         fos.close();
  67 
  68         String path = blah.getAbsolutePath();
  69         Path p = Paths.get(path);
  70         FileChannel fc = FileChannel.open(p, ExtendedOpenOption.DIRECT);
  71 
  72         try {
  73             fc.read(ByteBuffer.allocate(4096), -1L);
  74             throw new RuntimeException("Expected exception not thrown");
  75         } catch(IllegalArgumentException e) {
  76             // Correct result
  77         } finally {
  78             fc.close();
  79             blah.delete();
  80         }
  81     }
  82 
  83     private static void genericTest2() throws Exception {
  84         File blah = File.createTempFile("blah2", null);
  85         blah.deleteOnExit();
  86         FileOutputStream fos = new FileOutputStream(blah);
  87         fos.write(new byte[4096]);
  88         fos.close();
  89 
  90         String path = blah.getAbsolutePath();
  91         Path p = Paths.get(path);
  92         FileChannel fc = FileChannel.open(p, ExtendedOpenOption.DIRECT);
  93         try {
  94             fc.read(ByteBuffer.allocate(4096), 4000);
  95             throw new RuntimeException("Expected exception not thrown");
  96         } catch(IllegalArgumentException e) {
  97             if (!e.getMessage().contains("IO size or position is not aligned"))
  98                 throw new RuntimeException("Read test failed");
  99         } finally {
 100             fc.close();
 101             blah.delete();
 102         }
 103     }
 104 
 105     private static void genericTest1() throws Exception {
 106         StringBuffer sb = new StringBuffer();
 107         sb.setLength(2);
 108 
 109         File blah = File.createTempFile("blah3", null);
 110         blah.deleteOnExit();
 111         initTestFile(blah);
 112 
 113         String path = blah.getAbsolutePath();
 114         Path p = Paths.get(path);
 115         FileChannel c = FileChannel.open(p, ExtendedOpenOption.DIRECT);
 116         FileStore fs = Files.getFileStore(p);
 117         int alignment = 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).alignedSlice(alignment);
 124 
 125             long originalPosition = c.position();
 126 
 127             int totalRead = 0;
 128             while (totalRead < 4096) {
 129                 int read = c.read(block, offset);
 130                 if (read < 0)
 131                     throw new Exception("Read failed");
 132                 totalRead += read;
 133             }
 134 
 135             long newPosition = c.position();
 136 
 137             for (int i=0; i<2; i++) {
 138                 byte aByte = block.get(i);
 139                 sb.setCharAt(i, (char)aByte);
 140             }
 141             int result = Integer.parseInt(sb.toString());
 142             if (result != expectedResult) {
 143                 err.println("I expected "+ expectedResult);
 144                 err.println("I got "+ result);
 145                 throw new Exception("Read test failed");
 146             }
 147 
 148             // Ensure that file pointer position has not changed
 149             if (originalPosition != newPosition)
 150                 throw new Exception("File position modified");
 151         }
 152 
 153         c.close();
 154         blah.delete();
 155     }
 156 
 157     /**
 158      * Creates file blah:
 159      */
 160     private static void initTestFile(File blah) throws Exception {
 161         FileOutputStream fos = new FileOutputStream(blah);
 162         BufferedWriter awriter
 163             = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
 164 
 165         for(int i=0; i<100; i++) {
 166             String number = new Integer(i).toString();
 167             for (int h=0; h<2-number.length(); h++)
 168                 awriter.write("0");
 169             awriter.write(""+i);
 170             for (int j=0; j < 4094; j++)
 171                 awriter.write("0");
 172         }
 173        awriter.flush();
 174        awriter.close();
 175     }
 176 }