/** * Loads the actual byte array from a segment, in the range of a specific chunkSize. * Not that while the chunkSize is specified in this case, it's likely derived * from the invocations of other loading methods. */ private byte[] loadIntern(final ChunkCacheKey key) throws IOException { final String fileName = key.getFileName(); final long chunkId = key.getChunkId(); //needs to be long to upcast following operations int bufferSize = key.getBufferSize(); final long seekTo = chunkId * bufferSize; final byte[] buffer; final IndexInput input = directory.openInput(fileName); final long length = input.length(); try { if (seekTo != 0) { input.seek(seekTo); } bufferSize = (int) Math.min(length - seekTo, (long)bufferSize); buffer = new byte[bufferSize]; input.readBytes(buffer, 0, bufferSize); } finally { input.close(); } return buffer; }