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