package jetbrains.mps.vcs.core.mergedriver;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import jetbrains.mps.extapi.persistence.DataSourceBase;
import org.jetbrains.mps.openapi.persistence.StreamDataSource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import jetbrains.mps.util.ReadUtil;
import jetbrains.mps.util.FileUtil;
import org.jetbrains.annotations.NotNull;
import java.io.ByteArrayInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;

@GeneratedClass(node = "r:a178d3c3-970e-4352-b61c-4e55abc3bc24(jetbrains.mps.vcs.core.mergedriver)/1578360511938004001", model = "r:a178d3c3-970e-4352-b61c-4e55abc3bc24(jetbrains.mps.vcs.core.mergedriver)")
public class FileContent extends DataSourceBase implements StreamDataSource {
  private final File myFile;
  private final byte[] myData;

  public FileContent(File file) throws IOException {
    myFile = file;
    InputStream stream = new FileInputStream(file);
    try {
      myData = ReadUtil.read(stream);
    } finally {
      FileUtil.closeFileSafe(stream);
    }
  }

  @Override
  @NotNull
  public String getStreamName() {
    return "file-content " + myFile.getName();
  }

  public File getFile() {
    return myFile;
  }

  public byte[] getData() {
    return myData;
  }

  @NotNull
  @Override
  public InputStream openInputStream() {
    return new ByteArrayInputStream(myData);
  }

  @NotNull
  @Override
  public OutputStream openOutputStream() throws IOException {
    return new FileOutputStream(myFile);
  }

  @Override
  public boolean isReadOnly() {
    return false;
  }

  @Override
  public boolean delete() {
    return myFile.delete();
  }

  @Override
  public boolean exists() {
    return myFile.exists();
  }

  @NotNull
  @Override
  public String getLocation() {
    return myFile.getName();
  }
}
