diff --git a/src/main/java/com/github/sylvainjuge/memoryfs/MemoryByteChannel.java b/src/main/java/com/github/sylvainjuge/memoryfs/MemoryByteChannel.java index 498f8aa..34dc6e8 100644 --- a/src/main/java/com/github/sylvainjuge/memoryfs/MemoryByteChannel.java +++ b/src/main/java/com/github/sylvainjuge/memoryfs/MemoryByteChannel.java @@ -116,7 +116,18 @@ public boolean isOpen() { @Override public void close() throws IOException { - checkOpen(); + if (! open) { + return; + } + + if (writeChannel != null) { + writeChannel.close(); + } + + if (readChannel != null) { + readChannel.close(); + } + this.open = false; } diff --git a/src/test/java/com/github/sylvainjuge/memoryfs/MemoryByteChannelTest.java b/src/test/java/com/github/sylvainjuge/memoryfs/MemoryByteChannelTest.java index 5202ccb..3c24da8 100644 --- a/src/test/java/com/github/sylvainjuge/memoryfs/MemoryByteChannelTest.java +++ b/src/test/java/com/github/sylvainjuge/memoryfs/MemoryByteChannelTest.java @@ -52,11 +52,14 @@ public void openCloseChannel() throws IOException { assertThat(c.isOpen()).isFalse(); } - @Test(expectedExceptions = ClosedChannelException.class) - public void closeTwice() throws IOException { + @Test + public void closeTwiceShouldBeFine() throws IOException { MemoryByteChannel c = newReadChannel(FileData.newEmpty()); + assertThat(c.isOpen()).isTrue(); c.close(); + assertThat(c.isOpen()).isFalse(); c.close(); + assertThat(c.isOpen()).isFalse(); } @Test(expectedExceptions = ClosedChannelException.class)