diff --git a/Foundation/include/Poco/BinaryReader.h b/Foundation/include/Poco/BinaryReader.h index d30f7e763..d5e0f62c7 100644 --- a/Foundation/include/Poco/BinaryReader.h +++ b/Foundation/include/Poco/BinaryReader.h @@ -41,6 +41,8 @@ #include "Poco/Foundation.h" +#include "Poco/Buffer.h" +#include "Poco/MemoryStream.h" #include #include @@ -169,6 +171,44 @@ private: }; +template +class BasicMemoryBinaryReader : public BinaryReader + /// A convenient wrapper for using Buffer and MemoryStream with BinaryReader. +{ +public: + BasicMemoryBinaryReader(Buffer& data, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER): + BinaryReader(_istr, byteOrder), + _data(data), + _istr(data.begin(), data.size()) + { + } + + BasicMemoryBinaryReader(Buffer& data, TextEncoding& encoding, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER): + BinaryReader(_istr, encoding, byteOrder), + _data(data), + _istr(data.begin(), data.size()) + { + } + + Buffer& data() + { + return _data; + } + + const Buffer& data() const + { + return _data; + } + +private: + Buffer& _data; + MemoryInputStream _istr; +}; + + +typedef BasicMemoryBinaryReader MemoryBinaryReader; + + // // inlines // diff --git a/Foundation/include/Poco/BinaryWriter.h b/Foundation/include/Poco/BinaryWriter.h index e4e4ebdaf..7011c06d6 100644 --- a/Foundation/include/Poco/BinaryWriter.h +++ b/Foundation/include/Poco/BinaryWriter.h @@ -41,6 +41,8 @@ #include "Poco/Foundation.h" +#include "Poco/Buffer.h" +#include "Poco/MemoryStream.h" #include #include @@ -184,6 +186,44 @@ private: }; +template +class BasicMemoryBinaryWriter : public BinaryWriter + /// A convenient wrapper for using Buffer and MemoryStream with BinarWriter. +{ +public: + BasicMemoryBinaryWriter(Buffer& data, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER): + BinaryWriter(_ostr, byteOrder), + _data(data), + _ostr(data.begin(), data.size()) + { + } + + BasicMemoryBinaryWriter(Buffer& data, TextEncoding& encoding, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER): + BinaryWriter(_ostr, encoding, byteOrder), + _data(data), + _ostr(data.begin(), data.size()) + { + } + + Buffer& data() + { + return _data; + } + + const Buffer& data() const + { + return _data; + } + +private: + Buffer& _data; + MemoryOutputStream _ostr; +}; + + +typedef BasicMemoryBinaryWriter MemoryBinaryWriter; + + // // inlines // diff --git a/Foundation/testsuite/src/BinaryReaderWriterTest.cpp b/Foundation/testsuite/src/BinaryReaderWriterTest.cpp index d87bd7517..351ca4119 100644 --- a/Foundation/testsuite/src/BinaryReaderWriterTest.cpp +++ b/Foundation/testsuite/src/BinaryReaderWriterTest.cpp @@ -35,11 +35,15 @@ #include "CppUnit/TestSuite.h" #include "Poco/BinaryWriter.h" #include "Poco/BinaryReader.h" +#include "Poco/Buffer.h" #include using Poco::BinaryWriter; +using Poco::MemoryBinaryWriter; using Poco::BinaryReader; +using Poco::MemoryBinaryReader; +using Poco::Buffer; using Poco::Int32; using Poco::UInt32; using Poco::Int64; @@ -245,6 +249,27 @@ void BinaryReaderWriterTest::read(BinaryReader& reader) } +void BinaryReaderWriterTest::testWrappers() +{ + bool b = false; char c = '0'; int i = 0; + Buffer buf(64); + + MemoryBinaryWriter writer(buf); + writer << true; + writer << false; + writer << 'a'; + writer << 1; + writer << -1; + + MemoryBinaryReader reader(writer.data()); + reader >> b; assert (b); + reader >> b; assert (!b); + reader >> c; assert ('a' == c); + reader >> i; assert (1 == i); + reader >> i; assert (-1 == i); +} + + void BinaryReaderWriterTest::setUp() { } @@ -262,6 +287,7 @@ CppUnit::Test* BinaryReaderWriterTest::suite() CppUnit_addTest(pSuite, BinaryReaderWriterTest, testNative); CppUnit_addTest(pSuite, BinaryReaderWriterTest, testBigEndian); CppUnit_addTest(pSuite, BinaryReaderWriterTest, testLittleEndian); + CppUnit_addTest(pSuite, BinaryReaderWriterTest, testWrappers); return pSuite; } diff --git a/Foundation/testsuite/src/BinaryReaderWriterTest.h b/Foundation/testsuite/src/BinaryReaderWriterTest.h index bd8aa2acf..f4d8dd1fb 100644 --- a/Foundation/testsuite/src/BinaryReaderWriterTest.h +++ b/Foundation/testsuite/src/BinaryReaderWriterTest.h @@ -51,6 +51,7 @@ public: void testNative(); void testBigEndian(); void testLittleEndian(); + void testWrappers(); void write(Poco::BinaryWriter& writer); void read(Poco::BinaryReader& reader);