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