diff --git a/logwriting/src/main/java/com/redhat/mashona/logwriting/MappedFileChannel.java b/logwriting/src/main/java/com/redhat/mashona/logwriting/MappedFileChannel.java index 560cbe8..36fe603 100644 --- a/logwriting/src/main/java/com/redhat/mashona/logwriting/MappedFileChannel.java +++ b/logwriting/src/main/java/com/redhat/mashona/logwriting/MappedFileChannel.java @@ -25,7 +25,7 @@ import java.nio.channels.*; import java.nio.file.Files; import java.nio.file.StandardOpenOption; -import java.util.EnumSet; +import java.util.*; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -310,6 +310,10 @@ public int write(ByteBuffer src, long position) throws IOException { private int writeInternal(ByteBuffer src, int position) throws ClosedChannelException { + if(metadata.isReadShared()) { + throw new IllegalStateException(); + } + if (position < metadata.getPersistenceIndex()) { throw new IllegalArgumentException(); } @@ -324,9 +328,7 @@ private int writeInternal(ByteBuffer src, int position) throws ClosedChannelExce dst.put(srcSlice); src.position(src.position() + length); - int startIndex = position; - - persist(startIndex, length); + persist(position, length); return length; } @@ -531,12 +533,16 @@ protected void implCloseChannel() throws IOException { // https://bugs.openjdk.java.net/browse/JDK-4724038 unsafe.invokeCleaner(rawBuffer); - fileChannel.truncate(metadata.getPersistenceIndex()); - - // TODO close metadata (assuming it's private instance) + if(!metadata.isReadShared()) { + int persistenceIndex = metadata.getPersistenceIndex(); + logger.trace("truncating {} to {}", file.getAbsolutePath(), persistenceIndex); + fileChannel.truncate(persistenceIndex); + } fileChannel.close(); + metadata.close(); + } finally { lock.unlock(); } diff --git a/logwriting/src/main/java/com/redhat/mashona/logwriting/MappedFileChannelMetadata.java b/logwriting/src/main/java/com/redhat/mashona/logwriting/MappedFileChannelMetadata.java index 80ce3f6..5b55511 100644 --- a/logwriting/src/main/java/com/redhat/mashona/logwriting/MappedFileChannelMetadata.java +++ b/logwriting/src/main/java/com/redhat/mashona/logwriting/MappedFileChannelMetadata.java @@ -190,6 +190,15 @@ public void persist(int startIndex, int length) throws ClosedChannelException { logger.exit(); } + /** + * Returns the read sharing mode. + * + * @return true is read sharing is enabled, false otherwise. + */ + public boolean isReadShared() { + return readShared; + } + /** * Reinitializes the instance. *