package com.jscape.util.io;

import com.jscape.util.SuppressObfuscation;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static com.jscape.util.Parameters.assertNotNull;

/**
 * @author Alex Usun
 */
@SuppressObfuscation
public class FileSource
        implements DataSource {

    protected final Path path;

    public FileSource(Path path) {
        assertNotNull(path);
        this.path = path;
    }

    public FileSource(File path) {
        this(path.toPath());
    }

    public FileSource(String path) {
        this(Paths.get(path));
    }

    public boolean writeAllowed() {
        return Files.isWritable(this.path);
    }

    public boolean readAllowed() {
        return Files.isReadable(this.path);
    }

    public InputStream getInputStream()
            throws IOException {
        return Files.newInputStream(this.path);
    }

    public OutputStream getOutputStream()
            throws IOException {
        return Files.newOutputStream(this.path);
    }

    public File file() {
        return this.path.toFile();
    }

    @Override
    public String toString() {
        return "FileSource {"
               + "path=" + this.path
               + '}';
    }
}
