diff --git a/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java b/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java index 56546c78b..49f0a861b 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java @@ -376,15 +376,20 @@ public abstract class VanillaChronicleHash 0) { + if (ch.read(globalMutableStateBuffer, + this.headerSize + GLOBAL_MUTABLE_STATE_VALUE_OFFSET + + globalMutableStateBuffer.position()) == -1) { + throw new RuntimeException(); + } + } globalMutableStateBuffer.flip(); globalMutableState.bytesStore(BytesStore.wrap(globalMutableStateBuffer), 0, globalMutableState.maxSize()); @@ -814,7 +819,7 @@ public abstract class VanillaChronicleHash implements try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) { FileChannel fileChannel = raf.getChannel(); ByteBuffer headerBuffer = ByteBuffer.allocate(MAX_BOOTSTRAPPING_HEADER_SIZE); - fileChannel.read(headerBuffer); + while (headerBuffer.remaining() > 0 && + headerBuffer.position() < fileChannel.size()) { + if (fileChannel.read(headerBuffer, headerBuffer.position()) == -1) + break; + } headerBuffer.flip(); Bytes headerBytes = Bytes.wrapForRead(headerBuffer); headerBytes.readLimit(headerBuffer.limit()); @@ -1384,8 +1389,7 @@ public final class ChronicleMapBuilder implements map.initTransientsFromBuilder(this); initTransientsFromReplication(map, singleHashReplication, channel); - fileChannel.position(headerBytes.readPosition()); - map.initBeforeMapping(fileChannel); + map.initBeforeMapping(fileChannel, headerBytes.readPosition()); long expectedFileLength = map.expectedFileSize(); if (expectedFileLength != fileLength) { throw new IOException("The file " + file + " the map is serialized from " + @@ -1422,9 +1426,14 @@ public final class ChronicleMapBuilder implements Wire wire = new TextWire(headerBytes); wire.getValueOut().typedMarshallable(map); - headerBuffer.limit((int) headerBytes.writePosition()); - fileChannel.write(headerBuffer); - map.initBeforeMapping(fileChannel); + int headerSize = (int) headerBytes.writePosition(); + headerBuffer.limit(headerSize); + try (FileLock ignored = fileChannel.lock(0, headerSize, false)) { + while (headerBuffer.remaining() > 0) { + fileChannel.write(headerBuffer, headerBuffer.position()); + } + map.initBeforeMapping(fileChannel, headerSize); + } map.createMappedStoreAndSegments(file, raf); }