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
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"
35 //==============================================================================
37 An output stream that writes into a local file.
39 @see OutputStream, FileInputStream, File::createOutputStream
41 class FileOutputStream
: public OutputStream
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.
62 FileOutputStream (const File
& fileToWriteTo
,
63 size_t bufferSizeToUse
= 16384);
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.
82 bool failedToOpen() const noexcept
{ return status
.failed(); }
84 /** Returns true if the stream opened without problems.
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.
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
;
104 //==============================================================================
108 int64 currentPosition
;
109 size_t bufferSize
, bytesInBuffer
;
110 HeapBlock
<char> buffer
;
114 void flushInternal();
116 int64
setPositionInternal (int64
);
117 ssize_t
writeInternal (const void*, size_t);
119 CARLA_DECLARE_NON_COPYABLE (FileOutputStream
)
124 #endif // WATER_FILEOUTPUTSTREAM_H_INCLUDED