public abstract class MappedBytes extends AbstractBytes<Void> implements net.openhft.chronicle.core.io.Closeable, net.openhft.chronicle.core.io.ManagedCloseable, net.openhft.chronicle.core.io.Syncable
Bytes implementation backed by a memory-mapped file.
The underlying file is accessed in chunks (64 MiB by default) so very
large files can be treated as if they were in memory. Only the most recently
accessed chunk is retained; previous chunks are released. Call
ReferenceCounted.releaseLast() when finished with a MappedBytes instance to
free system resources.
Instances are single threaded and should be reserved before use.
| Modifier and Type | Field and Description |
|---|---|
protected static boolean |
TRACE
System property flag
trace.mapped.bytes for enabling trace
logging of MappedBytes operations. |
bytesStore, isPresent, readPosition, writeLimitreferenceCounted, WARN_COUNT, WARN_NSDEFAULT_BYTE_BUFFER_CAPACITY, MAX_CAPACITY, MAX_HEAP_CAPACITYJAVA9_STRING_CODER_LATIN, JAVA9_STRING_CODER_UTF16| Modifier | Constructor and Description |
|---|---|
protected |
MappedBytes()
Constructs an instance for use by subclasses.
|
protected |
MappedBytes(String name)
Constructs an instance for use by subclasses with a descriptive name.
|
| Modifier and Type | Method and Description |
|---|---|
@NotNull Bytes<Void> |
bytesForRead()
Returns a view for reading from the current mapping.
|
@NotNull Bytes<Void> |
bytesForWrite()
Returns a view for writing to the current mapping.
|
abstract void |
chunkCount(long[] chunkCount)
Populates the supplied array with the number of chunks held by the underlying
MappedFile. |
abstract boolean |
isBackingFileReadOnly() |
static @NotNull MappedBytes |
mappedBytes(@NotNull File file,
long chunkSize)
Creates a MappedBytes instance that wraps a memory-mapped file divided into chunks of a specified size.
|
static @NotNull MappedBytes |
mappedBytes(@NotNull File file,
long chunkSize,
long overlapSize)
Creates a chunked mapping with a specified overlap between chunks.
|
static @NotNull MappedBytes |
mappedBytes(@NotNull File file,
long chunkSize,
long overlapSize,
boolean readOnly)
Convenience overload using the default page size.
|
static @NotNull MappedBytes |
mappedBytes(@NotNull File file,
long chunkSize,
long overlapSize,
int pageSize,
boolean readOnly)
Creates a chunked mapping with explicit overlap, page size and read-only option.
|
static @NotNull MappedBytes |
mappedBytes(@NotNull MappedFile rw)
Creates a
MappedBytes view for a pre-existing MappedFile. |
static @NotNull MappedBytes |
mappedBytes(@NotNull String filename,
long chunkSize)
Creates a chunked mapping for the given file.
|
abstract MappedFile |
mappedFile() |
static @NotNull MappedBytes |
readOnly(@NotNull File file)
Maps the given file in read-only mode.
|
boolean |
sharedMemory()
Memory-mapped files are a form of shared memory, hence this returns
true. |
static @NotNull MappedBytes |
singleMappedBytes(@NotNull File file,
long capacity)
As
singleMappedBytes(String, long) but accepting a File instance. |
static @NotNull MappedBytes |
singleMappedBytes(@NotNull File file,
long capacity,
boolean readOnly)
Creates a single mapping for the whole file with explicit read-only option.
|
static @NotNull MappedBytes |
singleMappedBytes(@NotNull String filename,
long capacity)
Creates a
MappedBytes covering the entire file as a single mapping. |
void |
sync()
Forces any changes made to this mapping to be written to the storage device.
|
acquireUncheckedInput, addressForRead, addressForWrite, addressForWritePosition, append, append, append, append, append, append, appendAndReturnLength, byteCheckSum, byteCheckSum, byteCheckSum, bytesStore, bytesStore, canReadDirect, canWriteDirect, capacity, clear, clearAndPad, compact, compareAndSwapInt, compareAndSwapLong, copyTo, decimaliser, decimaliser, equals, fpAppend0, fpAppend0, hashCode, isDirectMemory, isElastic, isImmutableEmptyByteStore, lastDecimalPlaces, lastDecimalPlaces, lastNumberHadDigits, lastNumberHadDigits, length, lenient, lenient, move, nativeRead, nativeWrite, peekUnsignedByte, peekUnsignedByte, performRelease, prewrite, prewrite, prewriteByte, prewriteInt, prewriteLong, prewriteOffsetPositionMoved, prewriteShort, read, read, readByte, readByte, readCheckOffset, readDouble, readDouble, readFloat, readFloat, readInt, readInt, readLimit, readLimit, readLong, readLong, readOffsetPositionMoved, readPosition, readPosition, readPositionForHeader, readShort, readShort, readSkip, readUnsignedByte, readUnsignedByte, readVolatileByte, readVolatileInt, readVolatileInt, readVolatileLong, readVolatileLong, readVolatileShort, realCapacity, safeCopySize, start, startsWith, testAndSetInt, toString, uncheckedReadSkipBackOne, uncheckedReadSkipOne, uncheckedReadUnsignedByte, uncheckedWritePosition, underlyingObject, unmonitor, write, write, write, write, write, write8bit, write8bit, write8bit, write8bit, writeBoolean, writeByte, writeByte, writeCheckOffset, writeDouble, writeDouble, writeDoubleAndInt, writeFloat, writeFloat, writeInt, writeInt, writeIntAdv, writeLimit, writeLimit, writeLong, writeLong, writeLongAdv, writeOffsetPositionMoved, writeOffsetPositionMoved, writeOrderedInt, writeOrderedInt, writeOrderedLong, writeOrderedLong, writePosition, writePosition, writeShort, writeShort, writeSkip, writeSome, writeVolatileByte, writeVolatileInt, writeVolatileLong, writeVolatileShortaddReferenceChangeListener, assertReferencesReleased, backgroundPerformRelease, canReleaseInBackground, createdHere, disableReferenceTracing, enableReferenceTracing, refCount, referenceCountedUnmonitored, referenceId, release, releaseLast, removeReferenceChangeListener, reserve, reserveTransfer, singleThreadedCheckDisabled, singleThreadedCheckReset, threadSafetyCheck, throwExceptionIfNotReleased, throwExceptionIfReleased, tryReserve, unmonitor, warnAndReleaseIfNotReleasedclone, finalize, getClass, notify, notifyAll, wait, wait, waitcreatedHere, throwExceptionIfClosed, warnAndCloseIfNotClosedclose, closeQuietly, closeQuietlyallocateDirect, allocateDirect, allocateElastic, allocateElastic, allocateElasticDirect, allocateElasticDirect, allocateElasticOnHeap, allocateElasticOnHeap, copy, copyTo, copyTo, directFrom, elasticByteBuffer, elasticByteBuffer, elasticByteBuffer, elasticHeapByteBuffer, elasticHeapByteBuffer, empty, ensureCapacity, forFieldGroup, from, from, fromDirect, fromHexString, indexOf, indexOf, isClear, isEqual, readMarshallableLength16, readWithLength, readWrite, safeLimit, toHexString, toHexString, toHexString, toString, toString, toString, unchecked, unchecked, unwrite, valueOf, wrapForRead, wrapForRead, wrapForWrite, wrapForWrite, write, writeMarshallableLength16addAndGetDouble, addAndGetDoubleNotAtomic, addAndGetFloat, addAndGetFloatNotAtomic, addAndGetInt, addAndGetIntNotAtomic, addAndGetLong, addAndGetShortNotAtomic, addAndGetUnsignedByteNotAtomic, charAt, cipher, cipher, compareAndSwapDouble, compareAndSwapFloat, contentEquals, elasticByteBuffer, endsWith, equalBytes, follow, forFields, from, hash, inside, inside, isEmpty, isEqual, lazyNativeBytesStoreWithFixedCapacity, nativePointer, nativeStore, nativeStoreFrom, nativeStoreWithFixedCapacity, startsWith, subSequence, to8bitString, toDebugString, toDebugString, toUtf8String, wrap, wrap, wrap, writeMaxInt, writeMaxLong, zeroOutappend, append, write, write, writeBoolean, writeByte, writeInt24, writeOrderedDouble, writeOrderedFloat, writeUnsignedByte, writeUnsignedInt, writeUnsignedShort, writeUtf8, writeUtf8Limited, writeVolatileDouble, writeVolatileFloatchars, codePointsbytesMethodReader, bytesMethodReaderBuilder, readObjectcanReadDirect, compareUtf8, copyTo, fastHash, findByte, parseLong, peekVolatileInt, printable, readBoolean, readIncompleteLong, readUnsignedInt, readUnsignedInt24, readUnsignedShort, readUtf8, readUtf8Limited, readUtf8Limited, readVolatileDouble, readVolatileFloat, subBytes, toByteArray, toTemporaryDirectByteBufferparse8bit, parse8bit, parse8bit, parse8bit, parse8bit, parseBigDecimal, parseBoolean, parseBoolean, parseDouble, parseFlexibleLong, parseFloat, parseInt, parseLong, parseLongDecimal, parseUtf8, parseUtf8, parseUtf8, reader, skipToinputStream, parseHexLong, parseUtf8, parseUtf8, rawReadByte, rawReadInt, rawReadLong, read, read, read, read, read, read8bit, read8bit, read8bit, readBigDecimal, readBigInteger, readBoolean, readChar, readEnum, readHistogram, readIncompleteLong, readInt24, readLimitToCapacity, readPositionRemaining, readPositionUnlimited, readStopBit, readStopBitChar, readStopBitDecimal, readStopBitDouble, readUnsignedInt, readUnsignedInt24, readUnsignedShort, readUtf8, readUtf8, readUtf8, readUtf8, readWithLength, readWithLength, readWithLength0, unsafeRead, unsafeReadObjectbytesMethodWriter, writeObjectappend, append, append, append, append, append8bit, append8bit, append8bit, append8bit, append8bit, appendBase, appendBase16, appendBase16, appendDateMillis, appendDecimal, appendTimeMillis, writerappendUtf8, appendUtf8, appendUtf8, appendUtf8, appendUtf8, appendUtf8, copyFrom, outputStream, rawWriteByte, rawWriteInt, rawWriteLong, unsafeWrite, unsafeWriteObject, write, write, write, write, write8bit, write8bit, write8bit, writeBigDecimal, writeBigInteger, writeChar, writeEnum, writeHistogram, writeInt24, writePositionForHeader, writePositionRemaining, writeStopBit, writeStopBit, writeStopBit, writeStopBitDecimal, writeUnsignedByte, writeUnsignedInt, writeUnsignedInt24, writeUnsignedShort, writeUtf8, writeUtf8, writeWithLength, writeWithLengthprependadjustHexDumpIndentation, retainedHexDumpDescription, writeHexDumpDescriptionprotected static final boolean TRACE
trace.mapped.bytes for enabling trace
logging of MappedBytes operations.protected MappedBytes()
throws net.openhft.chronicle.core.io.ClosedIllegalStateException,
net.openhft.chronicle.core.io.ThreadingIllegalStateException
BytesStore until a real mapping is assigned.net.openhft.chronicle.core.io.ClosedIllegalStateExceptionnet.openhft.chronicle.core.io.ThreadingIllegalStateExceptionprotected MappedBytes(String name) throws net.openhft.chronicle.core.io.ClosedIllegalStateException, net.openhft.chronicle.core.io.ThreadingIllegalStateException
net.openhft.chronicle.core.io.ClosedIllegalStateExceptionnet.openhft.chronicle.core.io.ThreadingIllegalStateException@NotNull public static @NotNull MappedBytes singleMappedBytes(@NotNull @NotNull String filename, long capacity) throws FileNotFoundException, IllegalStateException
MappedBytes covering the entire file as a single mapping.filename - path of the file to mapcapacity - total capacity to mapMappedBytesFileNotFoundException - if the file cannot be foundnet.openhft.chronicle.core.io.ClosedIllegalStateException - if the mapped file is already closednet.openhft.chronicle.core.io.ThreadingIllegalStateException - if accessed from multiple threadsIllegalStateException@NotNull public static @NotNull MappedBytes singleMappedBytes(@NotNull @NotNull File file, long capacity) throws FileNotFoundException
singleMappedBytes(String, long) but accepting a File instance.FileNotFoundException@NotNull public static @NotNull MappedBytes singleMappedBytes(@NotNull @NotNull File file, long capacity, boolean readOnly) throws FileNotFoundException
file - file to mapcapacity - total capacityreadOnly - true for read onlyMappedBytesFileNotFoundException - if the file does not exist@NotNull public static @NotNull MappedBytes mappedBytes(@NotNull @NotNull String filename, long chunkSize) throws FileNotFoundException, net.openhft.chronicle.core.io.ClosedIllegalStateException
filename - path of the file to mapchunkSize - size of each chunk in bytesMappedBytesFileNotFoundException - if the file does not existnet.openhft.chronicle.core.io.ClosedIllegalStateException - if the mapped file is closednet.openhft.chronicle.core.io.ThreadingIllegalStateException - if accessed from multiple threads@NotNull public static @NotNull MappedBytes mappedBytes(@NotNull @NotNull File file, long chunkSize) throws FileNotFoundException, net.openhft.chronicle.core.io.ClosedIllegalStateException
file - The name of the file to be memory-mapped.chunkSize - The size of each chunk in bytes.FileNotFoundException - If the file does not exist.net.openhft.chronicle.core.io.ClosedIllegalStateException - If the resource has been released or closed.net.openhft.chronicle.core.io.ThreadingIllegalStateException - If this resource was accessed by multiple threads in an unsafe way@NotNull public static @NotNull MappedBytes mappedBytes(@NotNull @NotNull File file, long chunkSize, long overlapSize) throws FileNotFoundException, net.openhft.chronicle.core.io.ClosedIllegalStateException
file - file to mapchunkSize - size of each chunkoverlapSize - overlap between adjacent chunksMappedBytesFileNotFoundException - if the file does not existnet.openhft.chronicle.core.io.ClosedIllegalStateException - if the mapped file is closednet.openhft.chronicle.core.io.ThreadingIllegalStateException - if accessed from multiple threads@NotNull public static @NotNull MappedBytes mappedBytes(@NotNull @NotNull File file, long chunkSize, long overlapSize, int pageSize, boolean readOnly) throws FileNotFoundException, net.openhft.chronicle.core.io.ClosedIllegalStateException
file - file to mapchunkSize - size of each chunkoverlapSize - overlap between adjacent chunkspageSize - page size to align mappingsreadOnly - true for read onlyMappedBytesFileNotFoundException - if the file does not existnet.openhft.chronicle.core.io.ClosedIllegalStateException - if the mapped file is closednet.openhft.chronicle.core.io.ThreadingIllegalStateException - if accessed from multiple threads@NotNull public static @NotNull MappedBytes mappedBytes(@NotNull @NotNull File file, long chunkSize, long overlapSize, boolean readOnly) throws FileNotFoundException, net.openhft.chronicle.core.io.ClosedIllegalStateException
FileNotFoundExceptionnet.openhft.chronicle.core.io.ClosedIllegalStateExceptionmappedBytes(File, long, long, int, boolean)@NotNull public static @NotNull MappedBytes mappedBytes(@NotNull @NotNull MappedFile rw) throws net.openhft.chronicle.core.io.ClosedIllegalStateException
MappedBytes view for a pre-existing MappedFile.rw - the mapped file to useMappedBytesnet.openhft.chronicle.core.io.ClosedIllegalStateException - if the mapped file has been closednet.openhft.chronicle.core.io.ThreadingIllegalStateException - if accessed from multiple threads@NotNull public static @NotNull MappedBytes readOnly(@NotNull @NotNull File file) throws FileNotFoundException, net.openhft.chronicle.core.io.ClosedIllegalStateException, net.openhft.chronicle.core.io.ThreadingIllegalStateException
file - file to mapMappedBytesFileNotFoundException - if the file does not existnet.openhft.chronicle.core.io.ClosedIllegalStateException - if the mapped file is closednet.openhft.chronicle.core.io.ThreadingIllegalStateException - if accessed from multiple threadspublic abstract boolean isBackingFileReadOnly()
true if the underlying file was mapped read onlypublic boolean sharedMemory()
true.sharedMemory in interface Bytes<Void>sharedMemory in interface RandomCommontrue if this Bytes object uses memory that can be shared across processes,
false otherwise.public abstract void chunkCount(long[] chunkCount)
MappedFile.public abstract MappedFile mappedFile()
MappedFilepublic void sync()
sync in interface net.openhft.chronicle.core.io.Syncable@NotNull public @NotNull Bytes<Void> bytesForRead() throws net.openhft.chronicle.core.io.ClosedIllegalStateException
bytesForRead in interface Bytes<Void>bytesForRead in interface BytesStore<Bytes<Void>,Void>bytesForRead in interface RandomCommonnet.openhft.chronicle.core.io.ClosedIllegalStateException - If the resource has been released or closed.@NotNull public @NotNull Bytes<Void> bytesForWrite() throws net.openhft.chronicle.core.io.ClosedIllegalStateException
bytesForWrite in interface Bytes<Void>bytesForWrite in interface BytesStore<Bytes<Void>,Void>bytesForWrite in interface RandomCommonnet.openhft.chronicle.core.io.ClosedIllegalStateException - If the resource has been released or closed.Copyright © 2026 Chronicle Software Ltd. All rights reserved.