package io.qameta.allure;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.qameta.allure.model.Allure2ModelJackson;
import io.qameta.allure.model.TestResult;
import io.qameta.allure.model.TestResultContainer;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

import static io.qameta.allure.AllureUtils.generateTestResultContainerName;
import static io.qameta.allure.AllureUtils.generateTestResultName;
import static java.nio.file.StandardOpenOption.CREATE_NEW;

/**
 * @author charlie (Dmitry Baev).
 */
public class FileSystemResultsWriter implements AllureResultsWriter {

    private final Path outputDirectory;

    private final ObjectMapper mapper;

    public FileSystemResultsWriter(Path outputDirectory) {
        this.outputDirectory = outputDirectory;
        this.mapper = Allure2ModelJackson.createMapper();
    }

    @Override
    public void write(TestResult testResult) {
        final String testResultName = Objects.isNull(testResult.getUuid())
                ? generateTestResultName()
                : generateTestResultName(testResult.getUuid());
        createDirectories(outputDirectory);
        Path file = outputDirectory.resolve(testResultName);
        try (OutputStream os = Files.newOutputStream(file, CREATE_NEW)) {
            mapper.writeValue(os, testResult);
        } catch (IOException e) {
            throw new AllureResultsWriteException("Could not write Allure test result", e);
        }
    }

    @Override
    public void write(TestResultContainer testResultContainer) {
        final String testResultContainerName = Objects.isNull(testResultContainer.getUuid())
                ? generateTestResultContainerName()
                : generateTestResultContainerName(testResultContainer.getUuid());
        createDirectories(outputDirectory);
        Path file = outputDirectory.resolve(testResultContainerName);
        try (OutputStream os = Files.newOutputStream(file, CREATE_NEW)) {
            mapper.writeValue(os, testResultContainer);
        } catch (IOException e) {
            throw new AllureResultsWriteException("Could not write Allure test result container", e);
        }
    }

    @Override
    public void write(String source, InputStream attachment) {
        createDirectories(outputDirectory);
        Path file = outputDirectory.resolve(source);
        try (InputStream is = attachment) {
            Files.copy(is, file);
        } catch (IOException e) {
            throw new AllureResultsWriteException("Could not write Allure attachment", e);
        }
    }

    private void createDirectories(Path directory) {
        try {
            Files.createDirectories(directory);
        } catch (IOException e) {
            throw new AllureResultsWriteException("Could not create Allure results directory", e);
        }
    }
}

@@ -236,76 +236,9 @@
 .io.
-IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream
+*
 ;%0Aim
@@ -463,67 +463,8 @@
 ame;
-%0Aimport static java.nio.file.StandardOpenOption.CREATE_NEW;
 %0A%0A/*
@@ -1202,33 +1202,57 @@
 Stream os = 
-Files.new
+new BufferedOutputStream(new File
 OutputStream
@@ -1252,36 +1252,34 @@
 tStream(file
-, CREATE_NEW
+.toFile())
 )) %7B%0A       
@@ -1916,17 +1916,41 @@
 s = 
-Files.new
+new BufferedOutputStream(new File
 Outp
@@ -1966,20 +1966,18 @@
 file
-, CREATE_NEW
+.toFile())
 )) %7B

use blocking io instead of nio (via #16)