1 /*
2 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. 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 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.
22 */
33 import java.util.Random;
34
35
36 /**
37 * Testing FileChannel's size method.
38 */
39
40 public class Size {
41
42 private static Random generator = new Random();
43
44 private static File blah;
45
46 public static void main(String[] args) throws Exception {
47 test1();
48 test2();
49 }
50
51 private static void test1() throws Exception {
52 blah = File.createTempFile("blah", null);
53 blah.deleteOnExit();
54 for(int i=0; i<100; i++) {
55 long testSize = generator.nextInt(1000);
56 initTestFile(blah, testSize);
57 FileInputStream fis = new FileInputStream(blah);
58 FileChannel c = fis.getChannel();
59 if (c.size() != testSize)
60 throw new RuntimeException("Size failed");
61 c.close();
62 fis.close();
63 }
64 blah.delete();
65 }
66
67 // Test for bug 4563125
68 private static void test2() throws Exception {
69 // Windows and Linux can't handle the really large file sizes for a truncate
70 // or a positional write required by the test for 4563125
71 String osName = System.getProperty("os.name");
72 if (osName.startsWith("SunOS") || osName.contains("OS X")) {
73 blah = File.createTempFile("blah", null);
74 long testSize = ((long)Integer.MAX_VALUE) * 2;
75 initTestFile(blah, 10);
76 RandomAccessFile raf = new RandomAccessFile(blah, "rw");
77 FileChannel fc = raf.getChannel();
78 fc.size();
79 fc.map(FileChannel.MapMode.READ_WRITE, testSize, 10);
80 if (fc.size() != testSize + 10)
81 throw new RuntimeException("Size failed " + fc.size());
82 fc.close();
83 raf.close();
84 blah.delete();
85 }
86 }
87
88 /**
89 * Creates file blah of specified size in bytes.
90 *
91 */
92 private static void initTestFile(File blah, long size) throws Exception {
93 if (blah.exists())
94 blah.delete();
95 FileOutputStream fos = new FileOutputStream(blah);
96 BufferedWriter awriter
97 = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
98
99 for(int i=0; i<size; i++) {
100 awriter.write("e");
101 }
102 awriter.flush();
103 awriter.close();
104 }
105 }
|
1 /*
2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. 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 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.
22 */
33 import java.util.Random;
34
35
36 /**
37 * Testing FileChannel's size method.
38 */
39
40 public class Size {
41
42 private static Random generator = new Random();
43
44 private static File blah;
45
46 public static void main(String[] args) throws Exception {
47 test1();
48 test2();
49 }
50
51 private static void test1() throws Exception {
52 blah = File.createTempFile("blah", null);
53 for(int i=0; i<100; i++) {
54 long testSize = generator.nextInt(1000);
55 initTestFile(blah, testSize);
56 FileInputStream fis = new FileInputStream(blah);
57 FileChannel c = fis.getChannel();
58 if (c.size() != testSize)
59 throw new RuntimeException("Size failed");
60 c.close();
61 fis.close();
62 }
63 blah.delete();
64 }
65
66 // Test for bug 4563125
67 private static void test2() throws Exception {
68 // Windows and Linux can't handle the really large file sizes for a truncate
69 // or a positional write required by the test for 4563125
70 String osName = System.getProperty("os.name");
71 if (osName.startsWith("SunOS") || osName.contains("OS X")) {
72 blah = File.createTempFile("blah", null);
73 long testSize = ((long)Integer.MAX_VALUE) * 2;
74 initTestFile(blah, 10);
75 RandomAccessFile raf = new RandomAccessFile(blah, "rw");
76 FileChannel fc = raf.getChannel();
77 fc.size();
78 fc.map(FileChannel.MapMode.READ_WRITE, testSize, 10);
79 if (fc.size() != testSize + 10)
80 throw new RuntimeException("Size failed " + fc.size());
81 fc.close();
82 raf.close();
83 blah.delete();
84 }
85 }
86
87 /**
88 * Creates file blah of specified size in bytes.
89 *
90 */
91 private static void initTestFile(File blah, long size) throws Exception {
92 FileOutputStream fos = new FileOutputStream(blah);
93 BufferedWriter awriter
94 = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
95
96 for(int i=0; i<size; i++) {
97 awriter.write("e");
98 }
99 awriter.flush();
100 awriter.close();
101 }
102 }
|