I'm trying to read a file using mapped byte buffer and am getting an OutOfMemoryError:JavaHeapSpace at the line buffer.position(position);.. I don't understand what's wrong in the code.. What could be the reason for the error?
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;
}