Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what happens to FileChannel if RandomAccessFile is garbage collected

 
Oly Diesel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens to the FileChannel if the underlying RAF is garbage collected?
Does the FileChannel have a reference back to the RAF in order to prevent it's garbage collection?

If not the following method would be risky:
<pre>
static FileChannel getChannel(final File pFile) throws IOException {

RandomAccessFile raFile = new RandomAccessFile(pFile, READ_WRITE_MODE);
FileChannel channel = raFile.getChannel();

return channel;
}
</pre>

thanks,
oly
 
Josh Allen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Oly Diesel:
What happens to the FileChannel if the underlying RAF is garbage collected?
Does the FileChannel have a reference back to the RAF in order to prevent it's garbage collection?

If not the following method would be risky:
<pre>
static FileChannel getChannel(final File pFile) throws IOException {

RandomAccessFile raFile = new RandomAccessFile(pFile, READ_WRITE_MODE);
FileChannel channel = raFile.getChannel();

return channel;
}
</pre>

thanks,
oly
 
Josh Allen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you wouldn't let it be garbage collected without closing it, right?
So if you close the FileStream and then try to use the channel you'll probably get an IOException. Also be aware that you still can't use the NIO package in the assignment.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also be aware that you still can't use the NIO package in the assignment.
Actually, for some assignments you can. Search this forum for NIO, and you can find the thread dealing with it. I think it boils down to you can use NIO as long as your instructions do not explicitly say you cannot. Mine don't mention NIO at all, but I am leaving it out just to be safe...
 
Oly Diesel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Allen and Paul,

I actually did think of letting RAF beeing garbage collected without closing it because I assumed the FileChannel would do the right thing. A stupid assumption, but if you want to keep a channel you always have to keep the generating class, which is cumbersome and not explicitely described in the specs...

Apart from 'nio' in 'junior' URLyBird does not mention NIO :-)

Regards,
Oly
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic