package be.iminds.ilabt.jfed.highlevel.util;

import net.schmizz.sshj.xfer.InMemorySourceFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * User: twalcari
 * Date: 6/3/2016
 * Time: 16:11
 */
public class StringMemorySourceFile extends InMemorySourceFile {

    private final String fileName;
    private final byte[] contentBytes;

    public StringMemorySourceFile(String fileName, String contents) {
        this.fileName = fileName;
        this.contentBytes = contents.getBytes(StandardCharsets.UTF_8);
    }

    public StringMemorySourceFile(String fileName, char[] contents) {
        this(fileName, new String(contents));
    }

    @Override
    public String getName() {
        return fileName;
    }

    @Override
    public long getLength() {
        return contentBytes.length;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(contentBytes);
    }
}
