Add return values to OutputStream methods

This commit is contained in:
Vinnie Falco
2013-07-16 06:15:18 -07:00
parent e2fd51c282
commit a90ea340d2
7 changed files with 111 additions and 86 deletions

View File

@@ -114,7 +114,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)
{
bassert (((ssize_t) numBytes) >= 0);
@@ -123,9 +123,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);
}

View File

@@ -27,7 +27,6 @@
#include "beast_File.h"
#include "../streams/beast_OutputStream.h"
//==============================================================================
/**
An output stream that writes into a local file.
@@ -87,11 +86,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:
@@ -111,4 +110,4 @@ private:
ssize_t writeInternal (const void*, size_t);
};
#endif // BEAST_FILEOUTPUTSTREAM_BEASTHEADER
#endif

View File

@@ -90,10 +90,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 (beast_wchar c)

View File

@@ -28,7 +28,6 @@
#include "../memory/beast_MemoryBlock.h"
#include "../memory/beast_ScopedPointer.h"
//==============================================================================
/**
Writes data to an internal memory buffer, which grows as required.
@@ -36,9 +35,10 @@
The data that was written into the stream can then be accessed later as
a contiguous block of memory.
*/
class BEAST_API MemoryOutputStream
class BEAST_API MemoryOutputStream
: public OutputStream
, LeakChecked <MemoryOutputStream>
, Uncopyable
{
public:
//==============================================================================
@@ -108,11 +108,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:
//==============================================================================
@@ -128,4 +128,4 @@ private:
OutputStream& BEAST_CALLTYPE operator<< (OutputStream& stream, const MemoryOutputStream& streamToRead);
#endif // BEAST_MEMORYOUTPUTSTREAM_BEASTHEADER
#endif

View File

@@ -61,48 +61,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;
@@ -121,60 +124,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)
@@ -196,7 +199,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
@@ -209,9 +214,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')
@@ -222,7 +230,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;
}
@@ -230,6 +239,8 @@ void OutputStream::writeText (const String& text, const bool asUTF16,
++t;
}
}
return true;
}
int OutputStream::writeFromInputStream (InputStream& source, int64 numBytesToWrite)

View File

@@ -40,9 +40,7 @@ class File;
@see InputStream, MemoryOutputStream, FileOutputStream
*/
class BEAST_API OutputStream
: public Uncopyable
, LeakChecked <OutputStream>
class BEAST_API OutputStream
{
protected:
//==============================================================================
@@ -92,75 +90,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.
@@ -170,9 +181,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.
@@ -184,9 +196,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.
@@ -195,8 +208,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);
@@ -206,6 +220,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);
@@ -258,5 +273,4 @@ BEAST_API OutputStream& BEAST_CALLTYPE operator<< (OutputStream& stream, InputSt
*/
BEAST_API OutputStream& BEAST_CALLTYPE operator<< (OutputStream& stream, const NewLine&);
#endif // BEAST_OUTPUTSTREAM_BEASTHEADER
#endif

View File

@@ -80,9 +80,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.