OpenShot Library | libopenshot-audio  0.1.9
juce_FileOutputStream.h
1 
2 /** @weakgroup juce_core-files
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  An output stream that writes into a local file.
33 
34  @see OutputStream, FileInputStream, File::createOutputStream
35 
36  @tags{Core}
37 */
39 {
40 public:
41  //==============================================================================
42  /** Creates a FileOutputStream.
43 
44  If the file doesn't exist, it will first be created. If the file can't be
45  created or opened (for example, because the parent directory of the file
46  does not exist), the failedToOpen() method will return true.
47 
48  If the file already exists when opened, the stream's write-position will
49  be set to the end of the file. To overwrite an existing file, you can truncate
50  it like this:
51 
52  @code
53  FileOutputStream stream (file);
54 
55  if (stream.openedOk())
56  {
57  stream.setPosition (0);
58  stream.truncate();
59  ...
60  }
61  @endcode
62 
63 
64  Destroying a FileOutputStream object does not force the operating system
65  to write the buffered data to disk immediately. If this is required you
66  should call flush() before triggering the destructor.
67 
68  @see TemporaryFile
69  */
70  FileOutputStream (const File& fileToWriteTo,
71  size_t bufferSizeToUse = 16384);
72 
73  /** Destructor. */
74  ~FileOutputStream() override;
75 
76  //==============================================================================
77  /** Returns the file that this stream is writing to.
78  */
79  const File& getFile() const { return file; }
80 
81  /** Returns the status of the file stream.
82  The result will be ok if the file opened successfully. If an error occurs while
83  opening or writing to the file, this will contain an error message.
84  */
85  const Result& getStatus() const noexcept { return status; }
86 
87  /** Returns true if the stream couldn't be opened for some reason.
88  @see getResult()
89  */
90  bool failedToOpen() const noexcept { return status.failed(); }
91 
92  /** Returns true if the stream opened without problems.
93  @see getResult()
94  */
95  bool openedOk() const noexcept { return status.wasOk(); }
96 
97  /** Attempts to truncate the file to the current write position.
98  To truncate a file to a specific size, first use setPosition() to seek to the
99  appropriate location, and then call this method.
100  */
101  Result truncate();
102 
103  //==============================================================================
104  void flush() override;
105  int64 getPosition() override;
106  bool setPosition (int64) override;
107  bool write (const void*, size_t) override;
108  bool writeRepeatedByte (uint8 byte, size_t numTimesToRepeat) override;
109 
110 
111 private:
112  //==============================================================================
113  File file;
114  void* fileHandle = nullptr;
115  Result status { Result::ok() };
116  int64 currentPosition = 0;
117  size_t bufferSize, bytesInBuffer = 0;
118  HeapBlock<char> buffer;
119 
120  void openHandle();
121  void closeHandle();
122  void flushInternal();
123  bool flushBuffer();
124  int64 setPositionInternal (int64);
125  ssize_t writeInternal (const void*, size_t);
126 
127  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FileOutputStream)
128 };
129 
130 } // namespace juce
131 
132 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
static Result ok() noexcept
Creates and returns a &#39;successful&#39; result.
Definition: juce_Result.h:65
const Result & getStatus() const noexcept
Returns the status of the file stream.
Represents the &#39;success&#39; or &#39;failure&#39; of an operation, and holds an associated error message to descr...
Definition: juce_Result.h:60
Represents a local file or directory.
Definition: juce_File.h:44
The base class for streams that write data to some kind of destination.
An output stream that writes into a local file.
bool openedOk() const noexcept
Returns true if the stream opened without problems.
const File & getFile() const
Returns the file that this stream is writing to.
bool failedToOpen() const noexcept
Returns true if the stream couldn&#39;t be opened for some reason.