class FileUtils {

    public static ByteBuffer readFile(@NotNull File file, long offset, long length) {
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) length);
        int bytesRead = 0;
        try {
            // TODO: for exclusively read of file channel by single thread we can use read w/o offset (avoid additional seeks?)
            while ((bytesRead = file.getFileChannel().read(byteBuffer, offset)) != -1) {
                if (!byteBuffer.hasRemaining()) {
                    byteBuffer.flip();
                    return byteBuffer;
                }
                offset += bytesRead;
            }
        } catch (Exception e) {
            log.error("Can't read file", e);
        }

        if (bytesRead < byteBuffer.capacity()) {
            String error = String.format("File read error for file %s", file);
            log.error(error);
            throw new IllegalArgumentException(error);
        }

        byteBuffer.flip();
        return byteBuffer;
    }

    private  FileUtils();

}