FileOutputStream.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2020-08-20
  7. *
  8. * */
  9. #include "FileOutputStream.hpp"
  10. #include "../exception/FileNotFoundException.hpp"
  11. #include "../exception/FileOperationException.hpp"
  12. ls_std::FileOutputStream::FileOutputStream(File &_file) : Class("FileOutputStream"),
  13. file(_file)
  14. {
  15. if(!_file.exists()) {
  16. throw ls_std::FileNotFoundException {};
  17. } else {
  18. this->outputStream.open(_file.getAbsoluteFilePath());
  19. }
  20. }
  21. ls_std::FileOutputStream::~FileOutputStream()
  22. {
  23. this->_close();
  24. }
  25. void ls_std::FileOutputStream::close()
  26. {
  27. this->_close();
  28. }
  29. bool ls_std::FileOutputStream::write(const ls_std::byte *_data)
  30. {
  31. bool succeeded {};
  32. if(this->outputStream.is_open()) {
  33. if (this->outputStream << _data) {
  34. succeeded = true;
  35. }
  36. } else {
  37. throw ls_std::FileOperationException {};
  38. }
  39. return succeeded;
  40. }
  41. void ls_std::FileOutputStream::_close()
  42. {
  43. if(this->outputStream.is_open()) {
  44. this->outputStream.close();
  45. }
  46. }