28 struct DanglingStreamChecker
30 DanglingStreamChecker() {}
32 ~DanglingStreamChecker()
40 jassert (activeStreams.size() == 0);
43 Array<void*, CriticalSection> activeStreams;
46 static DanglingStreamChecker danglingStreamChecker;
50 OutputStream::OutputStream()
51 : newLineString (NewLine::getDefault())
54 danglingStreamChecker.activeStreams.add (
this);
58 OutputStream::~OutputStream()
61 danglingStreamChecker.activeStreams.removeFirstMatchingValue (
this);
66 bool OutputStream::writeBool (
bool b)
68 return writeByte (b ? (
char) 1
72 bool OutputStream::writeByte (
char byte)
74 return write (&byte, 1);
77 bool OutputStream::writeRepeatedByte (uint8 byte,
size_t numTimesToRepeat)
79 for (
size_t i = 0; i < numTimesToRepeat; ++i)
80 if (! writeByte ((
char) byte))
86 bool OutputStream::writeShort (
short value)
88 auto v = ByteOrder::swapIfBigEndian ((uint16) value);
92 bool OutputStream::writeShortBigEndian (
short value)
94 auto v = ByteOrder::swapIfLittleEndian ((uint16) value);
98 bool OutputStream::writeInt (
int value)
100 auto v = ByteOrder::swapIfBigEndian ((uint32) value);
101 return write (&v, 4);
104 bool OutputStream::writeIntBigEndian (
int value)
106 auto v = ByteOrder::swapIfLittleEndian ((uint32) value);
107 return write (&v, 4);
110 bool 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);
132 bool OutputStream::writeInt64 (int64 value)
134 auto v = ByteOrder::swapIfBigEndian ((uint64) value);
135 return write (&v, 8);
138 bool OutputStream::writeInt64BigEndian (int64 value)
140 auto v = ByteOrder::swapIfLittleEndian ((uint64) value);
141 return write (&v, 8);
144 bool OutputStream::writeFloat (
float value)
146 union {
int asInt;
float asFloat; } n;
148 return writeInt (n.asInt);
151 bool OutputStream::writeFloatBigEndian (
float value)
153 union {
int asInt;
float asFloat; } n;
155 return writeIntBigEndian (n.asInt);
158 bool OutputStream::writeDouble (
double value)
160 union { int64 asInt;
double asDouble; } n;
162 return writeInt64 (n.asInt);
165 bool OutputStream::writeDoubleBigEndian (
double value)
167 union { int64 asInt;
double asDouble; } n;
169 return writeInt64BigEndian (n.asInt);
172 bool OutputStream::writeString (
const String& text)
176 #if (JUCE_STRING_UTF_TYPE == 8) 177 return write (text.
toRawUTF8(), numBytes);
183 return write (temp, numBytes);
187 bool 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)
285 int64 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;
310 void OutputStream::setNewLineString (
const String& newLineStringToUse)
312 newLineString = newLineStringToUse;
316 template <
typename IntegerType>
317 static 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));
327 writeIntToStream (stream, number);
333 writeIntToStream (stream, number);
339 return stream <<
String (number);
350 stream.
write (text, strlen (text));
size_t getSize() const noexcept
Returns the block's current allocated size, in bytes.
#define JUCE_API
This macro is added to all JUCE public class declarations.
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 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.
This class is used for represent a new-line character sequence.
void * getData() const noexcept
Returns a void pointer to the data.
const String & getNewLineString() const noexcept
Returns the current new-line string that was set by setNewLineString().
juce_wchar getAndAdvance() noexcept
Returns the character that this pointer is currently pointing to, and then advances the pointer to po...
Represents a local file or directory.
The base class for streams that write data to some kind of destination.
virtual int64 writeFromInputStream(InputStream &source, int64 maxNumBytesToWrite)
Reads data from an input stream and writes it to this stream.
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.