blob: 00976c92123675717dd147bd20ac658286af5632 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 *
26 * @test
27 * @bug 6322678
28 * @summary Test for making sure that FIS/FOS.finalize() will
29 * not disturb the FD that is still in use.
30 */
31
32import java.io.*;
33import java.nio.*;
34import java.nio.channels.*;
35
36public class Finalize {
37
38 static byte data[] = new byte[] {48, 49, 50, 51, 52, 53, 54, 55, 56, 57,};
39 static String inFileName = "fd-in-test.txt";
40 static String outFileName = "fd-out-test.txt";
41 static File inFile;
42 static File outFile;
43
44 public static void main(String[] args)
45 throws Exception {
46 Thread.sleep(5000);
47
48 inFile= new File(System.getProperty("test.dir", "."),
49 inFileName);
50 inFile.createNewFile();
51 inFile.deleteOnExit();
52 writeToInFile();
53
54 doFileInputStream();
55
56 outFile = new File(System.getProperty("test.dir", "."),
57 outFileName);
58 outFile.createNewFile();
59 outFile.deleteOnExit();
60
61 doFileOutputStream();
62 doRandomAccessFile();
63 doFileChannel();
64 }
65
66 private static void doFileInputStream() throws Exception {
67
68 /* Create initial FIS for file */
69 FileInputStream fis1 = new FileInputStream(inFile);
70
71 /* Get the FileDescriptor from the fis */
72 FileDescriptor fd = fis1.getFD();
73
74 /*
75 * Create a new FIS based on the existing FD
76 * (so the two FIS's share the same native fd)
77 */
78 FileInputStream fis2 = new FileInputStream(fd);
79
80 /* allow fis1 to be gc'ed */
81 fis1 = null;
82 int ret = 0;
83
84 /* encourage gc */
85 System.gc();
86 Thread.sleep(200);
87
88 while((ret = fis2.read()) != -1 ) {
89 /*
90 * read from fis2 - when fis1 is gc'ed and finalizer is run,
91 * read should not fail
92 */
93 System.out.println("read from fis2:" + ret);
94 }
95 fis2.close();
96 }
97
98 private static void writeToInFile() throws IOException {
99 FileOutputStream out = new FileOutputStream(inFile);
100 out.write(data);
101 out.close();
102 }
103
104 private static void doFileOutputStream()
105 throws Exception {
106
107 System.out.println("--------FileOutputStream Test Started----------");
108
109 /*Create initial FIS for file */
110 FileOutputStream fos1 = new FileOutputStream(outFile);
111
112 /* Get the FileDescriptor from the fos */
113 FileDescriptor fd = fos1.getFD();
114 FileOutputStream fos2 = new FileOutputStream(fd);
115
116 /* allow fos1 to be gc'ed */
117 fos1 = null;
118
119 /* encourage gc */
120 System.gc();
121 Thread.sleep(200);
122
123 /*
124 * write to fos2 - when fos1 is gc'ed and finalizer is run,
125 * write to fos2 should not fail
126 */
127 fos2.write(data);
128 System.out.println("wrote:" + data.length + " bytes to fos2");
129 fos2.close();
130
131 System.out.println("--------FileOutputStream Test Over----------");
132 System.out.println();
133 }
134
135
136 private static void doRandomAccessFile()
137 throws Exception {
138
139 System.out.println("--------RandomAccessFile Read Test Started----------");
140
141 // Create initial FIS for file
142 RandomAccessFile raf = new RandomAccessFile(inFile, "r");
143
144 /* Get the FileDescriptor from the fis */
145 FileDescriptor fd = raf.getFD();
146
147 /* Create a new FIS based on the existing FD
148 * (so the two FIS's share the same native fd)
149 */
150 FileInputStream fis = new FileInputStream(fd);
151
152 /* allow fis to be gc'ed */
153 fis = null;
154 int ret = 0;
155
156 /* encourage gc */
157 System.gc();
158 Thread.sleep(50);
159
160 /*
161 * read from raf - when fis is gc'ed and finalizer is run,
162 * read from raf should not fail
163 */
164 while((ret = raf.read()) != -1 ) {
165 System.out.println("read from raf:" + ret);
166 }
167 raf.close();
168 Thread.sleep(200);
169
170 System.out.println("--------RandomAccessFile Write Test Started----------");
171 System.out.println();
172
173 raf = new RandomAccessFile(outFile, "rw");
174 fd = raf.getFD();
175 FileOutputStream fos = new FileOutputStream(fd);
176
177 /* allow fos to be gc'ed */
178 fos = null;
179
180 /* encourage gc */
181 System.gc();
182 Thread.sleep(200);
183
184 /*
185 * write to raf - when fos is gc'ed and finalizer is run,
186 * write to raf should not fail
187 */
188 raf.write(data);
189 System.out.println("wrote:" + data.length + " bytes to raf");
190 raf.close();
191
192 System.out.println("--------RandomAccessFile Write Test Over----------");
193 System.out.println();
194 }
195
196 private static void doFileChannel() throws Exception {
197
198 System.out.println("--------FileChannel Read Test Started----------");
199 System.out.println();
200
201 FileInputStream fis1 = new FileInputStream(inFile);
202
203 /* Get the FileDescriptor from the fis */
204 FileDescriptor fd = fis1.getFD();
205
206 /* Create a new FIS based on the existing FD
207 * (so the two FIS's share the same native fd)
208 */
209 FileInputStream fis2 = new FileInputStream(fd);
210 FileChannel fc2 = fis2.getChannel();
211
212 /**
213 * Encourage the GC
214 */
215 fis1 = null;
216 System.gc();
217 Thread.sleep(200);
218
219 int ret = 1;
220 ByteBuffer bb = ByteBuffer.allocateDirect(1);
221 ret = fc2.read(bb);
222 System.out.println("read " + ret + " bytes from fc2:");
223 fc2.close();
224
225 System.out.println("--------FileChannel Read Test Over----------");
226 System.out.println();
227
228 System.out.println("--------FileChannel Write Test Started----------");
229
230 FileOutputStream fos1 = new FileOutputStream(outFile);
231
232 /* Get the FileDescriptor from the fos */
233 fd = fos1.getFD();
234 FileOutputStream fos2 = new FileOutputStream(fd);
235 fc2 = fos2.getChannel();
236
237 /**
238 * Encourage the GC
239 */
240 fos1 = null;
241 System.gc();
242 Thread.sleep(200);
243
244 /*
245 * write to fc2 - when fos1 is gc'ed and finalizer is run,
246 * write to fc2 should not fail
247 */
248 bb = ByteBuffer.allocateDirect(data.length);
249 bb = bb.put(data);
250 bb = (ByteBuffer) bb.flip();
251 ret = fc2.write(bb);
252 System.out.println("Wrote:" + ret + " bytes to fc2");
253 fc2.close();
254
255 System.out.println("--------Channel Write Test Over----------");
256 }
257}