if ( raf == null ){
throw new FMFileManagerException( "read: raf is null" );
}
FileChannel fc = raf.getChannel();
if ( !fc.isOpen()){
Debug.out("FileChannel is closed: " + owner.getName());
throw( new FMFileManagerException( "read - file is closed"));
}
AEThread2.setDebug( owner );
int[] original_positions = new int[buffers.length];
long read_start = SystemTime.getHighPrecisionCounter();
try{
if(USE_MMAP)
{
long size = 0;
for(int i=0;i<buffers.length;i++)
{
size += buffers[i].remaining(DirectByteBuffer.SS_FILE);
original_positions[i] = buffers[i].position(DirectByteBuffer.SS_FILE);
}
size = Math.min(size, fc.size()-offset);
MappedByteBuffer buf = fc.map(MapMode.READ_ONLY, offset, size);
for(DirectByteBuffer b : buffers)
{
buf.limit(buf.position()+b.remaining(DirectByteBuffer.SS_FILE));
b.put(DirectByteBuffer.SS_FILE, buf);
}
} else {
fc.position(offset);
ByteBuffer[] bbs = new ByteBuffer[buffers.length];
ByteBuffer last_bb = null;
for (int i=0;i<bbs.length;i++){
ByteBuffer bb = bbs[i] = buffers[i].getBuffer(DirectByteBuffer.SS_FILE);
int pos = original_positions[i] = bb.position();
if ( pos != bb.limit()){
last_bb = bbs[i];
}
}
if ( last_bb != null ){
int loop = 0;
// we sometimes read off the end of the file (when rechecking) so
// bail out if we've completed the read or got to file end
// a "better" fix would be to prevent the over-read in the first
// place, but hey, we're just about to release and there may be other
// instances of this...
while ( fc.position() < fc.size() && last_bb.hasRemaining()){
long read = fc.read( bbs );
if ( read > 0 ){
loop = 0;
}else{
loop++;
if ( loop == READ_RETRY_LIMIT ){