int blockLockId = mTachyonFS.getBlockLockId();
if (!mTachyonFS.lockBlock(blockId, blockLockId)) {
return null;
}
String localFileName = getLocalFilename(blockIndex);
Closer closer = Closer.create();
if (localFileName != null) {
try {
RandomAccessFile localFile = closer.register(new RandomAccessFile(localFileName, "r"));
long fileLength = localFile.length();
String error = null;
if (offset > fileLength) {
error = String.format("Offset(%d) is larger than file length(%d)", offset, fileLength);
}
if (error == null && len != -1 && offset + len > fileLength) {
error =
String.format("Offset(%d) plus length(%d) is larger than file length(%d)", offset,
len, fileLength);
}
if (error != null) {
throw new IOException(error);
}
if (len == -1) {
len = fileLength - offset;
}
FileChannel localFileChannel = closer.register(localFile.getChannel());
final ByteBuffer buf = localFileChannel.map(FileChannel.MapMode.READ_ONLY, offset, len);
mTachyonFS.accessLocalBlock(blockId);
return new TachyonByteBuffer(mTachyonFS, buf, blockId, blockLockId);
} catch (FileNotFoundException e) {
LOG.info(localFileName + " is not on local disk.");
} catch (IOException e) {
LOG.warn("Failed to read local file " + localFileName + " because:", e);
} finally {
closer.close();
}
}
mTachyonFS.unlockBlock(blockId, blockLockId);
return null;