29 struct DanglingStreamChecker
31 DanglingStreamChecker() =
default;
33 ~DanglingStreamChecker()
41 jassert (activeStreams.size() == 0);
45 hasBeenDestroyed =
true;
48 Array<void*, CriticalSection> activeStreams;
50 static bool hasBeenDestroyed;
53 bool DanglingStreamChecker::hasBeenDestroyed =
false;
54 static DanglingStreamChecker danglingStreamChecker;
59 OutputStream::OutputStream()
60 : newLineString (NewLine::getDefault())
63 if (! DanglingStreamChecker::hasBeenDestroyed)
64 danglingStreamChecker.activeStreams.add (
this);
68 OutputStream::~OutputStream()
71 if (! DanglingStreamChecker::hasBeenDestroyed)
72 danglingStreamChecker.activeStreams.removeFirstMatchingValue (
this);
77 bool OutputStream::writeBool (
bool b)
79 return writeByte (b ? (
char) 1
83 bool OutputStream::writeByte (
char byte)
85 return write (&
byte, 1);
88 bool OutputStream::writeRepeatedByte (uint8
byte,
size_t numTimesToRepeat)
90 for (
size_t i = 0; i < numTimesToRepeat; ++i)
91 if (! writeByte ((
char)
byte))
97 bool OutputStream::writeShort (
short value)
99 auto v = ByteOrder::swapIfBigEndian ((uint16) value);
100 return write (&v, 2);
103 bool OutputStream::writeShortBigEndian (
short value)
105 auto v = ByteOrder::swapIfLittleEndian ((uint16) value);
106 return write (&v, 2);
109 bool OutputStream::writeInt (
int value)
111 auto v = ByteOrder::swapIfBigEndian ((uint32) value);
112 return write (&v, 4);
115 bool OutputStream::writeIntBigEndian (
int value)
117 auto v = ByteOrder::swapIfLittleEndian ((uint32) value);
118 return write (&v, 4);
121 bool OutputStream::writeCompressedInt (
int value)
123 auto un = (value < 0) ? (
unsigned int) -value
124 : (
unsigned int) value;
131 data[++num] = (uint8) un;
135 data[0] = (uint8) num;
140 return write (data, (
size_t) num + 1);
143 bool OutputStream::writeInt64 (int64 value)
145 auto v = ByteOrder::swapIfBigEndian ((uint64) value);
146 return write (&v, 8);
149 bool OutputStream::writeInt64BigEndian (int64 value)
151 auto v = ByteOrder::swapIfLittleEndian ((uint64) value);
152 return write (&v, 8);
155 bool OutputStream::writeFloat (
float value)
157 union {
int asInt;
float asFloat; } n;
159 return writeInt (n.asInt);
162 bool OutputStream::writeFloatBigEndian (
float value)
164 union {
int asInt;
float asFloat; } n;
166 return writeIntBigEndian (n.asInt);
169 bool OutputStream::writeDouble (
double value)
171 union { int64 asInt;
double asDouble; } n;
173 return writeInt64 (n.asInt);
176 bool OutputStream::writeDoubleBigEndian (
double value)
178 union { int64 asInt;
double asDouble; } n;
180 return writeInt64BigEndian (n.asInt);
183 bool OutputStream::writeString (
const String& text)
187 #if (JUCE_STRING_UTF_TYPE == 8)
188 return write (text.
toRawUTF8(), numBytes);
194 return write (temp, numBytes);
198 bool OutputStream::writeText (
const String& text,
bool asUTF16,
bool writeUTF16ByteOrderMark,
const char* lf)
200 bool replaceLineFeedWithUnix = lf !=
nullptr && lf[0] ==
'\n' && lf[1] == 0;
201 bool replaceLineFeedWithWindows = lf !=
nullptr && lf[0] ==
'\r' && lf[1] ==
'\n' && lf[2] == 0;
204 jassert (lf ==
nullptr || replaceLineFeedWithWindows || replaceLineFeedWithUnix);
208 if (writeUTF16ByteOrderMark)
209 write (
"\x0ff\x0fe", 2);
212 bool lastCharWasReturn =
false;
221 if (replaceLineFeedWithWindows)
223 if (c ==
'\n' && ! lastCharWasReturn)
224 writeShort ((
short)
'\r');
226 lastCharWasReturn = (c == L
'\r');
228 else if (replaceLineFeedWithUnix && c ==
'\r')
233 if (! writeShort ((
short) c))
241 if (replaceLineFeedWithWindows)
248 if (! write (src, (
size_t) (t - src)))
251 if (! write (
"\r\n", 2))
264 if (! write (src, (
size_t) (t - src)))
273 else if (replaceLineFeedWithUnix)
296 int64 OutputStream::writeFromInputStream (
InputStream& source, int64 numBytesToWrite)
298 if (numBytesToWrite < 0)
299 numBytesToWrite = std::numeric_limits<int64>::max();
301 int64 numWritten = 0;
303 while (numBytesToWrite > 0)
306 auto num = source.
read (buffer, (
int) jmin (numBytesToWrite, (int64)
sizeof (buffer)));
311 write (buffer, (
size_t) num);
313 numBytesToWrite -= num;
321 void OutputStream::setNewLineString (
const String& newLineStringToUse)
323 newLineString = newLineStringToUse;
327 template <
typename IntegerType>
328 static void writeIntToStream (
OutputStream& stream, IntegerType number)
330 char buffer[NumberToStringConverters::charsNeededForInt];
331 char* end = buffer + numElementsInArray (buffer);
332 const char* start = NumberToStringConverters::numberToString (end, number);
333 stream.
write (start, (
size_t) (end - start - 1));
336 JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const int number)
338 writeIntToStream (stream, number);
342 JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const int64 number)
344 writeIntToStream (stream, number);
348 JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const double number)
350 return stream << String (number);
353 JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const char character)
355 stream.writeByte (character);
359 JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const char*
const text)
361 stream.write (text, strlen (text));
365 JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const MemoryBlock& data)
367 if (data.getSize() > 0)
368 stream.write (data.getData(), data.getSize());
373 JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const File& fileToRead)
375 FileInputStream in (fileToRead);
383 JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, InputStream& streamToRead)
385 stream.writeFromInputStream (streamToRead, -1);
389 JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const NewLine&)
391 return stream << stream.getNewLineString();
juce_wchar getAndAdvance() noexcept
Returns the character that this pointer is currently pointing to, and then advances the pointer to po...
The base class for streams that write data to some kind of destination.
virtual bool write(const void *dataToWrite, size_t numberOfBytes)=0
Writes a block of data to the stream.
CharPointerType getCharPointer() const noexcept
Returns the character pointer currently being used to store this string.
const char * toRawUTF8() const
Returns a pointer to a UTF-8 version of this string.
size_t getNumBytesAsUTF8() const noexcept
Returns the number of bytes required to represent this string as UTF8.
size_t copyToUTF8(CharPointer_UTF8::CharType *destBuffer, size_t maxBufferSizeBytes) const noexcept
Copies the string to a buffer as UTF-8 characters.
#define JUCE_API
This macro is added to all JUCE public class declarations.