SessionWorkspace.java [src/csip/api/server] Revision:   Date:
/*
 * $Id:$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2022, Olaf David and others, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package csip.api.server;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.stream.Stream;

/**
 * Session Workspace management.
 *
 * @author od
 */
public interface SessionWorkspace {

  /**
   * Get the lines as stream.
   *
   * @param filename the file in the workspace
   * @return the stream of lines. (close it when done!)
   * @throws IOException if the file does not exist.
   */
  Stream<String> lines(String filename) throws IOException;


  /**
   * Get the lines as stream.
   *
   * @param file the file in the workspace
   * @return the stream of lines. (close it when done!)
   * @throws IOException if the file does not exist.
   */
  Stream<String> lines(File file) throws IOException;


  /**
   * Get a the workspace folder for this model run.
   *
   * @return the workspace directory
   */
  File getDir();


  /**
   * Get a file with the workspace folder.
   *
   * @param filename the file name
   * @return the file in the workspace
   */
  File getFile(String filename);


  /**
   * Get files in workspace based on pattern.
   *
   * @param pattern e.g. "*.txt"
   * @return the list of files matching the pattern
   * @throws IOException if the operation fails
   */
  File[] getFiles(String pattern) throws IOException;


  /**
   * Get a file with the workspace folder. The file must exist.
   *
   * @param filename the file in the workspace
   * @return the file in the workspace
   * @throws java.io.FileNotFoundException if the file is not found
   */
  File getExistingFile(String filename) throws FileNotFoundException;


  /**
   * Get the Path object of the workspace.
   * @return the Path
   */
  Path getPath();


  /**
   * Get the content of a file within the workspace as String.
   * @param filename the file name
   * @return the String content
   * @throws IOException if reading fails
   */
  String readString(String filename) throws IOException;


  /**
   * Write a string to a file within the workspace.
   *
   * @param filename the file name within the workspace
   * @param data the content
   * @throws IOException if writing fails
   */
  File writeString(String filename, String data) throws IOException;


  /**
   * Get the content of a file within the workspace as byte array.
   * @param filename the filename
   * @return the content as byte array
   * @throws IOException
   */
  byte[] readBytes(String filename) throws IOException;


  /**
   * Write bytes to a file within the workspace.
   *
   * @param filename the file name within the workspace
   * @param data the content
   * @throws IOException if writing fails.
   */
  void writeBytes(String filename, byte[] data) throws IOException;


  /**
   * Check is a file exists in the workspace.
   *
   * @param filenames the file names
   * @return this object
   * @throws ServiceException if the parameter is not found.
   */
  SessionWorkspace exist(String... filenames) throws ServiceException;

}