Skip to content

Commit

Permalink
Fixed acquireNextByteStore0 when shifting backwards, closes #452
Browse files Browse the repository at this point in the history
  • Loading branch information
glukos authored and JerryShea committed Oct 7, 2022
1 parent 442e7c8 commit 49a8b78
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Expand Up @@ -269,7 +269,11 @@ protected void writeCheckOffset(final long offset, final long adding)
throw writeBufferOverflowException0(offset);
BytesStore bytesStore = this.bytesStore;
if (adding > 0 && !bytesStore.inside(offset, checkSize0(adding - 1))) {
acquireNextByteStore0(offset + adding - 1, false);
if (bytesStore.start() > offset)
acquireNextByteStore0(offset, false);
else
acquireNextByteStore0(offset + adding - 1, false);

if (!this.bytesStore.inside(offset, checkSize0(adding - 1)))
throw new DecoratedBufferUnderflowException(String.format("Acquired the next BytesStore, but still not room to add %d when realCapacity %d", adding, this.bytesStore.realCapacity()));
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/net/openhft/chronicle/bytes/MappedBytesTest.java
Expand Up @@ -19,6 +19,7 @@
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

import static org.junit.Assert.*;
Expand Down Expand Up @@ -75,6 +76,28 @@ public void testMappedFileSafeLimitTooSmall()
}
}

@Test
public void testAcquireNextByteStoreShiftingBackwards() throws IOException {
final long chunkSize = OS.mapAlign(40_000);

File tempFile1 = File.createTempFile("mapped", "bytes");
try (MappedBytes bytesW = MappedBytes.mappedBytes(tempFile1, chunkSize, chunkSize)) {

for (int i = 0; i < chunkSize / 4; i++)
bytesW.writeLong(ThreadLocalRandom.current().nextLong());

Assert.assertEquals(chunkSize * 2, bytesW.writePosition());

bytesW.writeInt(7);
Assert.assertEquals(chunkSize * 2 + 4, bytesW.writePosition());

bytesW.writeInt(chunkSize * 2 - 2, 9);

bytesW.readPosition(chunkSize * 2 - 2);
Assert.assertEquals(9, bytesW.readInt());
}
}

@Test
public void testMappedFileSafeLimitTooSmall2()
throws IOException {
Expand Down

0 comments on commit 49a8b78

Please sign in to comment.