?? 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 + -