author | jbhateja |
Tue, 26 Nov 2019 16:09:25 +0300 | |
changeset 59278 | 8375560db76b |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @summary Test read method of FileChannel |
|
26 |
*/ |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.nio.ByteBuffer; |
|
30 |
import java.nio.CharBuffer; |
|
31 |
import java.nio.channels.*; |
|
32 |
import java.nio.channels.FileChannel; |
|
33 |
import java.util.Random; |
|
34 |
||
35 |
||
36 |
/** |
|
37 |
* Testing FileChannel's mapping capabilities. |
|
38 |
*/ |
|
39 |
||
40 |
public 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(); |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
83 |
blah.delete(); |
2 | 84 |
} |
85 |
||
86 |
/** |
|
87 |
* Creates file blah: |
|
88 |
* 0000 |
|
89 |
* 0001 |
|
90 |
* 0002 |
|
91 |
* 0003 |
|
92 |
* . |
|
93 |
* . |
|
94 |
* . |
|
95 |
* 3999 |
|
96 |
* |
|
97 |
* Blah extends beyond a single page of memory so that the |
|
98 |
* ability to index into a file of multiple pages is tested. |
|
99 |
*/ |
|
100 |
private static void initTestFile(File blah) throws Exception { |
|
101 |
FileOutputStream fos = new FileOutputStream(blah); |
|
102 |
BufferedWriter awriter |
|
103 |
= new BufferedWriter(new OutputStreamWriter(fos, "8859_1")); |
|
104 |
||
105 |
for(int i=0; i<4000; i++) { |
|
106 |
String number = new Integer(i).toString(); |
|
107 |
for (int h=0; h<4-number.length(); h++) |
|
108 |
awriter.write("0"); |
|
109 |
awriter.write(""+i); |
|
110 |
awriter.newLine(); |
|
111 |
} |
|
112 |
awriter.flush(); |
|
113 |
awriter.close(); |
|
114 |
} |
|
115 |
} |