1 /*
   2  * Copyright (c) 2006, 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 /**
  25  *
  26  * @test
  27  * @bug 6322678
  28  * @summary Test for making sure that fd is closed during
  29  *          finalization of a stream, when an associated
  30  *          file channel is not available
  31  */
  32 
  33 import java.io.*;
  34 import java.nio.*;
  35 import java.nio.channels.*;
  36 
  37 public class FileChannelFDTest {
  38 
  39     static byte data[] = new byte[] {48, 49, 50, 51, 52, 53, 54, 55, 56, 57,};
  40     static String inFileName = "fd-in-test.txt";
  41     static String outFileName = "fd-out-test.txt";
  42     static File inFile;
  43     static File outFile;
  44 
  45     private static void writeToInFile() throws IOException {
  46         FileOutputStream out = new FileOutputStream(inFile);
  47         out.write(data);
  48         out.close();
  49     }
  50 
  51     public static void main(String[] args)
  52                 throws Exception {
  53 
  54         inFile= new File(System.getProperty("test.dir", "."),
  55                         inFileName);
  56         inFile.createNewFile();
  57         inFile.deleteOnExit();
  58         writeToInFile();
  59 
  60         outFile  = new File(System.getProperty("test.dir", "."),
  61                         outFileName);
  62         outFile.createNewFile();
  63         outFile.deleteOnExit();
  64 
  65         doFileChannel();
  66     }
  67 
  68      private static void doFileChannel() throws Exception {
  69 
  70         FileInputStream fis = new FileInputStream(inFile);
  71         FileDescriptor fd = fis.getFD();
  72         FileChannel fc = fis.getChannel();
  73         System.out.println("Created fis:" + fis);
  74 
  75         /**
  76          * Encourage the GC
  77          */
  78         fis = null;
  79         fc = null;
  80         System.gc();
  81         Thread.sleep(500);
  82 
  83         if (fd.valid()) {
  84             throw new Exception("Finalizer either didn't run --" +
  85                 "try increasing the Thread's sleep time after System.gc();" +
  86                 "or the finalizer didn't close the file");
  87         }
  88 
  89         System.out.println("File Closed successfully");
  90         System.out.println();
  91   }
  92 }