2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2000, 2001, 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 position method of FileChannel
|
|
26 |
*/
|
|
27 |
|
|
28 |
import java.io.*;
|
|
29 |
import java.nio.MappedByteBuffer;
|
|
30 |
import java.nio.channels.*;
|
|
31 |
import java.nio.channels.FileChannel;
|
|
32 |
import java.util.Random;
|
|
33 |
|
|
34 |
|
|
35 |
/**
|
|
36 |
* Testing FileChannel's position method.
|
|
37 |
*/
|
|
38 |
|
|
39 |
public class Position {
|
|
40 |
|
|
41 |
private static PrintStream err = System.err;
|
|
42 |
|
|
43 |
private static Random generator = new Random();
|
|
44 |
|
|
45 |
private static int CHARS_PER_LINE = File.separatorChar == '/' ? 5 : 6;
|
|
46 |
|
|
47 |
private static File blah;
|
|
48 |
|
|
49 |
public static void main(String[] args) throws Exception {
|
|
50 |
blah = File.createTempFile("blah", null);
|
|
51 |
blah.deleteOnExit();
|
|
52 |
initTestFile(blah);
|
|
53 |
|
|
54 |
FileInputStream fis = new FileInputStream(blah);
|
|
55 |
FileChannel c = fis.getChannel();
|
|
56 |
|
|
57 |
for(int i=0; i<100; i++) {
|
|
58 |
long newPos = generator.nextInt(1000);
|
|
59 |
c.position(newPos);
|
|
60 |
if (c.position() != newPos)
|
|
61 |
throw new RuntimeException("Position failed");
|
|
62 |
}
|
|
63 |
|
|
64 |
c.close();
|
|
65 |
fis.close();
|
|
66 |
}
|
|
67 |
|
|
68 |
/**
|
|
69 |
* Creates file blah:
|
|
70 |
* 0000
|
|
71 |
* 0001
|
|
72 |
* 0002
|
|
73 |
* 0003
|
|
74 |
* .
|
|
75 |
* .
|
|
76 |
* .
|
|
77 |
* 3999
|
|
78 |
*
|
|
79 |
*/
|
|
80 |
private static void initTestFile(File blah) throws Exception {
|
|
81 |
FileOutputStream fos = new FileOutputStream(blah);
|
|
82 |
BufferedWriter awriter
|
|
83 |
= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
|
|
84 |
|
|
85 |
for(int i=0; i<4000; i++) {
|
|
86 |
String number = new Integer(i).toString();
|
|
87 |
for (int h=0; h<4-number.length(); h++)
|
|
88 |
awriter.write("0");
|
|
89 |
awriter.write(""+i);
|
|
90 |
awriter.newLine();
|
|
91 |
}
|
|
92 |
awriter.flush();
|
|
93 |
awriter.close();
|
|
94 |
}
|
|
95 |
}
|