📄 memorymappedfile.java
字号:
* @throws MemoryNotMappedException
* If the file not mapped into memory
* @throws MemoryReadException
* If some exception occurs when reading the memory.
*/
public int readInt(int pos) throws MemoryNotMappedException,
MemoryReadException {
if (mapBuf == null)
throw new MemoryNotMappedException(
"the file not mapped into memory.");
if (pos < 0)
throw new MemoryReadException("the memory map is underflow.");
if (pos > this.capacity - 4)
throw new MemoryReadException("the memory map is overflow.");
readLock.lock();
try {
mapBuf.position(pos);
return mapBuf.getInt();
} finally {
readLock.unlock();
}
}
/**
* 在内存映象的指定位置读取一个长整数
*
* @param pos
* The dest position of the memory map.
* @return The long nubmer if successed.
* @throws MemoryNotMappedException
* If the file not mapped into memory
* @throws MemoryReadException
* If some exception occurs when reading the memory.
*/
public long readLong(int pos) throws MemoryNotMappedException,
MemoryReadException {
if (mapBuf == null)
throw new MemoryNotMappedException(
"the file not mapped into memory.");
if (pos < 0)
throw new MemoryReadException("the memory map is underflow.");
if (pos > this.capacity - 8)
throw new MemoryReadException("the memory map is overflow.");
readLock.lock();
try {
mapBuf.position(pos);
return mapBuf.getLong();
} finally {
readLock.unlock();
}
}
/**
* 在内存映象的指定位置读取指定长度的字节数据
*
* @param pos
* The dest position of the memory map.
* @param len
* The length of data.
* @return The long nubmer if successed.
* @throws MemoryNotMappedException
* If the file not mapped into memory
* @throws MemoryReadException
* If some exception occurs when reading the memory.
*/
public byte[] readBytes(int pos, int len) throws MemoryNotMappedException,
MemoryReadException {
if (mapBuf == null)
throw new MemoryNotMappedException(
"the file not mapped into memory.");
if (pos < 0)
throw new MemoryReadException("the memory map is underflow.");
if (pos > this.capacity - len)
throw new MemoryReadException("the memory map is overflow.");
readLock.lock();
try {
mapBuf.position(pos);
byte[] dst = new byte[len];
mapBuf.get(dst);
return dst;
} finally {
readLock.unlock();
}
}
/**
* 在内存映象的指定位置重置一个字节
*
* @param pos
* The dest position of the memory map.
* @param value
* The byte value of new data.
* @return The old value at the given position.
* @throws MemoryNotMappedException
* If the file not mapped into memory
* @throws MemoryReadException
* If some exception occurs when reading the memory.
*/
public byte readAndWrite(int pos, byte value) {
if (mapBuf == null)
throw new MemoryNotMappedException(
"the file not mapped into memory.");
if (pos < 0)
throw new MemoryReadException("the memory map is underflow.");
if (pos > this.capacity - 1)
throw new MemoryReadException("the memory map is overflow.");
writeLock.lock();
try {
mapBuf.position(pos);
byte ret = mapBuf.get();
mapBuf.position(pos);
mapBuf.putInt(value);
// mapBuf.force();
return ret;
} finally {
writeLock.unlock();
}
}
/**
* 在内存映象的指定位置重置一个整数
*
* @param pos
* The dest position of the memory map.
* @param value
* The integer value of new data.
* @return The old value at the given position.
* @throws MemoryNotMappedException
* If the file not mapped into memory
* @throws MemoryReadException
* If some exception occurs when reading the memory.
*/
public int readAndWrite(int pos, int value) {
if (mapBuf == null)
throw new MemoryNotMappedException(
"the file not mapped into memory.");
if (pos < 0)
throw new MemoryReadException("the memory map is underflow.");
if (pos > this.capacity - 4)
throw new MemoryReadException("the memory map is overflow.");
writeLock.lock();
try {
mapBuf.position(pos);
int ret = mapBuf.getInt();
mapBuf.position(pos);
mapBuf.putInt(value);
// mapBuf.force();
return ret;
} finally {
writeLock.unlock();
}
}
/**
* 在内存映象的指定位置重置一块的值
*
* @param pos
* The dest position of the memory map.
* @param value
* The bytes array value of new data.
* @return The old bytes array at the given position.
* @throws MemoryNotMappedException
* If the file not mapped into memory
* @throws MemoryReadException
* If some exception occurs when reading the memory.
*/
public byte[] readAndWrite(int pos, byte[] value) {
if (mapBuf == null)
throw new MemoryNotMappedException(
"the file not mapped into memory.");
if (pos < 0)
throw new MemoryReadException("the memory map is underflow.");
if (pos > this.capacity - value.length)
throw new MemoryReadException("the memory map is overflow.");
writeLock.lock();
try {
mapBuf.position(pos);
byte[] ret = new byte[value.length];
mapBuf.get(ret);
mapBuf.position(pos);
mapBuf.put(value);
// mapBuf.force();
return ret;
} finally {
writeLock.unlock();
}
}
/**
* 在内存映象的指定位置整数相加
*
* @param pos
* The dest position of the memory map.
* @param delta
* The integer value of added data.
* @return The old integer value at the given position.
* @throws MemoryNotMappedException
* If the file not mapped into memory
* @throws MemoryReadException
* If some exception occurs when reading the memory.
*/
public int readAndAdd(int pos, int delta) {
if (mapBuf == null)
throw new MemoryNotMappedException(
"the file not mapped into memory.");
if (pos < 0)
throw new MemoryReadException("the memory map is underflow.");
if (pos > this.capacity - 4)
throw new MemoryReadException("the memory map is overflow.");
writeLock.lock();
try {
mapBuf.position(pos);
int ret = mapBuf.getInt();
mapBuf.position(pos);
mapBuf.putInt(ret + delta);
// mapBuf.force();
return ret;
} finally {
writeLock.unlock();
}
}
/**
* 在内存映象的指定位置整数相加
*
* @param pos
* The dest position of the memory map.
* @param delta
* The integer value of added data.
* @return The new integer value at the given position.
* @throws MemoryNotMappedException
* If the file not mapped into memory
* @throws MemoryReadException
* If some exception occurs when reading the memory.
*/
public int addAndRead(int pos, int delta) {
if (mapBuf == null)
throw new MemoryNotMappedException(
"the file not mapped into memory.");
if (pos < 0)
throw new MemoryReadException("the memory map is underflow.");
if (pos > this.capacity - 4)
throw new MemoryReadException("the memory map is overflow.");
writeLock.lock();
try {
mapBuf.position(pos);
int ret = mapBuf.getInt();
mapBuf.position(pos);
ret += delta;
mapBuf.putInt(ret);
// mapBuf.force();
return ret;
} finally {
writeLock.unlock();
}
}
/**
* 在内存映象的指定位置整数加一
*
* @param pos
* The dest position of the memory map.
* @return The old integer value at the given position.
* @throws MemoryNotMappedException
* If the file not mapped into memory
* @throws MemoryReadException
* If some exception occurs when reading the memory.
*/
public int readAndIncrement(int pos) {
if (mapBuf == null)
throw new MemoryNotMappedException(
"the file not mapped into memory.");
if (pos < 0)
throw new MemoryReadException("the memory map is underflow.");
if (pos > this.capacity - 4)
throw new MemoryReadException("the memory map is overflow.");
writeLock.lock();
try {
mapBuf.position(pos);
int ret = mapBuf.getInt();
mapBuf.position(pos);
mapBuf.putInt(ret + 1);
// mapBuf.force();
return ret;
} finally {
writeLock.unlock();
}
}
/**
* 在内存映象的指定位置整数加一
*
* @param pos
* The dest position of the memory map.
* @return The new integer value at the given position.
* @throws MemoryNotMappedException
* If the file not mapped into memory
* @throws MemoryReadException
* If some exception occurs when reading the memory.
*/
public int incrementAndRead(int pos) {
if (mapBuf == null)
throw new MemoryNotMappedException(
"the file not mapped into memory.");
if (pos < 0)
throw new MemoryReadException("the memory map is underflow.");
if (pos > this.capacity - 4)
throw new MemoryReadException("the memory map is overflow.");
writeLock.lock();
try {
mapBuf.position(pos);
int ret = mapBuf.getInt();
mapBuf.position(pos);
mapBuf.putInt(++ret);
// mapBuf.force();
return ret;
} finally {
writeLock.unlock();
}
}
/**
* 在内存映象的指定位置整数减一
*
* @param pos
* The dest position of the memory map.
* @return The old integer value at the given position.
* @throws MemoryNotMappedException
* If the file not mapped into memory
* @throws MemoryReadException
* If some exception occurs when reading the memory.
*/
public int readAndDecrement(int pos) {
if (mapBuf == null)
throw new MemoryNotMappedException(
"the file not mapped into memory.");
if (pos < 0)
throw new MemoryReadException("the memory map is underflow.");
if (pos > this.capacity - 4)
throw new MemoryReadException("the memory map is overflow.");
writeLock.lock();
try {
mapBuf.position(pos);
int ret = mapBuf.getInt();
mapBuf.position(pos);
mapBuf.putInt(ret - 1);
// mapBuf.force();
return ret;
} finally {
writeLock.unlock();
}
}
/**
* 在内存映象的指定位置整数减一
*
* @param pos
* The dest position of the memory map.
* @return The new integer value at the given position.
* @throws MemoryNotMappedException
* If the file not mapped into memory
* @throws MemoryReadException
* If some exception occurs when reading the memory.
*/
public int decrementAndRead(int pos) {
if (mapBuf == null)
throw new MemoryNotMappedException(
"the file not mapped into memory.");
if (pos < 0)
throw new MemoryReadException("the memory map is underflow.");
if (pos > this.capacity - 4)
throw new MemoryReadException("the memory map is overflow.");
writeLock.lock();
try {
mapBuf.position(pos);
int ret = mapBuf.getInt();
mapBuf.position(pos);
mapBuf.putInt(--ret);
// mapBuf.force();
return ret;
} finally {
writeLock.unlock();
}
}
public boolean isLoaded() {
if (mapBuf == null) {
return false;
}
return mapBuf.isLoaded();
}
public MappedByteBuffer force() {
return this.force();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -