| Code with Finding: |
class MappedRandomAccessFile {
/**
* initializes the channel and mapped bytebuffer
* @param channel FileChannel
* @param mapMode FileChannel.MapMode
* @throws IOException
*/
private void init(FileChannel channel, FileChannel.MapMode mapMode)
throws IOException {
this.channel = channel;
size = channel.size();
pos = 0;
int requiredBuffers = (int)(size/BUFSIZE) + (size % BUFSIZE == 0 ? 0 : 1);
//System.out.println("This will require " + requiredBuffers + " buffers");
mappedBuffers = new MappedByteBuffer[requiredBuffers];
try{
int index = 0;
for(long offset = 0; offset < size; offset += BUFSIZE){
long size2 = Math.min(size - offset, BUFSIZE);
mappedBuffers[index] = channel.map(mapMode, offset, size2);
mappedBuffers[index].load();
index++;
}
if (index != requiredBuffers){
throw new Error("Should never happen - " + index + " != " + requiredBuffers);
}
} catch (IOException e){
close();
throw e;
} catch (RuntimeException e){
close();
throw e;
}
}
}
|