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 */ 23 24 /* @test 25 * @bug 4563125 26 * @summary Test size method of FileChannel 27 * @run main/othervm Size 28 */ 29 30 import java.io.*; 31 import java.nio.MappedByteBuffer; 32 import java.nio.channels.*; 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 }