MappedAtomicInteger { public int get() { FileLock lock = lock(); try { return getUnsafe(); } finally { FileLocks.release(lock); } } MappedAtomicInteger(FileChannel channel);  MappedAtomicInteger(FileChannel channel, int position);  MappedAtomicInteger(String address);  MappedAtomicInteger(String address, int position); int addAndGet(int amount); @Override void finalize(); int get(); void set(int value); void setAndSync(int value); void sync();  }
@Test public void testGetBeginning() { try { String file = FileOps.tempFile(); int value = Random.getInt(); FileChannel channel = FileChannel.open(new File(file).toPath(), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE); channel.write(getByteBuffer(value)); for (int i = 0; i < Random.getScaleCount(); ++i) { channel.write(getByteBuffer(Random.getInt())); } MappedAtomicInteger integer = new MappedAtomicInteger(channel); for (int i = 0; i < Random.getScaleCount(); ++i) { Assert.assertEquals(value, integer.get()); } } catch (IOException e) { throw CheckedExceptions.wrapAsRuntimeException(e); } }
@Test public void testGetRandomPosition() { try { String file = FileOps.tempFile(); int value = Random.getInt(); FileChannel channel = FileChannel.open(new File(file).toPath(), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE); int before = Random.getScaleCount(); int after = Random.getScaleCount(); for (int i = 0; i < before; ++i) { channel.write(getByteBuffer(Random.getInt())); } channel.write(getByteBuffer(value)); for (int i = 0; i < after; ++i) { channel.write(getByteBuffer(Random.getInt())); } int position = 4 * before; MappedAtomicInteger integer = new MappedAtomicInteger(file, position); for (int i = 0; i < Random.getScaleCount(); ++i) { Assert.assertEquals(value, integer.get()); } } catch (IOException e) { throw CheckedExceptions.wrapAsRuntimeException(e); } }