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 * @summary Test read method of FileChannel with DirectIO 26 */ 27 28 import java.io.*; 29 import java.nio.ByteBuffer; 30 import java.nio.CharBuffer; 31 import java.nio.channels.*; 32 import java.nio.channels.FileChannel; 33 import java.util.Random; 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 /** 41 * Testing FileChannel's mapping capabilities. 42 */ 43 44 public class ReadDirect { 45 46 private static PrintStream err = System.err; 47 48 private static Random generator = new Random(); 49 50 private static File blah; 51 52 private static void testRead1() throws Exception { 53 StringBuffer sb = new StringBuffer(); 54 sb.setLength(2); 55 56 int charsPerGroup = 4096; 57 blah = File.createTempFile("blah1", null); 58 blah.deleteOnExit(); 59 initTestFile(blah, charsPerGroup); 60 String path = blah.getAbsolutePath(); 61 Path p = Paths.get(path); 62 FileChannel c = FileChannel.open(p, ExtendedOpenOption.DIRECT); 63 FileStore fs = Files.getFileStore(p); 64 int alignment = fs.getBlockSize(); 65 66 for (int x=0; x<100; x++) { 67 long offset = x * charsPerGroup; 68 long expectedResult = offset / charsPerGroup; 69 ByteBuffer block = ByteBuffer.allocateDirect(charsPerGroup + alignment - 1).alignedSlice(alignment); 70 c.read(block); 71 72 for (int i=0; i<2; i++) { 73 byte aByte = block.get(i); 74 sb.setCharAt(i, (char)aByte); 75 } 76 int result = Integer.parseInt(sb.toString()); 77 if (result != expectedResult) { 78 err.println("I expected "+expectedResult); 79 err.println("I got "+ result); 80 throw new Exception("Read test failed"); 81 } 82 } 83 c.close(); 84 blah.delete(); 85 } 86 87 private static void testRead2() throws Exception { 88 int charsPerGroup = 4000; 89 blah = File.createTempFile("blah2", null); 90 blah.deleteOnExit(); 91 92 FileOutputStream fos = new FileOutputStream(blah); 93 fos.write(new byte[charsPerGroup]); 94 fos.close(); 95 96 String path = blah.getAbsolutePath(); 97 Path p = Paths.get(path); 98 FileChannel c = FileChannel.open(p, ExtendedOpenOption.DIRECT); 99 FileStore fs = Files.getFileStore(p); 100 int alignment = fs.getBlockSize(); 101 102 ByteBuffer block = ByteBuffer.allocate(charsPerGroup); 103 try { 104 c.read(block); 105 throw new RuntimeException("Expected exception not thrown"); 106 } catch (IllegalArgumentException e) { 107 if (!e.getMessage().contains("IO size or position is not aligned")) 108 throw new Exception("Read test failed"); 109 } finally { 110 c.close(); 111 blah.delete(); 112 } 113 } 114 115 116 public static void main(String[] args) throws Exception { 117 testRead1(); 118 testRead2(); 119 } 120 121 /** 122 * Creates file blah: 123 */ 124 private static void initTestFile(File blah, int charsPerGroup) throws Exception { 125 FileOutputStream fos = new FileOutputStream(blah); 126 BufferedWriter awriter 127 = new BufferedWriter(new OutputStreamWriter(fos, "8859_1")); 128 129 for(int i=0; i<100; i++) { 130 String number = new Integer(i).toString(); 131 for (int h=0; h<2-number.length(); h++) 132 awriter.write("0"); 133 awriter.write(""+i); 134 for (int j=0; j < (charsPerGroup - 2); j++) 135 awriter.write("0"); 136 } 137 awriter.flush(); 138 awriter.close(); 139 } 140 }