From 7888d62f8a101394c79ec87b8349e689ed7211a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Schl=C3=B6=C3=9Fin?= Date: Fri, 22 Jan 2016 11:26:46 +0100 Subject: [PATCH 1/2] MemoryByteChannel can be closed more than once --- .../com/github/sylvainjuge/memoryfs/MemoryByteChannel.java | 5 ++++- .../github/sylvainjuge/memoryfs/MemoryByteChannelTest.java | 7 ------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/github/sylvainjuge/memoryfs/MemoryByteChannel.java b/src/main/java/com/github/sylvainjuge/memoryfs/MemoryByteChannel.java index 498f8aa..e7c5cb7 100644 --- a/src/main/java/com/github/sylvainjuge/memoryfs/MemoryByteChannel.java +++ b/src/main/java/com/github/sylvainjuge/memoryfs/MemoryByteChannel.java @@ -116,8 +116,11 @@ public boolean isOpen() { @Override public void close() throws IOException { - checkOpen(); this.open = false; + if (writeChannel != null) + writeChannel.close(); + if (readChannel != null) + readChannel.close(); } private void checkOpen() throws ClosedChannelException { diff --git a/src/test/java/com/github/sylvainjuge/memoryfs/MemoryByteChannelTest.java b/src/test/java/com/github/sylvainjuge/memoryfs/MemoryByteChannelTest.java index 5202ccb..33e6267 100644 --- a/src/test/java/com/github/sylvainjuge/memoryfs/MemoryByteChannelTest.java +++ b/src/test/java/com/github/sylvainjuge/memoryfs/MemoryByteChannelTest.java @@ -52,13 +52,6 @@ public void openCloseChannel() throws IOException { assertThat(c.isOpen()).isFalse(); } - @Test(expectedExceptions = ClosedChannelException.class) - public void closeTwice() throws IOException { - MemoryByteChannel c = newReadChannel(FileData.newEmpty()); - c.close(); - c.close(); - } - @Test(expectedExceptions = ClosedChannelException.class) public void readClosed() throws IOException { MemoryByteChannel c = newReadChannel(FileData.newEmpty()); From 0208f48a36b293899a6f1ef5ff901d49b5c772d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Schl=C3=B6=C3=9Fin?= Date: Mon, 25 Jan 2016 14:17:51 +0100 Subject: [PATCH 2/2] introduces a success test for double close --- .../sylvainjuge/memoryfs/MemoryByteChannel.java | 14 +++++++++++--- .../memoryfs/MemoryByteChannelTest.java | 10 ++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/sylvainjuge/memoryfs/MemoryByteChannel.java b/src/main/java/com/github/sylvainjuge/memoryfs/MemoryByteChannel.java index e7c5cb7..34dc6e8 100644 --- a/src/main/java/com/github/sylvainjuge/memoryfs/MemoryByteChannel.java +++ b/src/main/java/com/github/sylvainjuge/memoryfs/MemoryByteChannel.java @@ -116,11 +116,19 @@ public boolean isOpen() { @Override public void close() throws IOException { - this.open = false; - if (writeChannel != null) + if (! open) { + return; + } + + if (writeChannel != null) { writeChannel.close(); - if (readChannel != null) + } + + if (readChannel != null) { readChannel.close(); + } + + this.open = false; } private void checkOpen() throws ClosedChannelException { diff --git a/src/test/java/com/github/sylvainjuge/memoryfs/MemoryByteChannelTest.java b/src/test/java/com/github/sylvainjuge/memoryfs/MemoryByteChannelTest.java index 33e6267..3c24da8 100644 --- a/src/test/java/com/github/sylvainjuge/memoryfs/MemoryByteChannelTest.java +++ b/src/test/java/com/github/sylvainjuge/memoryfs/MemoryByteChannelTest.java @@ -52,6 +52,16 @@ public void openCloseChannel() throws IOException { assertThat(c.isOpen()).isFalse(); } + @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) public void readClosed() throws IOException { MemoryByteChannel c = newReadChannel(FileData.newEmpty());