blob: 6637863fa289b3c8580e947e648957070658f582 [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 * @summary Test read method of FileChannel
26 */
27
28import java.io.*;
29import java.nio.ByteBuffer;
30import java.nio.CharBuffer;
31import java.nio.channels.*;
32import java.nio.channels.FileChannel;
33import java.util.Random;
34
35
36/**
37 * Testing FileChannel's mapping capabilities.
38 */
39
40public class Read {
41
42 private static PrintStream err = System.err;
43
44 private static Random generator = new Random();
45
46 private static int CHARS_PER_LINE = File.separatorChar == '/' ? 5 : 6;
47
48 private static File blah;
49
50 public static void main(String[] args) throws Exception {
51 StringBuffer sb = new StringBuffer();
52 sb.setLength(4);
53
54 blah = File.createTempFile("blah", null);
55 blah.deleteOnExit();
56 initTestFile(blah);
57
58 FileInputStream fis = new FileInputStream(blah);
59 FileChannel c = fis.getChannel();
60
61 for (int x=0; x<1000; x++) {
62 long offset = x * CHARS_PER_LINE;
63 long expectedResult = offset / CHARS_PER_LINE;
64 offset = expectedResult * CHARS_PER_LINE;
65 ByteBuffer bleck = ByteBuffer.allocateDirect(CHARS_PER_LINE);
66
67 c.read(bleck);
68
69 for (int i=0; i<4; i++) {
70 byte aByte = bleck.get(i);
71 sb.setCharAt(i, (char)aByte);
72 }
73 int result = Integer.parseInt(sb.toString());
74 if (result != expectedResult) {
75 err.println("I expected "+expectedResult);
76 err.println("I got "+ result);
77 throw new Exception("Read test failed");
78 }
79 }
80
81 c.close();
82 fis.close();
83 }
84
85 /**
86 * Creates file blah:
87 * 0000
88 * 0001
89 * 0002
90 * 0003
91 * .
92 * .
93 * .
94 * 3999
95 *
96 * Blah extends beyond a single page of memory so that the
97 * ability to index into a file of multiple pages is tested.
98 */
99 private static void initTestFile(File blah) throws Exception {
100 FileOutputStream fos = new FileOutputStream(blah);
101 BufferedWriter awriter
102 = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
103
104 for(int i=0; i<4000; i++) {
105 String number = new Integer(i).toString();
106 for (int h=0; h<4-number.length(); h++)
107 awriter.write("0");
108 awriter.write(""+i);
109 awriter.newLine();
110 }
111 awriter.flush();
112 awriter.close();
113 }
114}