53bool DanglingStreamChecker::hasBeenDestroyed =
false;
59OutputStream::OutputStream()
60 : newLineString (NewLine::getDefault())
63 if (! DanglingStreamChecker::hasBeenDestroyed)
64 danglingStreamChecker.activeStreams.add (
this);
68OutputStream::~OutputStream()
71 if (! DanglingStreamChecker::hasBeenDestroyed)
72 danglingStreamChecker.activeStreams.removeFirstMatchingValue (
this);
77bool OutputStream::writeBool (
bool b)
79 return writeByte (b ? (
char) 1
83bool OutputStream::writeByte (
char byte)
85 return write (&
byte, 1);
88bool OutputStream::writeRepeatedByte (
uint8 byte,
size_t numTimesToRepeat)
90 for (
size_t i = 0; i < numTimesToRepeat; ++i)
91 if (! writeByte ((
char)
byte))
97bool OutputStream::writeShort (
short value)
99 auto v = ByteOrder::swapIfBigEndian ((
uint16) value);
100 return write (&v, 2);
103bool OutputStream::writeShortBigEndian (
short value)
105 auto v = ByteOrder::swapIfLittleEndian ((
uint16) value);
106 return write (&v, 2);
109bool OutputStream::writeInt (
int value)
111 auto v = ByteOrder::swapIfBigEndian ((
uint32) value);
112 return write (&v, 4);
115bool OutputStream::writeIntBigEndian (
int value)
117 auto v = ByteOrder::swapIfLittleEndian ((
uint32) value);
118 return write (&v, 4);
121bool 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);
143bool OutputStream::writeInt64 (
int64 value)
145 auto v = ByteOrder::swapIfBigEndian ((
uint64) value);
146 return write (&v, 8);
149bool OutputStream::writeInt64BigEndian (
int64 value)
151 auto v = ByteOrder::swapIfLittleEndian ((
uint64) value);
152 return write (&v, 8);
155bool OutputStream::writeFloat (
float value)
157 union {
int asInt;
float asFloat; } n;
159 return writeInt (n.asInt);
162bool OutputStream::writeFloatBigEndian (
float value)
164 union {
int asInt;
float asFloat; } n;
166 return writeIntBigEndian (n.asInt);
169bool OutputStream::writeDouble (
double value)
171 union {
int64 asInt;
double asDouble; } n;
173 return writeInt64 (n.asInt);
176bool OutputStream::writeDoubleBigEndian (
double value)
178 union {
int64 asInt;
double asDouble; } n;
180 return writeInt64BigEndian (n.asInt);
183bool OutputStream::writeString (
const String& text)
187 #if (JUCE_STRING_UTF_TYPE == 8)
194 return write (temp, numBytes);
198bool 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)
298 if (numBytesToWrite < 0)
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;
321void OutputStream::setNewLineString (
const String& newLineStringToUse)
323 newLineString = newLineStringToUse;
327template <
typename IntegerType>
328static void writeIntToStream (
OutputStream& stream, IntegerType number)
330 char buffer[NumberToStringConverters::charsNeededForInt];
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);
367 if (! data.isEmpty())
368 stream.
write (data.getData(), data.getSize());
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.
Very simple container class to hold a pointer to some data on the heap.
A class to hold a resizable block of raw data.
This class is used for represent a new-line character sequence.
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.
virtual int64 writeFromInputStream(InputStream &source, int64 maxNumBytesToWrite)
Reads data from an input stream and writes it to this stream.
virtual bool writeByte(char byte)
Writes a single byte to the stream.
const String & getNewLineString() const noexcept
Returns the current new-line string that was set by setNewLineString().
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.
unsigned short uint16
A platform-independent 16-bit unsigned integer type.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
unsigned long long uint64
A platform-independent 64-bit unsigned integer type.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
unsigned int uint32
A platform-independent 32-bit unsigned integer type.
unsigned char uint8
A platform-independent 8-bit unsigned integer type.
constexpr int numElementsInArray(Type(&)[N]) noexcept
Handy function for getting the number of elements in a simple const C array.
long long int64
A platform-independent 64-bit integer type.