28struct DanglingStreamChecker
30 DanglingStreamChecker() {}
32 ~DanglingStreamChecker()
40 jassert (activeStreams.size() == 0);
43 Array<void*, CriticalSection> activeStreams;
46static DanglingStreamChecker danglingStreamChecker;
50OutputStream::OutputStream()
51 : newLineString (NewLine::getDefault())
54 danglingStreamChecker.activeStreams.add (
this);
58OutputStream::~OutputStream()
61 danglingStreamChecker.activeStreams.removeFirstMatchingValue (
this);
66bool OutputStream::writeBool (
bool b)
68 return writeByte (b ? (
char) 1
72bool OutputStream::writeByte (
char byte)
74 return write (&
byte, 1);
77bool OutputStream::writeRepeatedByte (uint8
byte,
size_t numTimesToRepeat)
79 for (
size_t i = 0; i < numTimesToRepeat; ++i)
80 if (! writeByte ((
char)
byte))
86bool OutputStream::writeShort (
short value)
88 auto v = ByteOrder::swapIfBigEndian ((uint16) value);
92bool OutputStream::writeShortBigEndian (
short value)
94 auto v = ByteOrder::swapIfLittleEndian ((uint16) value);
98bool OutputStream::writeInt (
int value)
100 auto v = ByteOrder::swapIfBigEndian ((uint32) value);
101 return write (&v, 4);
104bool OutputStream::writeIntBigEndian (
int value)
106 auto v = ByteOrder::swapIfLittleEndian ((uint32) value);
107 return write (&v, 4);
110bool OutputStream::writeCompressedInt (
int value)
112 auto un = (value < 0) ? (
unsigned int) -value
113 : (
unsigned int) value;
120 data[++num] = (uint8) un;
124 data[0] = (uint8) num;
129 return write (data, (
size_t) num + 1);
132bool OutputStream::writeInt64 (int64 value)
134 auto v = ByteOrder::swapIfBigEndian ((uint64) value);
135 return write (&v, 8);
138bool OutputStream::writeInt64BigEndian (int64 value)
140 auto v = ByteOrder::swapIfLittleEndian ((uint64) value);
141 return write (&v, 8);
144bool OutputStream::writeFloat (
float value)
146 union {
int asInt;
float asFloat; } n;
148 return writeInt (n.asInt);
151bool OutputStream::writeFloatBigEndian (
float value)
153 union {
int asInt;
float asFloat; } n;
155 return writeIntBigEndian (n.asInt);
158bool OutputStream::writeDouble (
double value)
160 union { int64 asInt;
double asDouble; } n;
162 return writeInt64 (n.asInt);
165bool OutputStream::writeDoubleBigEndian (
double value)
167 union { int64 asInt;
double asDouble; } n;
169 return writeInt64BigEndian (n.asInt);
172bool OutputStream::writeString (
const String& text)
176 #if (JUCE_STRING_UTF_TYPE == 8)
177 return write (text.
toRawUTF8(), numBytes);
183 return write (temp, numBytes);
187bool OutputStream::writeText (
const String& text,
bool asUTF16,
bool writeUTF16ByteOrderMark,
const char* lf)
189 bool replaceLineFeedWithUnix = lf !=
nullptr && lf[0] ==
'\n' && lf[1] == 0;
190 bool replaceLineFeedWithWindows = lf !=
nullptr && lf[0] ==
'\r' && lf[1] ==
'\n' && lf[2] == 0;
193 jassert (lf ==
nullptr || replaceLineFeedWithWindows || replaceLineFeedWithUnix);
197 if (writeUTF16ByteOrderMark)
198 write (
"\x0ff\x0fe", 2);
201 bool lastCharWasReturn =
false;
210 if (replaceLineFeedWithWindows)
212 if (c ==
'\n' && ! lastCharWasReturn)
213 writeShort ((
short)
'\r');
215 lastCharWasReturn = (c == L
'\r');
217 else if (replaceLineFeedWithUnix && c ==
'\r')
222 if (! writeShort ((
short) c))
230 if (replaceLineFeedWithWindows)
237 if (! write (src, (
size_t) (t - src)))
240 if (! write (
"\r\n", 2))
253 if (! write (src, (
size_t) (t - src)))
262 else if (replaceLineFeedWithUnix)
285int64 OutputStream::writeFromInputStream (
InputStream& source, int64 numBytesToWrite)
287 if (numBytesToWrite < 0)
288 numBytesToWrite = std::numeric_limits<int64>::max();
290 int64 numWritten = 0;
292 while (numBytesToWrite > 0)
295 auto num = source.
read (buffer, (
int) jmin (numBytesToWrite, (int64)
sizeof (buffer)));
300 write (buffer, (
size_t) num);
302 numBytesToWrite -= num;
310void OutputStream::setNewLineString (
const String& newLineStringToUse)
312 newLineString = newLineStringToUse;
316template <
typename IntegerType>
317static void writeIntToStream (
OutputStream& stream, IntegerType number)
319 char buffer[NumberToStringConverters::charsNeededForInt];
320 char* end = buffer + numElementsInArray (buffer);
321 const char* start = NumberToStringConverters::numberToString (end, number);
322 stream.
write (start, (
size_t) (end - start - 1));
325JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const int number)
327 writeIntToStream (stream, number);
331JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const int64 number)
333 writeIntToStream (stream, number);
337JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const double number)
339 return stream << String (number);
342JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const char character)
344 stream.writeByte (character);
348JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const char*
const text)
350 stream.write (text, strlen (text));
354JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const MemoryBlock& data)
356 if (data.getSize() > 0)
357 stream.write (data.getData(), data.getSize());
362JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const File& fileToRead)
364 FileInputStream in (fileToRead);
372JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream, InputStream& streamToRead)
374 stream.writeFromInputStream (streamToRead, -1);
378JUCE_API OutputStream& JUCE_CALLTYPE operator<< (OutputStream& stream,
const NewLine&)
380 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...
Very simple container class to hold a pointer to some data on the heap.
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.