test/java/io/FileDescriptor/Finalize.java

Print this page




 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 }


 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                        .put(data)
 250                        .flip();
 251 
 252         ret = fc2.write(bb);
 253         System.out.println("Wrote:" +  ret + " bytes to fc2");
 254         fc2.close();
 255 
 256         System.out.println("--------Channel Write Test Over----------");
 257     }
 258 }