Path: blob/master/test/jdk/java/io/FileDescriptor/Finalize.java
41149 views
/*1* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24*25* @test26* @bug 632267827* @summary Test for making sure that FIS/FOS.finalize() will28* not disturb the FD that is still in use.29*/3031import java.io.*;32import java.nio.*;33import java.nio.channels.*;3435public class Finalize {3637static byte data[] = new byte[] {48, 49, 50, 51, 52, 53, 54, 55, 56, 57,};38static String inFileName = "fd-in-test.txt";39static String outFileName = "fd-out-test.txt";40static File inFile;41static File outFile;4243public static void main(String[] args)44throws Exception {45Thread.sleep(5000);4647inFile= new File(System.getProperty("test.dir", "."),48inFileName);49inFile.createNewFile();50inFile.deleteOnExit();51writeToInFile();5253doFileInputStream();5455outFile = new File(System.getProperty("test.dir", "."),56outFileName);57outFile.createNewFile();58outFile.deleteOnExit();5960doFileOutputStream();61doRandomAccessFile();62doFileChannel();63}6465private static void doFileInputStream() throws Exception {6667/* Create initial FIS for file */68FileInputStream fis1 = new FileInputStream(inFile);6970/* Get the FileDescriptor from the fis */71FileDescriptor fd = fis1.getFD();7273/*74* Create a new FIS based on the existing FD75* (so the two FIS's share the same native fd)76*/77FileInputStream fis2 = new FileInputStream(fd);7879/* allow fis1 to be gc'ed */80fis1 = null;81int ret = 0;8283/* encourage gc */84System.gc();85Thread.sleep(200);8687while((ret = fis2.read()) != -1 ) {88/*89* read from fis2 - when fis1 is gc'ed and finalizer is run,90* read should not fail91*/92System.out.println("read from fis2:" + ret);93}94fis2.close();95}9697private static void writeToInFile() throws IOException {98FileOutputStream out = new FileOutputStream(inFile);99out.write(data);100out.close();101}102103private static void doFileOutputStream()104throws Exception {105106System.out.println("--------FileOutputStream Test Started----------");107108/*Create initial FIS for file */109FileOutputStream fos1 = new FileOutputStream(outFile);110111/* Get the FileDescriptor from the fos */112FileDescriptor fd = fos1.getFD();113FileOutputStream fos2 = new FileOutputStream(fd);114115/* allow fos1 to be gc'ed */116fos1 = null;117118/* encourage gc */119System.gc();120Thread.sleep(200);121122/*123* write to fos2 - when fos1 is gc'ed and finalizer is run,124* write to fos2 should not fail125*/126fos2.write(data);127System.out.println("wrote:" + data.length + " bytes to fos2");128fos2.close();129130System.out.println("--------FileOutputStream Test Over----------");131System.out.println();132}133134135private static void doRandomAccessFile()136throws Exception {137138System.out.println("--------RandomAccessFile Read Test Started----------");139140// Create initial FIS for file141RandomAccessFile raf = new RandomAccessFile(inFile, "r");142143/* Get the FileDescriptor from the fis */144FileDescriptor fd = raf.getFD();145146/* Create a new FIS based on the existing FD147* (so the two FIS's share the same native fd)148*/149FileInputStream fis = new FileInputStream(fd);150151/* allow fis to be gc'ed */152fis = null;153int ret = 0;154155/* encourage gc */156System.gc();157Thread.sleep(50);158159/*160* read from raf - when fis is gc'ed and finalizer is run,161* read from raf should not fail162*/163while((ret = raf.read()) != -1 ) {164System.out.println("read from raf:" + ret);165}166raf.close();167Thread.sleep(200);168169System.out.println("--------RandomAccessFile Write Test Started----------");170System.out.println();171172raf = new RandomAccessFile(outFile, "rw");173fd = raf.getFD();174FileOutputStream fos = new FileOutputStream(fd);175176/* allow fos to be gc'ed */177fos = null;178179/* encourage gc */180System.gc();181Thread.sleep(200);182183/*184* write to raf - when fos is gc'ed and finalizer is run,185* write to raf should not fail186*/187raf.write(data);188System.out.println("wrote:" + data.length + " bytes to raf");189raf.close();190191System.out.println("--------RandomAccessFile Write Test Over----------");192System.out.println();193}194195private static void doFileChannel() throws Exception {196197System.out.println("--------FileChannel Read Test Started----------");198System.out.println();199200FileInputStream fis1 = new FileInputStream(inFile);201202/* Get the FileDescriptor from the fis */203FileDescriptor fd = fis1.getFD();204205/* Create a new FIS based on the existing FD206* (so the two FIS's share the same native fd)207*/208FileInputStream fis2 = new FileInputStream(fd);209FileChannel fc2 = fis2.getChannel();210211/**212* Encourage the GC213*/214fis1 = null;215System.gc();216Thread.sleep(200);217218int ret = 1;219ByteBuffer bb = ByteBuffer.allocateDirect(1);220ret = fc2.read(bb);221System.out.println("read " + ret + " bytes from fc2:");222fc2.close();223224System.out.println("--------FileChannel Read Test Over----------");225System.out.println();226227System.out.println("--------FileChannel Write Test Started----------");228229FileOutputStream fos1 = new FileOutputStream(outFile);230231/* Get the FileDescriptor from the fos */232fd = fos1.getFD();233FileOutputStream fos2 = new FileOutputStream(fd);234fc2 = fos2.getChannel();235236/**237* Encourage the GC238*/239fos1 = null;240System.gc();241Thread.sleep(200);242243/*244* write to fc2 - when fos1 is gc'ed and finalizer is run,245* write to fc2 should not fail246*/247bb = ByteBuffer.allocateDirect(data.length)248.put(data)249.flip();250251ret = fc2.write(bb);252System.out.println("Wrote:" + ret + " bytes to fc2");253fc2.close();254255System.out.println("--------Channel Write Test Over----------");256}257}258259260