diff --git a/Foundation/include/Poco/BinaryReader.h b/Foundation/include/Poco/BinaryReader.h index 3454741be..2318f9b87 100644 --- a/Foundation/include/Poco/BinaryReader.h +++ b/Foundation/include/Poco/BinaryReader.h @@ -164,6 +164,12 @@ public: /// Returns the byte-order used by the reader, which is /// either BIG_ENDIAN_BYTE_ORDER or LITTLE_ENDIAN_BYTE_ORDER. + void setExceptions(std::ios_base::iostate st = (std::istream::failbit | std::istream::badbit)); + /// Sets the stream to throw exception on specified state (default failbit and badbit); + + std::streamsize available() const; + /// Returns the number of available bytes in the stream. + private: std::istream& _istr; bool _flipBytes; @@ -176,14 +182,14 @@ class BasicMemoryBinaryReader : public BinaryReader /// A convenient wrapper for using Buffer and MemoryStream with BinaryReader. { public: - BasicMemoryBinaryReader(Buffer& data, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER): + BasicMemoryBinaryReader(const Buffer& data, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER): BinaryReader(_istr, byteOrder), _data(data), _istr(data.begin(), data.capacity()) { } - BasicMemoryBinaryReader(Buffer& data, TextEncoding& encoding, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER): + BasicMemoryBinaryReader(const Buffer& data, TextEncoding& encoding, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER): BinaryReader(_istr, encoding, byteOrder), _data(data), _istr(data.begin(), data.capacity()) @@ -194,11 +200,6 @@ public: { } - Buffer& data() - { - return _data; - } - const Buffer& data() const { return _data; @@ -215,7 +216,7 @@ public: } private: - Buffer& _data; + const Buffer& _data; MemoryInputStream _istr; }; @@ -268,6 +269,18 @@ inline BinaryReader::StreamByteOrder BinaryReader::byteOrder() const } +inline void BinaryReader::setExceptions(std::ios_base::iostate st) +{ + _istr.exceptions(st); +} + + +inline std::streamsize BinaryReader::available() const +{ + return _istr.rdbuf()->in_avail(); +} + + } // namespace Poco diff --git a/Foundation/testsuite/src/BinaryReaderWriterTest.cpp b/Foundation/testsuite/src/BinaryReaderWriterTest.cpp index 351ca4119..17efb046f 100644 --- a/Foundation/testsuite/src/BinaryReaderWriterTest.cpp +++ b/Foundation/testsuite/src/BinaryReaderWriterTest.cpp @@ -252,7 +252,7 @@ void BinaryReaderWriterTest::read(BinaryReader& reader) void BinaryReaderWriterTest::testWrappers() { bool b = false; char c = '0'; int i = 0; - Buffer buf(64); + Buffer buf(2 * sizeof(bool) + sizeof(char) + 2 * sizeof(int)); MemoryBinaryWriter writer(buf); writer << true; @@ -265,8 +265,18 @@ void BinaryReaderWriterTest::testWrappers() reader >> b; assert (b); reader >> b; assert (!b); reader >> c; assert ('a' == c); + assert(reader.available() == sizeof(i) * 2); reader >> i; assert (1 == i); + assert(reader.available() == sizeof(i)); reader >> i; assert (-1 == i); + assert(reader.available() == 0); + + reader.setExceptions(std::istream::eofbit); + try + { + reader >> i; + fail ("must throw on EOF"); + } catch(std::exception&) { } }