SF#579 Add EOF mark detect on reading (Note: only partially accepted, see SF tracker for details)

This commit is contained in:
Aleksandar Fabijanic
2012-09-23 17:45:39 +00:00
parent bb1e8c1852
commit fa4ca5e18e
2 changed files with 32 additions and 9 deletions

View File

@@ -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<T>& data, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER):
BasicMemoryBinaryReader(const Buffer<T>& data, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER):
BinaryReader(_istr, byteOrder),
_data(data),
_istr(data.begin(), data.capacity())
{
}
BasicMemoryBinaryReader(Buffer<T>& data, TextEncoding& encoding, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER):
BasicMemoryBinaryReader(const Buffer<T>& 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<T>& data()
{
return _data;
}
const Buffer<T>& data() const
{
return _data;
@@ -215,7 +216,7 @@ public:
}
private:
Buffer<T>& _data;
const Buffer<T>& _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

View File

@@ -252,7 +252,7 @@ void BinaryReaderWriterTest::read(BinaryReader& reader)
void BinaryReaderWriterTest::testWrappers()
{
bool b = false; char c = '0'; int i = 0;
Buffer<char> buf(64);
Buffer<char> 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&) { }
}