diff options
author | Sergio Giro <sgiro@google.com> | 2015-04-24 09:38:43 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2015-04-24 09:38:44 +0000 |
commit | 73e9c6e19e1ffcf00cf55fc1529f9c6c1e8a3bdf (patch) | |
tree | 6c8eec7fab8c61a30cae401c641b45c541f74c91 | |
parent | 726db7aea4817a43a1fd094d18e57de5b244ee39 (diff) | |
parent | 97db716bae1ebde9a55435e64e2db65f9e432ccd (diff) | |
download | libcore-73e9c6e19e1ffcf00cf55fc1529f9c6c1e8a3bdf.zip libcore-73e9c6e19e1ffcf00cf55fc1529f9c6c1e8a3bdf.tar.gz libcore-73e9c6e19e1ffcf00cf55fc1529f9c6c1e8a3bdf.tar.bz2 |
Merge "java/io/RandomAccessFile: do not nullify FileChannel when closing the RAF"
-rw-r--r-- | luni/src/main/java/java/io/RandomAccessFile.java | 5 | ||||
-rw-r--r-- | luni/src/test/java/libcore/java/io/RandomAccessFileTest.java | 57 |
2 files changed, 61 insertions, 1 deletions
diff --git a/luni/src/main/java/java/io/RandomAccessFile.java b/luni/src/main/java/java/io/RandomAccessFile.java index da99765..0e4fa4f 100644 --- a/luni/src/main/java/java/io/RandomAccessFile.java +++ b/luni/src/main/java/java/io/RandomAccessFile.java @@ -160,7 +160,6 @@ public class RandomAccessFile implements DataInput, DataOutput, Closeable { synchronized (this) { if (channel != null && channel.isOpen()) { channel.close(); - channel = null; } IoBridge.closeAndSignalBlockedThreads(fd); } @@ -185,6 +184,10 @@ public class RandomAccessFile implements DataInput, DataOutput, Closeable { * changes made to this file's file pointer offset are also visible in the * file channel's position and vice versa. * + * Closing the channel closes the RandomAccessFile as well. The instance + * of FileChannel returned is always the same even if the RandomAccessFile + * or the FileChannel have been closed. + * * @return this file's file channel instance. */ public final synchronized FileChannel getChannel() { diff --git a/luni/src/test/java/libcore/java/io/RandomAccessFileTest.java b/luni/src/test/java/libcore/java/io/RandomAccessFileTest.java index afe49b7..8d99457 100644 --- a/luni/src/test/java/libcore/java/io/RandomAccessFileTest.java +++ b/luni/src/test/java/libcore/java/io/RandomAccessFileTest.java @@ -20,6 +20,8 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; +import java.nio.channels.FileChannel; + import junit.framework.TestCase; import libcore.java.lang.ref.FinalizationTester; @@ -73,6 +75,61 @@ public final class RandomAccessFileTest extends TestCase { FinalizationTester.induceFinalization(); } } + + // http://b/19892782 + public void testCloseRaf_sameChannelReturned() throws Exception { + RandomAccessFile raf = new RandomAccessFile(file, "rw"); + + FileChannel fileChannelBeforeClosing = raf.getChannel(); + raf.close(); + FileChannel fileChannelAfterClosing = raf.getChannel(); + assertSame(fileChannelBeforeClosing, fileChannelAfterClosing); + } + + // http://b/19892782 + public void testCloseRaf_channelIsClosed() throws Exception { + RandomAccessFile raf = new RandomAccessFile(file, "rw"); + + FileChannel fileChannelBeforeClosing = raf.getChannel(); + raf.close(); + FileChannel fileChannelAfterClosing = raf.getChannel(); + assertFalse(fileChannelBeforeClosing.isOpen()); + } + + // http://b/19892782 + public void testCloseFileChannel_sameChannelReturned() throws Exception { + RandomAccessFile raf = new RandomAccessFile(file, "rw"); + + FileChannel fileChannelBeforeClosing = raf.getChannel(); + fileChannelBeforeClosing.close(); + + FileChannel fileChannelAfterClosing = raf.getChannel(); + assertSame(fileChannelBeforeClosing, fileChannelAfterClosing); + } + + // http://b/19892782 + public void testCloseFileChannel_returnedFileChannelIsClosed() throws Exception { + RandomAccessFile raf = new RandomAccessFile(file, "rw"); + + FileChannel fileChannelBeforeClosing = raf.getChannel(); + // This should close the Raf, and previous implementations wrongly returned a new + // open (but useless) channel in this case. + fileChannelBeforeClosing.close(); + FileChannel fileChannelAfterClosing = raf.getChannel(); + assertFalse(fileChannelBeforeClosing.isOpen()); + } + + // http://b/19892782 + public void testCloseRafBeforeGetChannel_returnChannelWithCloseFdAfterClose() throws Exception { + RandomAccessFile raf = new RandomAccessFile(file, "rw"); + raf.close(); + try { + raf.getChannel().size(); + fail(); + } catch (IOException expected) { + } + } + private void createRandomAccessFile(File file) throws Exception { // TODO: fix our register maps and remove this otherwise unnecessary // indirection! (http://b/5412580) |