📄 randomaccessfile.java
字号:
package org.garret.perst.impl;
import java.io.IOException;
import javax.microedition.rms.*;
public class RandomAccessFile {
private RecordStore[] store;
private FileDescriptor fd;
private String name;
private boolean readWrite;
private int maxPagesInStore;
private int nPages;
private int currPos;
private int size;
static final int PAGE_SIZE_LOG2 = 12;
static final int PAGE_SIZE = 1 << PAGE_SIZE_LOG2;
public RandomAccessFile(String name, String mode) throws IOException {
try {
this.name = name;
readWrite = mode.equals("rw");
String[] stores = RecordStore.listRecordStores();
int maxStoreId = 0;
int prefixLength = name.length();
if (stores != null) {
for (int i = 0; i < stores.length; i++) {
if (stores[i].startsWith(name)) {
try {
int sid = Integer.parseInt(stores[i].substring(prefixLength));
if (sid > maxStoreId) {
maxStoreId = sid;
}
} catch (NumberFormatException x) {}
}
}
}
store = new RecordStore[maxStoreId+1];
store[0] = RecordStore.openRecordStore(name + '0', readWrite);
nPages = store[0].getNumRecords();
if (maxStoreId > 0) {
maxPagesInStore = nPages;
store[maxStoreId] = RecordStore.openRecordStore(name + Integer.toString(maxStoreId), readWrite);
size = (maxStoreId*maxPagesInStore + store[maxStoreId].getNumRecords())*PAGE_SIZE;
} else {
size = nPages*PAGE_SIZE;
}
} catch (RecordStoreException x) {
throw new IOException(x.getMessage());
}
currPos = 0;
fd = new FileDescriptor();
}
private boolean extend(int sid) throws RecordStoreException {
if (sid >= store.length) {
RecordStore s;
try {
s = RecordStore.openRecordStore(name + Integer.toString(sid), readWrite);
} catch (RecordStoreNotFoundException x) {
return false;
}
RecordStore[] newStore = new RecordStore[sid+1];
System.arraycopy(store, 0, newStore, 0, store.length);
store = newStore;
newStore[sid] = s;
} else if (store[sid] == null) {
store[sid] = RecordStore.openRecordStore(name + Integer.toString(sid), readWrite);
}
nPages = store[sid].getNumRecords();
return true;
}
public int read(byte b[], int off, int len) throws IOException {
int rc = len;
try {
while (len > 0) {
int currPage = currPos >>> PAGE_SIZE_LOG2;
int sid = 0;
if (maxPagesInStore != 0) {
sid = currPage / maxPagesInStore;
currPage %= maxPagesInStore;
if (!extend(sid)) {
break;
}
}
if (currPage >= nPages) {
break;
}
byte[] pageData = store[sid].getRecord(currPage+1);
int pageOffs = currPos & (PAGE_SIZE - 1);
int n = PAGE_SIZE - pageOffs < len ? PAGE_SIZE - pageOffs : len;
System.arraycopy(pageData, pageOffs, b, off, n);
len -= n;
currPos += n;
off += n;
}
} catch (RecordStoreException x) {
throw new IOException(x.getMessage());
}
return rc - len;
}
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
public void write(byte b[]) throws IOException {
write(b, 0, b.length);
}
public void write(byte b[], int off, int len) throws IOException {
try {
while (len > 0) {
int currPage = currPos >>> PAGE_SIZE_LOG2;
int sid = 0;
if (maxPagesInStore != 0) {
sid = currPage / maxPagesInStore;
currPage %= maxPagesInStore;
if (!extend(sid)) {
throw new IOException("Failed to create new storage");
}
}
byte[] pageData;
if (currPage >= nPages) {
pageData = new byte[PAGE_SIZE];
} else if (off != 0 || len != PAGE_SIZE || (currPos & (PAGE_SIZE-1)) != 0) {
pageData = store[sid].getRecord(currPage+1);
} else {
pageData = b;
}
int pageOffs = currPos & (PAGE_SIZE - 1);
int n = PAGE_SIZE - pageOffs < len ? PAGE_SIZE - pageOffs : len;
if (currPage >= nPages) {
try {
while (currPage > nPages) {
store[sid].addRecord(pageData, 0, PAGE_SIZE);
nPages += 1;
}
System.arraycopy(b, off, pageData, pageOffs, n);
store[sid].addRecord(pageData, 0, PAGE_SIZE);
nPages += 1;
} catch (RecordStoreFullException x) {
if (maxPagesInStore != 0) {
throw new IOException("Space is exhausted");
}
maxPagesInStore = nPages;
continue;
}
} else {
if (pageData != b) {
System.arraycopy(b, off, pageData, pageOffs, n);
}
store[sid].setRecord(currPage+1, pageData, 0, PAGE_SIZE);
}
len -= n;
currPos += n;
off += n;
}
if (currPos > size) {
size = (currPos + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
}
} catch (RecordStoreException x) {
System.out.println(x);
throw new IOException(x.getMessage());
}
}
public void seek(long pos) throws IOException {
currPos = (int)pos;
}
public long length() throws IOException {
return size;
}
public long getFilePointer() throws IOException {
return currPos;
}
public void close() throws IOException {
try {
for (int i = 0; i < store.length; i++) {
if (store[i] != null) {
store[i].closeRecordStore();
}
}
} catch (RecordStoreException x) {
throw new IOException(x.getMessage());
}
}
public final FileDescriptor getFD() throws IOException {
return fd;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -