mirror of
https://github.com/pocoproject/poco.git
synced 2025-01-18 00:15:27 +01:00
BinaryReader/Writer wrappers for Buffer and MemoryStream
This commit is contained in:
parent
2ebe065a37
commit
0466c67ff2
@ -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
|
||||
//
|
||||
|
@ -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
|
||||
//
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -51,6 +51,7 @@ public:
|
||||
void testNative();
|
||||
void testBigEndian();
|
||||
void testLittleEndian();
|
||||
void testWrappers();
|
||||
void write(Poco::BinaryWriter& writer);
|
||||
void read(Poco::BinaryReader& reader);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user