Search code examples
javafileniofilechannel

Java doc specification for Filechannel transferTO


As per Java doc, ClosedByInterruptException is thrown when:

  @throws  ClosedByInterruptException
         If another thread interrupts the current thread while the
         transfer is in progress, thereby closing both channels and
         setting the current thread's interrupt status

I want to get some clarification. For example, there is a file which expands occasionally as new data gets added to the end of it. In that instance, does the above line mean that an exception would be thrown if at the time of new data being added, the transferTO method of FileChannel tries to copy the content.

Here, Another thread would be transferTO method of Filechannel Current thread would be that who is trying to write more data to it.

Is that correct?


Solution

  • I think that ClosedByInterruptException in the method transferTo() of FileChannel is thrown when other thread calls the interrupt() method on calling thread.

    For example:

        import java.io.IOException;
        import java.io.RandomAccessFile;
        import java.nio.channels.FileChannel;
    
        public class Test {
    
            public static void main(final String[] args) throws Exception {
                final RandomAccessFile fromFile = new RandomAccessFile("d:/pp.dat", "rw");
                final FileChannel fromChannel = fromFile.getChannel();
    
                final RandomAccessFile toFile = new RandomAccessFile("d:/out.dat", "rw");
                final FileChannel toChannel = toFile.getChannel();
    
                final long position = 0;
                final long count = fromChannel.size();
                final Runnable r = new Runnable() {
                    @Override
                    public void run() {
                        try {
                            fromChannel.transferTo(position, count, toChannel);
                        } catch (final IOException e) {
                            e.printStackTrace();
                        }
                    }
                };
                final Thread t = new Thread(r);
                t.start();
                t.interrupt();
            }
        }
    

    Relating to the question you make about another thread writing concurrently (if I understand correctly your question), from javadoc:

    File channels are safe for use by multiple concurrent threads. The close method may be invoked at any time, as specified by the Channel interface. Only one operation that involves the channel's position or can change its file's size may be in progress at any given time; attempts to initiate a second such operation while the first is still in progress will block until the first operation completes. Other operations, in particular those that take an explicit position, may proceed concurrently; whether they in fact do so is dependent upon the underlying implementation and is therefore unspecified.

    The view of a file provided by an instance of this class is guaranteed to be consistent with other views of the same file provided by other instances in the same program. The view provided by an instance of this class may or may not, however, be consistent with the views seen by other concurrently-running programs due to caching performed by the underlying operating system and delays induced by network-filesystem protocols. This is true regardless of the language in which these other programs are written, and whether they are running on the same machine or on some other machine. The exact nature of any such inconsistencies are system-dependent and are therefore unspecified.