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