BinaryReader/Writer wrappers for Buffer and MemoryStream

This commit is contained in:
Aleksandar Fabijanic 2012-07-23 00:39:09 +00:00
parent 2ebe065a37
commit 0466c67ff2
4 changed files with 107 additions and 0 deletions

View File

@ -41,6 +41,8 @@
#include "Poco/Foundation.h"
#include "Poco/Buffer.h"
#include "Poco/MemoryStream.h"
#include <vector>
#include <istream>
@ -169,6 +171,44 @@ private:
};
template <typename T>
class BasicMemoryBinaryReader : public BinaryReader
/// A convenient wrapper for using Buffer and MemoryStream with BinaryReader.
{
public:
BasicMemoryBinaryReader(Buffer<T>& data, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER):
BinaryReader(_istr, byteOrder),
_data(data),
_istr(data.begin(), data.size())
{
}
BasicMemoryBinaryReader(Buffer<T>& data, TextEncoding& encoding, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER):
BinaryReader(_istr, encoding, byteOrder),
_data(data),
_istr(data.begin(), data.size())
{
}
Buffer<T>& data()
{
return _data;
}
const Buffer<T>& data() const
{
return _data;
}
private:
Buffer<T>& _data;
MemoryInputStream _istr;
};
typedef BasicMemoryBinaryReader<char> MemoryBinaryReader;
//
// inlines
//

View File

@ -41,6 +41,8 @@
#include "Poco/Foundation.h"
#include "Poco/Buffer.h"
#include "Poco/MemoryStream.h"
#include <vector>
#include <ostream>
@ -184,6 +186,44 @@ private:
};
template <typename T>
class BasicMemoryBinaryWriter : public BinaryWriter
/// A convenient wrapper for using Buffer and MemoryStream with BinarWriter.
{
public:
BasicMemoryBinaryWriter(Buffer<T>& data, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER):
BinaryWriter(_ostr, byteOrder),
_data(data),
_ostr(data.begin(), data.size())
{
}
BasicMemoryBinaryWriter(Buffer<T>& data, TextEncoding& encoding, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER):
BinaryWriter(_ostr, encoding, byteOrder),
_data(data),
_ostr(data.begin(), data.size())
{
}
Buffer<T>& data()
{
return _data;
}
const Buffer<T>& data() const
{
return _data;
}
private:
Buffer<T>& _data;
MemoryOutputStream _ostr;
};
typedef BasicMemoryBinaryWriter<char> MemoryBinaryWriter;
//
// inlines
//

View File

@ -35,11 +35,15 @@
#include "CppUnit/TestSuite.h"
#include "Poco/BinaryWriter.h"
#include "Poco/BinaryReader.h"
#include "Poco/Buffer.h"
#include <sstream>
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<char> 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;
}

View File

@ -51,6 +51,7 @@ public:
void testNative();
void testBigEndian();
void testLittleEndian();
void testWrappers();
void write(Poco::BinaryWriter& writer);
void read(Poco::BinaryReader& reader);