Package ch.heigvd.dai.utilities
Class FileManipulator
java.lang.Object
ch.heigvd.dai.utilities.FileManipulator
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 Summary
-
Constructor Summary
ConstructorDescriptionFileManipulator
(String filename) Main constructor.FileManipulator
(String filename, boolean writeEnabled) Write constructor. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]
Reads the file linked to the object and creates a byte array with its content.void
writeBytesToFile
(byte[] bytes) Writes a byte array to a file in the given output.
-
Field Details
-
file
-
writeEnabled
private boolean writeEnabled
-
-
Constructor Details
-
FileManipulator
Main constructor.- Parameters:
filename
- aString
with the path to the file- Throws:
NullPointerException
- if thefilename
argument isnull
-
FileManipulator
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
- aString
with the path to the filewriteEnabled
- aboolean
to enable thewriteBytesToFile(byte[])
function- Throws:
NullPointerException
- if thefilename
argument isnull
-
-
Method Details
-
readBytesFromFile
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 fileOutOfMemoryError
- if the file it too big to fit inside abyte[]
array
-
writeBytesToFile
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
- abyte[]
array with the content to write to the file- Throws:
IOException
- if there is an IO error when trying to open the fileRuntimeException
- if the write operations have not been enabled at the instance creation
-