Class FileManipulator

java.lang.Object
ch.heigvd.dai.utilities.FileManipulator

public class FileManipulator extends Object
Implements the necessary functions to read/write the bytes of the file to hide/expose.
Author:
Pedro Alves da Silva, Gonçalo Carvalheiro Heleno
  • Field Details

    • file

      private final File file
    • writeEnabled

      private boolean writeEnabled
  • Constructor Details

    • FileManipulator

      public FileManipulator(String filename) throws NullPointerException
      Main constructor.
      Parameters:
      filename - a String with the path to the file
      Throws:
      NullPointerException - if the filename argument is null
    • FileManipulator

      public FileManipulator(String filename, boolean writeEnabled) throws NullPointerException
      Write constructor.

      Constructor used when the file is opened with write permissions. This is merely a flag we use to make sure the write function cannot be called unless the object explicitly allows it.

      Parameters:
      filename - a String with the path to the file
      writeEnabled - a boolean to enable the writeBytesToFile(byte[]) function
      Throws:
      NullPointerException - if the filename argument is null
  • Method Details

    • readBytesFromFile

      public byte[] readBytesFromFile() throws IOException, OutOfMemoryError
      Reads the file linked to the object and creates a byte array with its content.
      Returns:
      a byte[] array with the contents of the file
      Throws:
      IOException - if there is an IO error when trying to open the file
      OutOfMemoryError - if the file it too big to fit inside a byte[] array
    • writeBytesToFile

      public void writeBytesToFile(byte[] bytes) throws IOException, RuntimeException
      Writes a byte array to a file in the given output.

      Note that this could be a destructive operation if there is already a file at that location, so you should take care to perform the necessary verifications.

      Parameters:
      bytes - a byte[] array with the content to write to the file
      Throws:
      IOException - if there is an IO error when trying to open the file
      RuntimeException - if the write operations have not been enabled at the instance creation