OpenShot Audio Library | OpenShotAudio 0.4.0
juce_FileOutputStream.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26//==============================================================================
27FileOutputStream::FileOutputStream (const File& f, const size_t bufferSizeToUse)
28 : file (f),
29 bufferSize (bufferSizeToUse),
30 buffer (jmax (bufferSizeToUse, (size_t) 16))
31{
32 openHandle();
33}
34
36{
37 flushBuffer();
38 closeHandle();
39}
40
42{
43 return currentPosition;
44}
45
46bool FileOutputStream::setPosition (int64 newPosition)
47{
48 if (newPosition != currentPosition)
49 {
50 flushBuffer();
51 currentPosition = juce_fileSetPosition (fileHandle, newPosition);
52 }
53
54 return newPosition == currentPosition;
55}
56
57bool FileOutputStream::flushBuffer()
58{
59 bool ok = true;
60
61 if (bytesInBuffer > 0)
62 {
63 ok = (writeInternal (buffer, bytesInBuffer) == (ssize_t) bytesInBuffer);
64 bytesInBuffer = 0;
65 }
66
67 return ok;
68}
69
71{
72 flushBuffer();
73 flushInternal();
74}
75
76bool FileOutputStream::write (const void* const src, const size_t numBytes)
77{
78 jassert (src != nullptr && ((ssize_t) numBytes) >= 0);
79
80 if (! openedOk())
81 return false;
82
83 if (bytesInBuffer + numBytes < bufferSize)
84 {
85 memcpy (buffer + bytesInBuffer, src, numBytes);
86 bytesInBuffer += numBytes;
87 currentPosition += (int64) numBytes;
88 }
89 else
90 {
91 if (! flushBuffer())
92 return false;
93
94 if (numBytes < bufferSize)
95 {
96 memcpy (buffer + bytesInBuffer, src, numBytes);
97 bytesInBuffer += numBytes;
98 currentPosition += (int64) numBytes;
99 }
100 else
101 {
102 auto bytesWritten = writeInternal (src, numBytes);
103
104 if (bytesWritten < 0)
105 return false;
106
107 currentPosition += (int64) bytesWritten;
108 return bytesWritten == (ssize_t) numBytes;
109 }
110 }
111
112 return true;
113}
114
115bool FileOutputStream::writeRepeatedByte (uint8 byte, size_t numBytes)
116{
117 jassert (((ssize_t) numBytes) >= 0);
118
119 if (bytesInBuffer + numBytes < bufferSize)
120 {
121 memset (buffer + bytesInBuffer, byte, numBytes);
122 bytesInBuffer += numBytes;
123 currentPosition += (int64) numBytes;
124 return true;
125 }
126
127 return OutputStream::writeRepeatedByte (byte, numBytes);
128}
129
130} // namespace juce
bool writeRepeatedByte(uint8 byte, size_t numTimesToRepeat) override
bool write(const void *, size_t) override
bool setPosition(int64) override
bool openedOk() const noexcept
FileOutputStream(const File &fileToWriteTo, size_t bufferSizeToUse=16384)
virtual bool writeRepeatedByte(uint8 byte, size_t numTimesToRepeat)