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 4862411 26 * @summary Test positional write 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 java.nio.file.StandardOpenOption; 41 import com.sun.nio.file.ExtendedOpenOption; 42 43 /** 44 * Testing FileChannel's positional write method. 45 */ 46 public class PwriteDirect { 47 48 private static Random generator = new Random(); 49 50 private static File blah; 51 52 public static void main(String[] args) throws Exception { 53 genericTest(); 54 testUnwritableChannel(); 55 } 56 57 // This test for bug 4862411 58 private static void testUnwritableChannel() throws Exception { 59 File blah = File.createTempFile("blah2", null); 60 blah.deleteOnExit(); 61 FileOutputStream fos = new FileOutputStream(blah); 62 fos.write(new byte[4096]); 63 fos.close(); 64 65 String path = blah.getAbsolutePath(); 66 Path p = Paths.get(path); 67 FileChannel fc = FileChannel.open(p, ExtendedOpenOption.DIRECT); 68 69 try { 70 fc.write(ByteBuffer.allocate(4096),0); 71 throw new RuntimeException("Expected exception not thrown"); 72 } catch(NonWritableChannelException e) { 73 // Correct result 74 } finally { 75 fc.close(); 76 blah.delete(); 77 } 78 } 79 80 private static void genericTest() throws Exception { 81 blah = File.createTempFile("blah", null); 82 blah.deleteOnExit(); 83 initTestFile(blah); 84 85 String path = blah.getAbsolutePath(); 86 Path p = Paths.get(path); 87 FileChannel c = FileChannel.open(p, StandardOpenOption.WRITE, StandardOpenOption.READ, ExtendedOpenOption.DIRECT); 88 FileStore fs = Files.getFileStore(p); 89 int alignment = fs.getBlockSize(); 90 91 for (int x=0; x<100; x++) { 92 long offset = generator.nextInt(100) * 4096; 93 ByteBuffer block = ByteBuffer.allocateDirect(4096 + alignment - 1).alignedSlice(alignment); 94 95 // Write known sequence out 96 for (byte i=0; i<4; i++) { 97 block.put(i); 98 } 99 block.position(0); 100 long originalPosition = c.position(); 101 int totalWritten = 0; 102 while (totalWritten < 4096) { 103 int written = c.write(block, offset); 104 if (written < 0) 105 throw new Exception("Write failed"); 106 totalWritten += written; 107 } 108 109 long newPosition = c.position(); 110 111 // Ensure that file pointer position has not changed 112 if (originalPosition != newPosition) 113 throw new Exception("File position modified"); 114 115 // Attempt to read sequence back in 116 block = ByteBuffer.allocateDirect(4096 + alignment - 1).alignedSlice(alignment); 117 originalPosition = c.position(); 118 int totalRead = 0; 119 while (totalRead < 4096) { 120 int read = c.read(block, offset); 121 if (read < 0) 122 throw new Exception("Read failed"); 123 totalRead += read; 124 } 125 newPosition = c.position(); 126 127 // Ensure that file pointer position has not changed 128 if (originalPosition != newPosition) 129 throw new Exception("File position modified"); 130 131 for (byte i=0; i<4; i++) { 132 if (block.get(i) != i) 133 throw new Exception("Write test failed"); 134 } 135 } 136 c.close(); 137 blah.delete(); 138 } 139 140 /** 141 * Creates file blah: 142 */ 143 private static void initTestFile(File blah) throws Exception { 144 FileOutputStream fos = new FileOutputStream(blah); 145 BufferedWriter awriter 146 = new BufferedWriter(new OutputStreamWriter(fos, "8859_1")); 147 148 for(int i=0; i<100; i++) { 149 String number = new Integer(i).toString(); 150 for (int h=0; h<4-number.length(); h++) 151 awriter.write("0"); 152 awriter.write(""+i); 153 for (int j=0; j < 4092; j++) 154 awriter.write("0"); 155 } 156 awriter.flush(); 157 awriter.close(); 158 } 159 }