blob: acc2766fc5ce117e2f6aa0cba7c0f30da5272ad4 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2001 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 4862382 4862408
26 * @summary Test positional read method of FileChannel
27 */
28
29import java.io.*;
30import java.nio.ByteBuffer;
31import java.nio.CharBuffer;
32import java.nio.channels.*;
33import java.nio.channels.FileChannel;
34import java.util.Random;
35
36
37/**
38 * Testing FileChannel's positional read method.
39 */
40
41public 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 }
69 }
70
71 // This test for bug 4862408
72 private static void testUnreadableChannel() throws Exception {
73 File blah = File.createTempFile("blah2", null);
74 blah.deleteOnExit();
75 FileOutputStream fos = new FileOutputStream(blah);
76 fos.write(new byte[128]);
77 FileChannel fc = fos.getChannel();
78 try {
79 fc.read(ByteBuffer.allocate(256),1);
80 throw new RuntimeException("Expected exception not thrown");
81 } catch(NonReadableChannelException e) {
82 // Correct result
83 }
84 }
85
86 private static void genericTest() throws Exception {
87 StringBuffer sb = new StringBuffer();
88 sb.setLength(4);
89
90 File blah = File.createTempFile("blah3", null);
91 blah.deleteOnExit();
92 initTestFile(blah);
93
94 FileInputStream fis = new FileInputStream(blah);
95 FileChannel c = fis.getChannel();
96
97 for (int x=0; x<100; x++) {
98 long offset = generator.nextInt(1000);
99 long expectedResult = offset / CHARS_PER_LINE;
100 offset = expectedResult * CHARS_PER_LINE;
101 ByteBuffer bleck = ByteBuffer.allocateDirect(4);
102
103 long originalPosition = c.position();
104
105 int totalRead = 0;
106 while (totalRead < 4) {
107 int read = c.read(bleck, offset);
108 if (read < 0)
109 throw new Exception("Read failed");
110 totalRead += read;
111 }
112
113 long newPosition = c.position();
114
115 for (int i=0; i<4; i++) {
116 byte aByte = bleck.get(i);
117 sb.setCharAt(i, (char)aByte);
118 }
119 int result = Integer.parseInt(sb.toString());
120 if (result != expectedResult) {
121 err.println("I expected "+ expectedResult);
122 err.println("I got "+ result);
123 throw new Exception("Read test failed");
124 }
125
126 // Ensure that file pointer position has not changed
127 if (originalPosition != newPosition)
128 throw new Exception("File position modified");
129 }
130
131 c.close();
132 fis.close();
133 }
134
135 /**
136 * Creates file blah:
137 * 0000
138 * 0001
139 * 0002
140 * 0003
141 * .
142 * .
143 * .
144 * 3999
145 *
146 * Blah extends beyond a single page of memory so that the
147 * ability to index into a file of multiple pages is tested.
148 */
149 private static void initTestFile(File blah) throws Exception {
150 FileOutputStream fos = new FileOutputStream(blah);
151 BufferedWriter awriter
152 = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
153
154 for(int i=0; i<4000; i++) {
155 String number = new Integer(i).toString();
156 for (int h=0; h<4-number.length(); h++)
157 awriter.write("0");
158 awriter.write(""+i);
159 awriter.newLine();
160 }
161 awriter.flush();
162 awriter.close();
163 }
164}