我正在尝试使用映射字节缓冲区读取文件,并在buffer.position(position);行获得OutOfMemoryError:JavaHeapSpace ; ..我不明白代码有什么问题..错误的原因可能是什么?
private void readFile()
{
int lastPos = buffer.getInt();
int dirLen = buffer.getInt();
byte[] dirBytes = new byte[dirLen];
buffer.get(dirBytes);
this.dir = new String(dirBytes);
int filePatternLen = buffer.getInt();
byte[] filePatternBytes = new byte[filePatternLen];
buffer.get(filePatternBytes);
this.filePattern = new String(filePatternBytes);
int numFileMetaDatas = 0;
while (buffer.position() < lastPos)
{
numFileMetaDatas++;
int position = buffer.position();
//Size is needed as we are reserving some extra bytes
int size = buffer.getInt();
buffer.position(position + ByteUtil.SIZE_OF_INT + ByteUtil.SIZE_OF_BYTE + ByteUtil.SIZE_OF_LONG + ByteUtil.SIZE_OF_LONG + ByteUtil.SIZE_OF_LONG);
int fileNameLen = buffer.getInt();
byte[] fileNameBytes = new byte[fileNameLen];
buffer.get(fileNameBytes);
FileMetaData fileMetaData = new FileMetaData(buffer, size, position);
UniqueID uniqID = fileMetaData.getUniqueID();
uniqIdVsFileMetaData.put(uniqID, fileMetaData);
position = position + size;
buffer.position(position);
}
nextMetaStartPos = lastPos;
}