Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does InputStreamReader.close() work internally?

Tags:

java

file-io

It seems that the actual close() implementation is tucked away somewhere in the hierarchy of base classes and implementations of abstract methods. For example, are you guaranteed that the file descriptor gets released? Here is the closest thing to what I wanted to know:

   nd.preClose(fd);
   long th;
   if ((th = readerThread) != 0)
   NativeThread.signal(th);
   if ((th = writerThread) != 0)
   NativeThread.signal(th);
   if (!isRegistered())
   kill();

from DatagramChannelImpl. Can anyone translate into English?

like image 885
lynxoid Avatar asked Mar 16 '12 04:03

lynxoid


1 Answers

So every time I source dive Sun Oracle's Java code I am disappointed. nd as a variable name? Does not get more opaque than that.

What nd is referencing is NativeDispatcher which handles platform specific operations, such as closing File Descriptors (kindly named the variable fd). I can only assume the NativeThread check and signal is to clean up the read/write threads, the source does not lend much information. The isRegistered() from AbstractSelectableChannel makes sure the Channel is not being used and the kill method is what closes everything down and finally calls the nd.close(fd);

like image 100
mguymon Avatar answered Nov 06 '22 14:11

mguymon