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 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 37 38 /** 39 * Testing FileChannel's positional write method. 40 */ 41 public class Pwrite { 42 43 private static Random generator = new Random(); 44 45 private static File blah; 46 47 public static void main(String[] args) throws Exception { 48 genericTest(); 49 testUnwritableChannel(); 50 } 51 52 // This test for bug 4862411 53 private static void testUnwritableChannel() throws Exception { 54 File blah = File.createTempFile("blah2", null); 55 blah.deleteOnExit(); 56 FileOutputStream fos = new FileOutputStream(blah); 57 fos.write(new byte[128]); 58 fos.close(); 59 FileInputStream fis = new FileInputStream(blah); 60 FileChannel fc = fis.getChannel(); 61 try { 62 fc.write(ByteBuffer.allocate(256),1); 63 throw new RuntimeException("Expected exception not thrown"); 64 } catch(NonWritableChannelException e) { 65 // Correct result 66 } finally { 67 fc.close(); 68 blah.delete(); 69 } 70 } 71 72 private static void genericTest() throws Exception { 73 StringBuffer sb = new StringBuffer(); 74 sb.setLength(4); 75 76 blah = File.createTempFile("blah", null); 77 blah.deleteOnExit(); 78 initTestFile(blah); 79 80 RandomAccessFile raf = new RandomAccessFile(blah, "rw"); 81 FileChannel c = raf.getChannel(); 82 83 for (int x=0; x<100; x++) { 84 long offset = generator.nextInt(1000); 85 ByteBuffer bleck = ByteBuffer.allocateDirect(4); 86 87 // Write known sequence out 88 for (byte i=0; i<4; i++) { 89 bleck.put(i); 90 } 91 bleck.flip(); 92 long originalPosition = c.position(); 93 int totalWritten = 0; 94 while (totalWritten < 4) { 95 int written = c.write(bleck, offset); 96 if (written < 0) 97 throw new Exception("Read failed"); 98 totalWritten += written; 99 } 100 101 long newPosition = c.position(); 102 103 // Ensure that file pointer position has not changed 104 if (originalPosition != newPosition) 105 throw new Exception("File position modified"); 106 107 // Attempt to read sequence back in 108 bleck = ByteBuffer.allocateDirect(4); 109 originalPosition = c.position(); 110 int totalRead = 0; 111 while (totalRead < 4) { 112 int read = c.read(bleck, offset); 113 if (read < 0) 114 throw new Exception("Read failed"); 115 totalRead += read; 116 } 117 newPosition = c.position(); 118 119 // Ensure that file pointer position has not changed 120 if (originalPosition != newPosition) 121 throw new Exception("File position modified"); 122 123 for (byte i=0; i<4; i++) { 124 if (bleck.get(i) != i) 125 throw new Exception("Write test failed"); 126 } 127 } 128 c.close(); 129 raf.close(); 130 blah.delete(); 131 } 132 133 /** 134 * Creates file blah: 135 * 0000 136 * 0001 137 * 0002 138 * 0003 139 * . 140 * . 141 * . 142 * 3999 143 * 144 * Blah extends beyond a single page of memory so that the 145 * ability to index into a file of multiple pages is tested. 146 */ 147 private static void initTestFile(File blah) throws Exception { 148 FileOutputStream fos = new FileOutputStream(blah); 149 BufferedWriter awriter 150 = new BufferedWriter(new OutputStreamWriter(fos, "8859_1")); 151 152 for(int i=0; i<4000; i++) { 153 String number = new Integer(i).toString(); 154 for (int h=0; h<4-number.length(); h++) 155 awriter.write("0"); 156 awriter.write(""+i); 157 awriter.newLine(); 158 } 159 awriter.flush(); 160 awriter.close(); 161 } 162 }