diff --git a/src/main/java/org/sejda/io/BufferedCountingChannelWriter.java b/src/main/java/org/sejda/io/BufferedCountingChannelWriter.java
index <HASH>..<HASH> 100644
--- a/src/main/java/org/sejda/io/BufferedCountingChannelWriter.java
+++ b/src/main/java/org/sejda/io/BufferedCountingChannelWriter.java
@@ -50,7 +50,7 @@ public class BufferedCountingChannelWriter implements Closeable {
 
     @Override
     public void close() throws IOException {
-        if (buffer.position() != 0) {
+        if (channel.isOpen() && buffer.position() != 0) {
             flush();
         }
         IOUtils.close(channel);
Safety check to make sure we don't flush on a closed channel