{
if ( logging )
log("R(0x%X)", loc) ;
if ( inAllocWrite )
throw new FileException("In the middle of an alloc-write") ;
if ( loc < 0 )
throw new IllegalArgumentException("ObjectFile.read["+file.getLabel()+"]: Bad read: "+loc) ;
// Maybe it's in the in the write buffer.
// Maybe the write buffer should keep more structure?
if ( loc >= filesize )
{
if ( loc >= filesize+writeBuffer.position() )
throw new IllegalArgumentException("ObjectFileStorage.read["+file.getLabel()+"]: Bad read: location="+loc+" >= max="+(filesize+writeBuffer.position())) ;
int x = writeBuffer.position() ;
int y = writeBuffer.limit() ;
int offset = (int)(loc-filesize) ;
int len = writeBuffer.getInt(offset) ;
int posn = offset + SizeOfInt ;
// Slice the data bytes,
writeBuffer.position(posn) ;
writeBuffer.limit(posn+len) ;
ByteBuffer bb = writeBuffer.slice() ;
writeBuffer.limit(y) ;
writeBuffer.position(x) ;
return bb ;
}
// No - it's in the underlying file storage.
lengthBuffer.clear() ;
int x = file.read(lengthBuffer, loc) ;
if ( x != 4 )
throw new FileException("ObjectFileStorage.read["+file.getLabel()+"]("+loc+")[filesize="+filesize+"][file.size()="+file.size()+"]: Failed to read the length : got "+x+" bytes") ;
int len = lengthBuffer.getInt(0) ;
// Sanity check.
if ( len > filesize-(loc+SizeOfInt) )
{
String msg = "ObjectFileStorage.read["+file.getLabel()+"]("+loc+")[filesize="+filesize+"][file.size()="+file.size()+"]: Impossibly large object : "+len+" bytes > filesize-(loc+SizeOfInt)="+(filesize-(loc+SizeOfInt)) ;
SystemTDB.errlog.error(msg) ;
throw new FileException(msg) ;
}
ByteBuffer bb = ByteBuffer.allocate(len) ;
if ( len == 0 )
// Zero bytes.
return bb ;
x = file.read(bb, loc+SizeOfInt) ;
bb.flip() ;
if ( x != len )
throw new FileException("ObjectFileStorage.read: Failed to read the object ("+len+" bytes) : got "+x+" bytes") ;
return bb ;
}