author | jbhateja |
Tue, 26 Nov 2019 16:09:25 +0300 | |
changeset 59278 | 8375560db76b |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
21840
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
2 |
* Copyright (c) 2000, 2013, 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 |
* @bug 4563125 |
|
26 |
* @summary Test size method of FileChannel |
|
5970
d4e98bbfb0be
6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents:
5506
diff
changeset
|
27 |
* @run main/othervm Size |
30046 | 28 |
* @key randomness |
2 | 29 |
*/ |
30 |
||
31 |
import java.io.*; |
|
32 |
import java.nio.MappedByteBuffer; |
|
33 |
import java.nio.channels.*; |
|
34 |
import java.util.Random; |
|
35 |
||
36 |
||
37 |
/** |
|
38 |
* Testing FileChannel's size method. |
|
39 |
*/ |
|
40 |
||
41 |
public class Size { |
|
42 |
||
43 |
public static void main(String[] args) throws Exception { |
|
21840
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
44 |
testSmallFile(); |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
45 |
testLargeFile(); |
2 | 46 |
} |
47 |
||
21840
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
48 |
private static void testSmallFile() throws Exception { |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
49 |
File smallFile = new File("smallFileTest"); |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
50 |
Random generator = new Random(); |
2 | 51 |
for(int i=0; i<100; i++) { |
52 |
long testSize = generator.nextInt(1000); |
|
21840
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
53 |
initTestFile(smallFile, testSize); |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
54 |
try (FileChannel c = new FileInputStream(smallFile).getChannel()) { |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
55 |
if (c.size() != testSize) { |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
56 |
throw new RuntimeException("Size failed in testSmallFile. " |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
57 |
+ "Expect size " + testSize |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
58 |
+ ", actual size " + c.size()); |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
59 |
} |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
60 |
} |
2 | 61 |
} |
21840
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
62 |
smallFile.deleteOnExit(); |
2 | 63 |
} |
64 |
||
65 |
// Test for bug 4563125 |
|
21840
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
66 |
private static void testLargeFile() throws Exception { |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
67 |
File largeFile = new File("largeFileTest"); |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
68 |
long testSize = ((long)Integer.MAX_VALUE) * 2; |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
69 |
initTestFile(largeFile, 10); |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
70 |
try (FileChannel fc = new RandomAccessFile(largeFile, "rw").getChannel()) |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
71 |
{ |
2 | 72 |
fc.size(); |
73 |
fc.map(FileChannel.MapMode.READ_WRITE, testSize, 10); |
|
21840
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
74 |
if (fc.size() != testSize + 10) { |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
75 |
throw new RuntimeException("Size failed in testLargeFile. " |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
76 |
+ "Expect size " + (testSize + 10) |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
77 |
+ ", actual size " + fc.size()); |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
78 |
} |
2 | 79 |
} |
21840
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
80 |
largeFile.deleteOnExit(); |
2 | 81 |
} |
82 |
||
83 |
/** |
|
21840
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
84 |
* Create a file with the specified size in bytes. |
2 | 85 |
* |
86 |
*/ |
|
21840
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
87 |
private static void initTestFile(File f, long size) throws Exception { |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
88 |
try (BufferedWriter awriter = new BufferedWriter( |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
89 |
new OutputStreamWriter(new FileOutputStream(f), "8859_1"))) |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
90 |
{ |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
91 |
for(int i=0; i<size; i++) { |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
92 |
awriter.write("e"); |
394835b0d057
8028628: java/nio/channels/FileChannel/Size.java failed once in the same binary run
dxu
parents:
12538
diff
changeset
|
93 |
} |
2 | 94 |
} |
95 |
} |
|
96 |
} |