| @@ -119,7 +119,7 @@ bool FileOutputStream::write (const void* const src, const size_t numBytes) | |||
| return true; | |||
| } | |||
| void FileOutputStream::writeRepeatedByte (uint8 byte, size_t numBytes) | |||
| bool FileOutputStream::writeRepeatedByte (uint8 byte, size_t numBytes) | |||
| { | |||
| jassert (((ssize_t) numBytes) >= 0); | |||
| @@ -128,9 +128,8 @@ void FileOutputStream::writeRepeatedByte (uint8 byte, size_t numBytes) | |||
| memset (buffer + bytesInBuffer, byte, numBytes); | |||
| bytesInBuffer += numBytes; | |||
| currentPosition += numBytes; | |||
| return true; | |||
| } | |||
| else | |||
| { | |||
| OutputStream::writeRepeatedByte (byte, numBytes); | |||
| } | |||
| return OutputStream::writeRepeatedByte (byte, numBytes); | |||
| } | |||
| @@ -90,11 +90,11 @@ public: | |||
| Result truncate(); | |||
| //============================================================================== | |||
| void flush(); | |||
| int64 getPosition(); | |||
| bool setPosition (int64 pos); | |||
| bool write (const void* data, size_t numBytes); | |||
| void writeRepeatedByte (uint8 byte, size_t numTimesToRepeat); | |||
| void flush() override; | |||
| int64 getPosition() override; | |||
| bool setPosition (int64) override; | |||
| bool write (const void*, size_t) override; | |||
| bool writeRepeatedByte (uint8 byte, size_t numTimesToRepeat) override; | |||
| private: | |||
| @@ -95,10 +95,12 @@ bool MemoryOutputStream::write (const void* const buffer, size_t howMany) | |||
| return true; | |||
| } | |||
| void MemoryOutputStream::writeRepeatedByte (uint8 byte, size_t howMany) | |||
| bool MemoryOutputStream::writeRepeatedByte (uint8 byte, size_t howMany) | |||
| { | |||
| if (howMany > 0) | |||
| memset (prepareToWrite (howMany), byte, howMany); | |||
| return true; | |||
| } | |||
| void MemoryOutputStream::appendUTF8Char (juce_wchar c) | |||
| @@ -111,11 +111,11 @@ public: | |||
| */ | |||
| void flush(); | |||
| bool write (const void* buffer, size_t howMany); | |||
| int64 getPosition() { return position; } | |||
| bool setPosition (int64 newPosition); | |||
| int writeFromInputStream (InputStream& source, int64 maxNumBytesToWrite); | |||
| void writeRepeatedByte (uint8 byte, size_t numTimesToRepeat); | |||
| bool write (const void*, size_t) override; | |||
| int64 getPosition() override { return position; } | |||
| bool setPosition (int64) override; | |||
| int writeFromInputStream (InputStream&, int64 maxNumBytesToWrite) override; | |||
| bool writeRepeatedByte (uint8 byte, size_t numTimesToRepeat) override; | |||
| private: | |||
| //============================================================================== | |||
| @@ -66,48 +66,51 @@ OutputStream::~OutputStream() | |||
| } | |||
| //============================================================================== | |||
| void OutputStream::writeBool (const bool b) | |||
| bool OutputStream::writeBool (const bool b) | |||
| { | |||
| writeByte (b ? (char) 1 | |||
| : (char) 0); | |||
| return writeByte (b ? (char) 1 | |||
| : (char) 0); | |||
| } | |||
| void OutputStream::writeByte (char byte) | |||
| bool OutputStream::writeByte (char byte) | |||
| { | |||
| write (&byte, 1); | |||
| return write (&byte, 1); | |||
| } | |||
| void OutputStream::writeRepeatedByte (uint8 byte, size_t numTimesToRepeat) | |||
| bool OutputStream::writeRepeatedByte (uint8 byte, size_t numTimesToRepeat) | |||
| { | |||
| for (size_t i = 0; i < numTimesToRepeat; ++i) | |||
| writeByte ((char) byte); | |||
| if (! writeByte ((char) byte)) | |||
| return false; | |||
| return true; | |||
| } | |||
| void OutputStream::writeShort (short value) | |||
| bool OutputStream::writeShort (short value) | |||
| { | |||
| const unsigned short v = ByteOrder::swapIfBigEndian ((unsigned short) value); | |||
| write (&v, 2); | |||
| return write (&v, 2); | |||
| } | |||
| void OutputStream::writeShortBigEndian (short value) | |||
| bool OutputStream::writeShortBigEndian (short value) | |||
| { | |||
| const unsigned short v = ByteOrder::swapIfLittleEndian ((unsigned short) value); | |||
| write (&v, 2); | |||
| return write (&v, 2); | |||
| } | |||
| void OutputStream::writeInt (int value) | |||
| bool OutputStream::writeInt (int value) | |||
| { | |||
| const unsigned int v = ByteOrder::swapIfBigEndian ((unsigned int) value); | |||
| write (&v, 4); | |||
| return write (&v, 4); | |||
| } | |||
| void OutputStream::writeIntBigEndian (int value) | |||
| bool OutputStream::writeIntBigEndian (int value) | |||
| { | |||
| const unsigned int v = ByteOrder::swapIfLittleEndian ((unsigned int) value); | |||
| write (&v, 4); | |||
| return write (&v, 4); | |||
| } | |||
| void OutputStream::writeCompressedInt (int value) | |||
| bool OutputStream::writeCompressedInt (int value) | |||
| { | |||
| unsigned int un = (value < 0) ? (unsigned int) -value | |||
| : (unsigned int) value; | |||
| @@ -126,60 +129,60 @@ void OutputStream::writeCompressedInt (int value) | |||
| if (value < 0) | |||
| data[0] |= 0x80; | |||
| write (data, num + 1); | |||
| return write (data, num + 1); | |||
| } | |||
| void OutputStream::writeInt64 (int64 value) | |||
| bool OutputStream::writeInt64 (int64 value) | |||
| { | |||
| const uint64 v = ByteOrder::swapIfBigEndian ((uint64) value); | |||
| write (&v, 8); | |||
| return write (&v, 8); | |||
| } | |||
| void OutputStream::writeInt64BigEndian (int64 value) | |||
| bool OutputStream::writeInt64BigEndian (int64 value) | |||
| { | |||
| const uint64 v = ByteOrder::swapIfLittleEndian ((uint64) value); | |||
| write (&v, 8); | |||
| return write (&v, 8); | |||
| } | |||
| void OutputStream::writeFloat (float value) | |||
| bool OutputStream::writeFloat (float value) | |||
| { | |||
| union { int asInt; float asFloat; } n; | |||
| n.asFloat = value; | |||
| writeInt (n.asInt); | |||
| return writeInt (n.asInt); | |||
| } | |||
| void OutputStream::writeFloatBigEndian (float value) | |||
| bool OutputStream::writeFloatBigEndian (float value) | |||
| { | |||
| union { int asInt; float asFloat; } n; | |||
| n.asFloat = value; | |||
| writeIntBigEndian (n.asInt); | |||
| return writeIntBigEndian (n.asInt); | |||
| } | |||
| void OutputStream::writeDouble (double value) | |||
| bool OutputStream::writeDouble (double value) | |||
| { | |||
| union { int64 asInt; double asDouble; } n; | |||
| n.asDouble = value; | |||
| writeInt64 (n.asInt); | |||
| return writeInt64 (n.asInt); | |||
| } | |||
| void OutputStream::writeDoubleBigEndian (double value) | |||
| bool OutputStream::writeDoubleBigEndian (double value) | |||
| { | |||
| union { int64 asInt; double asDouble; } n; | |||
| n.asDouble = value; | |||
| writeInt64BigEndian (n.asInt); | |||
| return writeInt64BigEndian (n.asInt); | |||
| } | |||
| void OutputStream::writeString (const String& text) | |||
| bool OutputStream::writeString (const String& text) | |||
| { | |||
| // (This avoids using toUTF8() to prevent the memory bloat that it would leave behind | |||
| // if lots of large, persistent strings were to be written to streams). | |||
| const size_t numBytes = text.getNumBytesAsUTF8() + 1; | |||
| HeapBlock<char> temp (numBytes); | |||
| text.copyToUTF8 (temp, numBytes); | |||
| write (temp, numBytes); | |||
| return write (temp, numBytes); | |||
| } | |||
| void OutputStream::writeText (const String& text, const bool asUTF16, | |||
| bool OutputStream::writeText (const String& text, const bool asUTF16, | |||
| const bool writeUTF16ByteOrderMark) | |||
| { | |||
| if (asUTF16) | |||
| @@ -201,7 +204,9 @@ void OutputStream::writeText (const String& text, const bool asUTF16, | |||
| writeShort ((short) '\r'); | |||
| lastCharWasReturn = (c == L'\r'); | |||
| writeShort ((short) c); | |||
| if (! writeShort ((short) c)) | |||
| return false; | |||
| } | |||
| } | |||
| else | |||
| @@ -214,9 +219,12 @@ void OutputStream::writeText (const String& text, const bool asUTF16, | |||
| if (*t == '\n') | |||
| { | |||
| if (t > src) | |||
| write (src, (int) (t - src)); | |||
| if (! write (src, (int) (t - src))) | |||
| return false; | |||
| if (! write ("\r\n", 2)) | |||
| return false; | |||
| write ("\r\n", 2); | |||
| src = t + 1; | |||
| } | |||
| else if (*t == '\r') | |||
| @@ -227,7 +235,8 @@ void OutputStream::writeText (const String& text, const bool asUTF16, | |||
| else if (*t == 0) | |||
| { | |||
| if (t > src) | |||
| write (src, (int) (t - src)); | |||
| if (! write (src, (int) (t - src))) | |||
| return false; | |||
| break; | |||
| } | |||
| @@ -235,6 +244,8 @@ void OutputStream::writeText (const String& text, const bool asUTF16, | |||
| ++t; | |||
| } | |||
| } | |||
| return true; | |||
| } | |||
| int OutputStream::writeFromInputStream (InputStream& source, int64 numBytesToWrite) | |||
| @@ -95,75 +95,88 @@ public: | |||
| //============================================================================== | |||
| /** Writes a single byte to the stream. | |||
| @returns false if the write operation fails for some reason | |||
| @see InputStream::readByte | |||
| */ | |||
| virtual void writeByte (char byte); | |||
| virtual bool writeByte (char byte); | |||
| /** Writes a boolean to the stream as a single byte. | |||
| This is encoded as a binary byte (not as text) with a value of 1 or 0. | |||
| @returns false if the write operation fails for some reason | |||
| @see InputStream::readBool | |||
| */ | |||
| virtual void writeBool (bool boolValue); | |||
| virtual bool writeBool (bool boolValue); | |||
| /** Writes a 16-bit integer to the stream in a little-endian byte order. | |||
| This will write two bytes to the stream: (value & 0xff), then (value >> 8). | |||
| @returns false if the write operation fails for some reason | |||
| @see InputStream::readShort | |||
| */ | |||
| virtual void writeShort (short value); | |||
| virtual bool writeShort (short value); | |||
| /** Writes a 16-bit integer to the stream in a big-endian byte order. | |||
| This will write two bytes to the stream: (value >> 8), then (value & 0xff). | |||
| @returns false if the write operation fails for some reason | |||
| @see InputStream::readShortBigEndian | |||
| */ | |||
| virtual void writeShortBigEndian (short value); | |||
| virtual bool writeShortBigEndian (short value); | |||
| /** Writes a 32-bit integer to the stream in a little-endian byte order. | |||
| @returns false if the write operation fails for some reason | |||
| @see InputStream::readInt | |||
| */ | |||
| virtual void writeInt (int value); | |||
| virtual bool writeInt (int value); | |||
| /** Writes a 32-bit integer to the stream in a big-endian byte order. | |||
| @returns false if the write operation fails for some reason | |||
| @see InputStream::readIntBigEndian | |||
| */ | |||
| virtual void writeIntBigEndian (int value); | |||
| virtual bool writeIntBigEndian (int value); | |||
| /** Writes a 64-bit integer to the stream in a little-endian byte order. | |||
| @returns false if the write operation fails for some reason | |||
| @see InputStream::readInt64 | |||
| */ | |||
| virtual void writeInt64 (int64 value); | |||
| virtual bool writeInt64 (int64 value); | |||
| /** Writes a 64-bit integer to the stream in a big-endian byte order. | |||
| @returns false if the write operation fails for some reason | |||
| @see InputStream::readInt64BigEndian | |||
| */ | |||
| virtual void writeInt64BigEndian (int64 value); | |||
| virtual bool writeInt64BigEndian (int64 value); | |||
| /** Writes a 32-bit floating point value to the stream in a binary format. | |||
| The binary 32-bit encoding of the float is written as a little-endian int. | |||
| @returns false if the write operation fails for some reason | |||
| @see InputStream::readFloat | |||
| */ | |||
| virtual void writeFloat (float value); | |||
| virtual bool writeFloat (float value); | |||
| /** Writes a 32-bit floating point value to the stream in a binary format. | |||
| The binary 32-bit encoding of the float is written as a big-endian int. | |||
| @returns false if the write operation fails for some reason | |||
| @see InputStream::readFloatBigEndian | |||
| */ | |||
| virtual void writeFloatBigEndian (float value); | |||
| virtual bool writeFloatBigEndian (float value); | |||
| /** Writes a 64-bit floating point value to the stream in a binary format. | |||
| The eight raw bytes of the double value are written out as a little-endian 64-bit int. | |||
| @returns false if the write operation fails for some reason | |||
| @see InputStream::readDouble | |||
| */ | |||
| virtual void writeDouble (double value); | |||
| virtual bool writeDouble (double value); | |||
| /** Writes a 64-bit floating point value to the stream in a binary format. | |||
| The eight raw bytes of the double value are written out as a big-endian 64-bit int. | |||
| @see InputStream::readDoubleBigEndian | |||
| @returns false if the write operation fails for some reason | |||
| */ | |||
| virtual void writeDoubleBigEndian (double value); | |||
| virtual bool writeDoubleBigEndian (double value); | |||
| /** Writes a byte to the output stream a given number of times. */ | |||
| virtual void writeRepeatedByte (uint8 byte, size_t numTimesToRepeat); | |||
| /** Writes a byte to the output stream a given number of times. | |||
| @returns false if the write operation fails for some reason | |||
| */ | |||
| virtual bool writeRepeatedByte (uint8 byte, size_t numTimesToRepeat); | |||
| /** Writes a condensed binary encoding of a 32-bit integer. | |||
| @@ -173,9 +186,10 @@ public: | |||
| The format used is: number of significant bytes + up to 4 bytes in little-endian order. | |||
| @returns false if the write operation fails for some reason | |||
| @see InputStream::readCompressedInt | |||
| */ | |||
| virtual void writeCompressedInt (int value); | |||
| virtual bool writeCompressedInt (int value); | |||
| /** Stores a string in the stream in a binary format. | |||
| @@ -187,9 +201,10 @@ public: | |||
| For appending text to a file, instead use writeText, or operator<< | |||
| @returns false if the write operation fails for some reason | |||
| @see InputStream::readString, writeText, operator<< | |||
| */ | |||
| virtual void writeString (const String& text); | |||
| virtual bool writeString (const String& text); | |||
| /** Writes a string of text to the stream. | |||
| @@ -198,8 +213,9 @@ public: | |||
| of a file). | |||
| The method also replaces '\\n' characters in the text with '\\r\\n'. | |||
| @returns false if the write operation fails for some reason | |||
| */ | |||
| virtual void writeText (const String& text, | |||
| virtual bool writeText (const String& text, | |||
| bool asUTF16, | |||
| bool writeUTF16ByteOrderMark); | |||
| @@ -209,6 +225,7 @@ public: | |||
| @param maxNumBytesToWrite the number of bytes to read from the stream (if this is | |||
| less than zero, it will keep reading until the input | |||
| is exhausted) | |||
| @returns the number of bytes written | |||
| */ | |||
| virtual int writeFromInputStream (InputStream& source, int64 maxNumBytesToWrite); | |||
| @@ -78,9 +78,9 @@ public: | |||
| */ | |||
| void flush(); | |||
| int64 getPosition(); | |||
| bool setPosition (int64 newPosition); | |||
| bool write (const void* destBuffer, size_t howMany); | |||
| int64 getPosition() override; | |||
| bool setPosition (int64) override; | |||
| bool write (const void*, size_t) override; | |||
| /** These are preset values that can be used for the constructor's windowBits paramter. | |||
| For more info about this, see the zlib documentation for its windowBits parameter. | |||