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 import java.io.*; 25 import java.nio.ByteBuffer; 26 import java.nio.CharBuffer; 27 import java.nio.channels.*; 28 import java.nio.channels.FileChannel; 29 import java.nio.file.Paths; 30 import java.nio.file.Path; 31 import java.nio.file.Files; 32 import java.nio.file.FileStore; 33 import java.nio.file.StandardOpenOption; 34 import com.sun.nio.file.ExtendedOpenOption; 35 36 public class DirectIO { 37 38 private static final int SIZE = 4096; 39 40 static { 41 System.loadLibrary("DirectIO"); 42 } 43 44 private static void testWrite(Path p) throws Exception { 45 FileChannel fc = FileChannel.open(p, StandardOpenOption.WRITE, 46 ExtendedOpenOption.DIRECT); 47 FileStore fs = Files.getFileStore(p); 48 int alignment = (int)fs.getBlockSize(); 49 ByteBuffer src = ByteBuffer.allocateDirect(SIZE + alignment -1) 50 .alignedSlice(alignment); 51 for (int j = 0; j < SIZE; j++) { 52 src.put((byte)0); 53 } 54 src.flip(); 55 try { 56 fc.write(src); 57 } finally { 58 fc.close(); 59 } 60 } 61 62 private static void testRead(Path p) throws Exception { 63 FileChannel fc = FileChannel.open(p, ExtendedOpenOption.DIRECT); 64 FileStore fs = Files.getFileStore(p); 65 int alignment = (int)fs.getBlockSize(); 66 67 ByteBuffer dest = ByteBuffer.allocateDirect(SIZE + alignment - 1) 68 .alignedSlice(alignment); 69 fc.read(dest); 70 71 fc.close(); 72 } 73 74 private static boolean isFileInCache(Path p) { 75 String path = p.toString(); 76 return isFileInCache0(SIZE, path); 77 } 78 79 private static native boolean isFileInCache0(int size, String path); 80 81 public static void main(String[] args) throws Exception { 82 Path p = Files.createTempFile(Paths.get( 83 System.getProperty("test.dir", ".")), "test", null); 84 testWrite(p); 85 if (isFileInCache(p)) { 86 throw new RuntimeException("DirectIO is not working properly with " 87 + "write. File still exists in cache!"); 88 } 89 testRead(p); 90 if (isFileInCache(p)) { 91 throw new RuntimeException("DirectIO is not working properly with " 92 + "read. File still exists in cache!"); 93 } 94 Files.delete(p); 95 } 96 }