Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does FileChannel.close() close the underlying stream?

Tags:

Like the title; Does closing a FileChannel close the underlying file stream?


From the AbstractInterruptibleChannel.close() API docs you can read:

Closes this channel.

If the channel has already been closed then this method returns immediately. Otherwise it marks the channel as closed and then invokes the implCloseChannel method in order to complete the close operation.

Which invokes AbstractInterruptibleChannel.implCloseChannel:

Closes this channel.

This method is invoked by the close method in order to perform the actual work of closing the channel. This method is only invoked if the channel has not yet been closed, and it is never invoked more than once.

An implementation of this method must arrange for any other thread that is blocked in an I/O operation upon this channel to return immediately, either by throwing an exception or by returning normally.

And that doesn't say anything about the stream. So in fact, when I do:

public static void copyFile(File from, File to)          throws IOException, FileNotFoundException {      FileChannel sc = null;     FileChannel dc = null;      try {         to.createNewFile();          sc = new FileInputStream(from).getChannel();          dc = new FileOutputStream(to).getChannel();          long pos = 0;         long total = sc.size();         while (pos < total)             pos += dc.transferFrom(sc, pos, total - pos);      } finally {         if (sc != null)              sc.close();         if (dc != null)              dc.close();     } } 

...I leave the streams open?

like image 209
dacwe Avatar asked Oct 19 '12 08:10

dacwe


People also ask

Does Bufferedinputstream close underlying stream?

Yes. The underlying stream will be closed.

How does FileChannel work Java?

Reads a sequence of bytes from this channel into the given buffer, starting at the given file position. This method works in the same manner as the read(ByteBuffer) method, except that bytes are read starting at the given file position rather than at the channel's current position.

Do we need to close streams in Java?

Streams have a BaseStream. close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned by Files. lines(Path, Charset)) will require closing.

Why we need to close the stream objects always after usage?

You should always close a stream in order to free open resources on your OS. Opening a stream always returns an identifier which you can use to close it wherever you are in your code (as long as the identifier is valid), whether their from another method or class.


1 Answers

The answer is 'yes' but there's nothing in the Javadoc that actually says so. The reason is that FileChannel itself is an abstract class, and its concrete implementation provides the implCloseChannel() method, which closes the underlying FD. However due to that architecture and the fact that implCloseChannel() is protected, this doesn't get documented.

like image 81
user207421 Avatar answered Sep 24 '22 05:09

user207421