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  * @key randomness
  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 {
  44         testSmallFile();
  45         testLargeFile();
  46     }
  47 
  48     private static void testSmallFile() throws Exception {
  49         File smallFile = new File("smallFileTest");
  50         Random generator = new Random();
  51         for(int i=0; i<100; i++) {
  52             long testSize = generator.nextInt(1000);
  53             initTestFile(smallFile, testSize);
  54             try (FileChannel c = new FileInputStream(smallFile).getChannel()) {
  55                 if (c.size() != testSize) {
  56                     throw new RuntimeException("Size failed in testSmallFile. "
  57                                              + "Expect size " + testSize
  58                                              + ", actual size " + c.size());
  59                 }
  60             }
  61         }
  62         smallFile.deleteOnExit();
  63     }
  64 
  65     // Test for bug 4563125
  66     private static void testLargeFile() throws Exception {
  67         File largeFile = new File("largeFileTest");
  68         long testSize = ((long)Integer.MAX_VALUE) * 2;
  69         initTestFile(largeFile, 10);
  70         try (FileChannel fc = new RandomAccessFile(largeFile, "rw").getChannel())
  71         {
  72             fc.size();
  73             fc.map(FileChannel.MapMode.READ_WRITE, testSize, 10);
  74             if (fc.size() != testSize + 10) {
  75                 throw new RuntimeException("Size failed in testLargeFile. "
  76                                          + "Expect size " + (testSize + 10)
  77                                          + ", actual size " + fc.size());
  78             }
  79         }
  80         largeFile.deleteOnExit();
  81     }
  82 
  83     /**
  84      * Create a file with the specified size in bytes.
  85      *
  86      */
  87     private static void initTestFile(File f, long size) throws Exception {
  88         try (BufferedWriter awriter = new BufferedWriter(
  89                 new OutputStreamWriter(new FileOutputStream(f), "8859_1")))
  90         {
  91             for(int i=0; i<size; i++) {
  92                 awriter.write("e");
  93             }
  94         }
  95     }
  96 }
--- EOF ---