Browse Source

Added return values for various OutputStream::writeXYZ methods.

tags/2021-05-28
jules 12 years ago
parent
commit
67740c1441
7 changed files with 102 additions and 73 deletions
  1. +4
    -5
      modules/juce_core/files/juce_FileOutputStream.cpp
  2. +5
    -5
      modules/juce_core/files/juce_FileOutputStream.h
  3. +3
    -1
      modules/juce_core/streams/juce_MemoryOutputStream.cpp
  4. +5
    -5
      modules/juce_core/streams/juce_MemoryOutputStream.h
  5. +47
    -36
      modules/juce_core/streams/juce_OutputStream.cpp
  6. +35
    -18
      modules/juce_core/streams/juce_OutputStream.h
  7. +3
    -3
      modules/juce_core/zip/juce_GZIPCompressorOutputStream.h

+ 4
- 5
modules/juce_core/files/juce_FileOutputStream.cpp View File

@@ -119,7 +119,7 @@ bool FileOutputStream::write (const void* const src, const size_t numBytes)
return true; return true;
} }
void FileOutputStream::writeRepeatedByte (uint8 byte, size_t numBytes)
bool FileOutputStream::writeRepeatedByte (uint8 byte, size_t numBytes)
{ {
jassert (((ssize_t) numBytes) >= 0); jassert (((ssize_t) numBytes) >= 0);
@@ -128,9 +128,8 @@ void FileOutputStream::writeRepeatedByte (uint8 byte, size_t numBytes)
memset (buffer + bytesInBuffer, byte, numBytes); memset (buffer + bytesInBuffer, byte, numBytes);
bytesInBuffer += numBytes; bytesInBuffer += numBytes;
currentPosition += numBytes; currentPosition += numBytes;
return true;
} }
else
{
OutputStream::writeRepeatedByte (byte, numBytes);
}
return OutputStream::writeRepeatedByte (byte, numBytes);
} }

+ 5
- 5
modules/juce_core/files/juce_FileOutputStream.h View File

@@ -90,11 +90,11 @@ public:
Result truncate(); 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: private:


+ 3
- 1
modules/juce_core/streams/juce_MemoryOutputStream.cpp View File

@@ -95,10 +95,12 @@ bool MemoryOutputStream::write (const void* const buffer, size_t howMany)
return true; return true;
} }
void MemoryOutputStream::writeRepeatedByte (uint8 byte, size_t howMany)
bool MemoryOutputStream::writeRepeatedByte (uint8 byte, size_t howMany)
{ {
if (howMany > 0) if (howMany > 0)
memset (prepareToWrite (howMany), byte, howMany); memset (prepareToWrite (howMany), byte, howMany);
return true;
} }
void MemoryOutputStream::appendUTF8Char (juce_wchar c) void MemoryOutputStream::appendUTF8Char (juce_wchar c)


+ 5
- 5
modules/juce_core/streams/juce_MemoryOutputStream.h View File

@@ -111,11 +111,11 @@ public:
*/ */
void flush(); 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: private:
//============================================================================== //==============================================================================


+ 47
- 36
modules/juce_core/streams/juce_OutputStream.cpp View File

@@ -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) 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); 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); 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); 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); 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 un = (value < 0) ? (unsigned int) -value
: (unsigned int) value; : (unsigned int) value;
@@ -126,60 +129,60 @@ void OutputStream::writeCompressedInt (int value)
if (value < 0) if (value < 0)
data[0] |= 0x80; 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); 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); 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; union { int asInt; float asFloat; } n;
n.asFloat = value; 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; union { int asInt; float asFloat; } n;
n.asFloat = value; 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; union { int64 asInt; double asDouble; } n;
n.asDouble = value; 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; union { int64 asInt; double asDouble; } n;
n.asDouble = value; 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 // (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). // if lots of large, persistent strings were to be written to streams).
const size_t numBytes = text.getNumBytesAsUTF8() + 1; const size_t numBytes = text.getNumBytesAsUTF8() + 1;
HeapBlock<char> temp (numBytes); HeapBlock<char> temp (numBytes);
text.copyToUTF8 (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) const bool writeUTF16ByteOrderMark)
{ {
if (asUTF16) if (asUTF16)
@@ -201,7 +204,9 @@ void OutputStream::writeText (const String& text, const bool asUTF16,
writeShort ((short) '\r'); writeShort ((short) '\r');
lastCharWasReturn = (c == L'\r'); lastCharWasReturn = (c == L'\r');
writeShort ((short) c);
if (! writeShort ((short) c))
return false;
} }
} }
else else
@@ -214,9 +219,12 @@ void OutputStream::writeText (const String& text, const bool asUTF16,
if (*t == '\n') if (*t == '\n')
{ {
if (t > src) 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; src = t + 1;
} }
else if (*t == '\r') else if (*t == '\r')
@@ -227,7 +235,8 @@ void OutputStream::writeText (const String& text, const bool asUTF16,
else if (*t == 0) else if (*t == 0)
{ {
if (t > src) if (t > src)
write (src, (int) (t - src));
if (! write (src, (int) (t - src)))
return false;
break; break;
} }
@@ -235,6 +244,8 @@ void OutputStream::writeText (const String& text, const bool asUTF16,
++t; ++t;
} }
} }
return true;
} }
int OutputStream::writeFromInputStream (InputStream& source, int64 numBytesToWrite) int OutputStream::writeFromInputStream (InputStream& source, int64 numBytesToWrite)


+ 35
- 18
modules/juce_core/streams/juce_OutputStream.h View File

@@ -95,75 +95,88 @@ public:
//============================================================================== //==============================================================================
/** Writes a single byte to the stream. /** Writes a single byte to the stream.
@returns false if the write operation fails for some reason
@see InputStream::readByte @see InputStream::readByte
*/ */
virtual void writeByte (char byte);
virtual bool writeByte (char byte);
/** Writes a boolean to the stream as a single 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. 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 @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. /** 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). 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 @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. /** 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). 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 @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. /** 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 @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. /** 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 @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. /** 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 @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. /** 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 @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. /** 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. 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 @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. /** 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. 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 @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. /** 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. 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 @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. /** 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. The eight raw bytes of the double value are written out as a big-endian 64-bit int.
@see InputStream::readDoubleBigEndian @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. /** 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. 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 @see InputStream::readCompressedInt
*/ */
virtual void writeCompressedInt (int value);
virtual bool writeCompressedInt (int value);
/** Stores a string in the stream in a binary format. /** 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<< 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<< @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. /** Writes a string of text to the stream.
@@ -198,8 +213,9 @@ public:
of a file). of a file).
The method also replaces '\\n' characters in the text with '\\r\\n'. 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 asUTF16,
bool writeUTF16ByteOrderMark); bool writeUTF16ByteOrderMark);
@@ -209,6 +225,7 @@ public:
@param maxNumBytesToWrite the number of bytes to read from the stream (if this is @param maxNumBytesToWrite the number of bytes to read from the stream (if this is
less than zero, it will keep reading until the input less than zero, it will keep reading until the input
is exhausted) is exhausted)
@returns the number of bytes written
*/ */
virtual int writeFromInputStream (InputStream& source, int64 maxNumBytesToWrite); virtual int writeFromInputStream (InputStream& source, int64 maxNumBytesToWrite);


+ 3
- 3
modules/juce_core/zip/juce_GZIPCompressorOutputStream.h View File

@@ -78,9 +78,9 @@ public:
*/ */
void flush(); 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. /** 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. For more info about this, see the zlib documentation for its windowBits parameter.


Loading…
Cancel
Save