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 read method of FileChannel with DirectIO
  27  */
  28 
  29 import java.io.*;
  30 import java.nio.ByteBuffer;
  31 import java.nio.CharBuffer;
  32 import java.nio.channels.*;
  33 import java.nio.channels.FileChannel;
  34 import java.nio.file.Paths;
  35 import java.nio.file.Path;
  36 import java.nio.file.Files;
  37 import java.nio.file.FileStore;
  38 import com.sun.nio.file.ExtendedOpenOption;
  39 
  40 public class ReadDirect {
  41 
  42     private static PrintStream err = System.err;
  43 
  44     private static void testRead1() throws Exception {
  45         StringBuffer sb = new StringBuffer();
  46         sb.setLength(2);
  47 
  48         int charsPerGroup = 4096;
  49         Path p = Files.createTempFile(Paths.get(
  50                 System.getProperty("test.dir", ".")), "test", null);
  51         initTestFile(p, charsPerGroup);
  52         FileChannel c = FileChannel.open(p, ExtendedOpenOption.DIRECT);
  53         FileStore fs = Files.getFileStore(p);
  54         int alignment = (int)fs.getBlockSize();
  55 
  56         for (int x=0; x<100; x++) {
  57             long offset = x * charsPerGroup;
  58             long expectedResult = offset / charsPerGroup;
  59             ByteBuffer block = ByteBuffer.allocateDirect(charsPerGroup
  60                     + alignment - 1).alignedSlice(alignment);
  61             c.read(block);
  62 
  63             for (int i=0; i<2; i++) {
  64                 byte aByte = block.get(i);
  65                 sb.setCharAt(i, (char)aByte);
  66             }
  67             int result = Integer.parseInt(sb.toString());
  68             if (result != expectedResult) {
  69                 err.println("I expected "+expectedResult);
  70                 err.println("I got "+ result);
  71                 throw new Exception("Read test failed");
  72             }
  73         }
  74         c.close();
  75         Files.delete(p);
  76     }
  77 
  78     private static void testRead2() throws Exception {
  79         int charsPerGroup = 4000;
  80         Path p = Files.createTempFile(Paths.get(
  81                 System.getProperty("test.dir", ".")), "test", null);
  82         OutputStream fos = Files.newOutputStream(p);
  83         fos.write(new byte[charsPerGroup]);
  84         fos.close();
  85 
  86         FileChannel c = FileChannel.open(p, ExtendedOpenOption.DIRECT);
  87         FileStore fs = Files.getFileStore(p);
  88         int alignment = (int)fs.getBlockSize();
  89 
  90         ByteBuffer block = ByteBuffer.allocate(charsPerGroup);
  91         try {
  92             c.read(block);
  93             throw new RuntimeException("Expected exception not thrown");
  94         } catch (IOException e) {
  95             if (!e.getMessage().contains("Number of remaining bytes is "
  96                                          + "not a multiple of the block size"))
  97                 throw new Exception("Read test failed");
  98         } finally {
  99             c.close();
 100             Files.delete(p);
 101         }
 102     }
 103 
 104 
 105     public static void main(String[] args) throws Exception {
 106         testRead1();
 107         testRead2();
 108     }
 109 
 110     private static void initTestFile(Path p, int charsPerGroup)
 111             throws Exception {
 112         OutputStream fos = Files.newOutputStream(p);
 113         BufferedWriter awriter
 114             = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
 115 
 116         for(int i=0; i<100; i++) {
 117             String number = new Integer(i).toString();
 118             for (int h=0; h<2-number.length(); h++)
 119                 awriter.write("0");
 120             awriter.write(""+i);
 121             for (int j=0; j < (charsPerGroup - 2); j++)
 122                 awriter.write("0");
 123         }
 124        awriter.flush();
 125        awriter.close();
 126     }
 127 }