Cleanup
[carla.git] / source / modules / water / files / FileOutputStream.h
blob782612ba68f5c2c173e246cf66038ce2d85e828c
1 /*
2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 OF THIS SOFTWARE.
23 ==============================================================================
26 #ifndef WATER_FILEOUTPUTSTREAM_H_INCLUDED
27 #define WATER_FILEOUTPUTSTREAM_H_INCLUDED
29 #include "../files/File.h"
30 #include "../memory/HeapBlock.h"
31 #include "../streams/OutputStream.h"
33 namespace water {
35 //==============================================================================
36 /**
37 An output stream that writes into a local file.
39 @see OutputStream, FileInputStream, File::createOutputStream
41 class FileOutputStream : public OutputStream
43 public:
44 //==============================================================================
45 /** Creates a FileOutputStream.
47 If the file doesn't exist, it will first be created. If the file can't be
48 created or opened (for example, because the parent directory of the file
49 does not exist), the failedToOpen() method will return true.
51 If the file already exists when opened, the stream's write-position will
52 be set to the end of the file. To overwrite an existing file,
53 use File::deleteFile() before opening the stream, or use setPosition(0)
54 after it's opened (although this won't truncate the file).
56 Destroying a FileOutputStream object does not force the operating system
57 to write the buffered data to disk immediately. If this is required you
58 should call flush() before triggering the destructor.
60 @see TemporaryFile
62 FileOutputStream (const File& fileToWriteTo,
63 size_t bufferSizeToUse = 16384);
65 /** Destructor. */
66 ~FileOutputStream();
68 //==============================================================================
69 /** Returns the file that this stream is writing to.
71 const File& getFile() const { return file; }
73 /** Returns the status of the file stream.
74 The result will be ok if the file opened successfully. If an error occurs while
75 opening or writing to the file, this will contain an error message.
77 const Result& getStatus() const noexcept { return status; }
79 /** Returns true if the stream couldn't be opened for some reason.
80 @see getResult()
82 bool failedToOpen() const noexcept { return status.failed(); }
84 /** Returns true if the stream opened without problems.
85 @see getResult()
87 bool openedOk() const noexcept { return status.wasOk(); }
89 /** Attempts to truncate the file to the current write position.
90 To truncate a file to a specific size, first use setPosition() to seek to the
91 appropriate location, and then call this method.
93 Result truncate();
95 //==============================================================================
96 void flush() override;
97 int64 getPosition() override;
98 bool setPosition (int64) override;
99 bool write (const void*, size_t) override;
100 bool writeRepeatedByte (uint8 byte, size_t numTimesToRepeat) override;
103 private:
104 //==============================================================================
105 File file;
106 void* fileHandle;
107 Result status;
108 int64 currentPosition;
109 size_t bufferSize, bytesInBuffer;
110 HeapBlock<char> buffer;
112 void openHandle();
113 void closeHandle();
114 void flushInternal();
115 bool flushBuffer();
116 int64 setPositionInternal (int64);
117 ssize_t writeInternal (const void*, size_t);
119 CARLA_DECLARE_NON_COPYABLE (FileOutputStream)
124 #endif // WATER_FILEOUTPUTSTREAM_H_INCLUDED