class SW360AttachmentUtils {

    static Path safeCreateDirectory(Path path, boolean withParents) throws IOException {
        while (!Files.exists(path)) {
            try {
                LOGGER.info("Creating attachment download path {}.", path);
                return withParents ? Files.createDirectories(path) : Files.createDirectory(path);
            } catch (FileAlreadyExistsException e) {
                // ignore; this may be caused by a concurrent attempt to create the directory
                LOGGER.debug("Concurrent creation of directory {}. Ignoring exception.", path);
            }
        }

        if (!Files.isDirectory(path)) {
            throw new FileAlreadyExistsException(path.toString());
        }
        return path;
    }

    private  SW360AttachmentUtils();

}