JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_FileOutputStream.cpp
Go to the documentation of this file.
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//==============================================================================
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
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
Writes a byte to the output stream a given number of times.
bool write(const void *, size_t) override
Writes a block of data to the stream.
~FileOutputStream() override
Destructor.
bool setPosition(int64) override
Tries to move the stream's output position.
bool openedOk() const noexcept
Returns true if the stream opened without problems.
void flush() override
If the stream is using a buffer, this will ensure it gets written out to the destination.
int64 getPosition() override
Returns the stream's current position.
FileOutputStream(const File &fileToWriteTo, size_t bufferSizeToUse=16384)
Creates a FileOutputStream.
Represents a local file or directory.
Definition juce_File.h:45
virtual bool writeRepeatedByte(uint8 byte, size_t numTimesToRepeat)
Writes a byte to the output stream a given number of times.
#define jassert(expression)
Platform-independent assertion macro.
memcpy
memset
JUCE Namespace.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
unsigned char uint8
A platform-independent 8-bit unsigned integer type.
long long int64
A platform-independent 64-bit integer type.
typedef size_t