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);
71 if (! DanglingStreamChecker::hasBeenDestroyed)
72 danglingStreamChecker.activeStreams.removeFirstMatchingValue (
this);
79 return writeByte (b ? (
char) 1
85 return write (&byte, 1);
90 for (
size_t i = 0; i < numTimesToRepeat; ++i)
91 if (! writeByte ((
char) byte))
100 return write (&v, 2);
106 return write (&v, 2);
112 return write (&v, 4);
118 return write (&v, 4);
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);
146 return write (&v, 8);
152 return write (&v, 8);
157 union {
int asInt;
float asFloat; } n;
159 return writeInt (n.asInt);
164 union {
int asInt;
float asFloat; } n;
166 return writeIntBigEndian (n.asInt);
171 union { int64 asInt;
double asDouble; } n;
173 return writeInt64 (n.asInt);
178 union { int64 asInt;
double asDouble; } n;
180 return writeInt64BigEndian (n.asInt);
187 #if (JUCE_STRING_UTF_TYPE == 8) 188 return write (text.
toRawUTF8(), numBytes);
194 return write (temp, numBytes);
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)
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;
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));
338 writeIntToStream (stream, number);
344 writeIntToStream (stream, number);
350 return stream <<
String (number);
361 stream.
write (text, strlen (text));
size_t getSize() const noexcept
Returns the block's current allocated size, in bytes.
static Type swapIfBigEndian(Type value) noexcept
Swaps the byte order of a signed or unsigned integer if the CPU is big-endian.
virtual bool writeDoubleBigEndian(double value)
Writes a 64-bit floating point value to the stream in a binary format.
virtual bool writeShortBigEndian(short value)
Writes a 16-bit integer to the stream in a big-endian byte order.
#define JUCE_API
This macro is added to all JUCE public class declarations.
virtual bool writeFloat(float value)
Writes a 32-bit floating point value to the stream in a binary format.
virtual bool write(const void *dataToWrite, size_t numberOfBytes)=0
Writes a block of data to the stream.
const char * toRawUTF8() const
Returns a pointer to a UTF-8 version of this string.
virtual bool writeFloatBigEndian(float value)
Writes a 32-bit floating point value to the stream in a binary format.
virtual bool writeByte(char byte)
Writes a single byte to the stream.
size_t copyToUTF8(CharPointer_UTF8::CharType *destBuffer, size_t maxBufferSizeBytes) const noexcept
Copies the string to a buffer as UTF-8 characters.
CharPointerType getCharPointer() const noexcept
Returns the character pointer currently being used to store this string.
virtual ~OutputStream()
Destructor.
virtual bool writeInt64BigEndian(int64 value)
Writes a 64-bit integer to the stream in a big-endian byte order.
This class is used for represent a new-line character sequence.
virtual bool writeText(const String &text, bool asUTF16, bool writeUTF16ByteOrderMark, const char *lineEndings)
Writes a string of text to the stream.
const String & getNewLineString() const noexcept
Returns the current new-line string that was set by setNewLineString().
void * getData() noexcept
Returns a void pointer to the data.
virtual bool writeIntBigEndian(int value)
Writes a 32-bit integer to the stream in a big-endian byte order.
void setNewLineString(const String &newLineString)
Sets the string to write to the stream when a new line is written.
virtual bool writeInt64(int64 value)
Writes a 64-bit integer to the stream in a little-endian byte order.
juce_wchar getAndAdvance() noexcept
Returns the character that this pointer is currently pointing to, and then advances the pointer to po...
virtual bool writeShort(short value)
Writes a 16-bit integer to the stream in a little-endian byte order.
static Type swapIfLittleEndian(Type value) noexcept
Swaps the byte order of a signed or unsigned integer if the CPU is little-endian. ...
Represents a local file or directory.
The base class for streams that write data to some kind of destination.
virtual bool writeDouble(double value)
Writes a 64-bit floating point value to the stream in a binary format.
virtual int64 writeFromInputStream(InputStream &source, int64 maxNumBytesToWrite)
Reads data from an input stream and writes it to this stream.
virtual bool writeRepeatedByte(uint8 byte, size_t numTimesToRepeat)
Writes a byte to the output stream a given number of times.
virtual bool writeBool(bool boolValue)
Writes a boolean to the stream as a single byte.
virtual bool writeInt(int value)
Writes a 32-bit integer to the stream in a little-endian byte order.
virtual bool writeCompressedInt(int value)
Writes a condensed binary encoding of a 32-bit integer.
virtual bool writeString(const String &text)
Stores a string in the stream in a binary format.
size_t getNumBytesAsUTF8() const noexcept
Returns the number of bytes required to represent this string as UTF8.
A class to hold a resizable block of raw data.