mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-28 03:20:11 +01:00
BinaryReader/Writer wrappers for Buffer and MemoryStream
This commit is contained in:
@@ -41,6 +41,8 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "Poco/Foundation.h"
|
#include "Poco/Foundation.h"
|
||||||
|
#include "Poco/Buffer.h"
|
||||||
|
#include "Poco/MemoryStream.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <istream>
|
#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
|
// inlines
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -41,6 +41,8 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "Poco/Foundation.h"
|
#include "Poco/Foundation.h"
|
||||||
|
#include "Poco/Buffer.h"
|
||||||
|
#include "Poco/MemoryStream.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <ostream>
|
#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
|
// inlines
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -35,11 +35,15 @@
|
|||||||
#include "CppUnit/TestSuite.h"
|
#include "CppUnit/TestSuite.h"
|
||||||
#include "Poco/BinaryWriter.h"
|
#include "Poco/BinaryWriter.h"
|
||||||
#include "Poco/BinaryReader.h"
|
#include "Poco/BinaryReader.h"
|
||||||
|
#include "Poco/Buffer.h"
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
using Poco::BinaryWriter;
|
using Poco::BinaryWriter;
|
||||||
|
using Poco::MemoryBinaryWriter;
|
||||||
using Poco::BinaryReader;
|
using Poco::BinaryReader;
|
||||||
|
using Poco::MemoryBinaryReader;
|
||||||
|
using Poco::Buffer;
|
||||||
using Poco::Int32;
|
using Poco::Int32;
|
||||||
using Poco::UInt32;
|
using Poco::UInt32;
|
||||||
using Poco::Int64;
|
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()
|
void BinaryReaderWriterTest::setUp()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -262,6 +287,7 @@ CppUnit::Test* BinaryReaderWriterTest::suite()
|
|||||||
CppUnit_addTest(pSuite, BinaryReaderWriterTest, testNative);
|
CppUnit_addTest(pSuite, BinaryReaderWriterTest, testNative);
|
||||||
CppUnit_addTest(pSuite, BinaryReaderWriterTest, testBigEndian);
|
CppUnit_addTest(pSuite, BinaryReaderWriterTest, testBigEndian);
|
||||||
CppUnit_addTest(pSuite, BinaryReaderWriterTest, testLittleEndian);
|
CppUnit_addTest(pSuite, BinaryReaderWriterTest, testLittleEndian);
|
||||||
|
CppUnit_addTest(pSuite, BinaryReaderWriterTest, testWrappers);
|
||||||
|
|
||||||
return pSuite;
|
return pSuite;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ public:
|
|||||||
void testNative();
|
void testNative();
|
||||||
void testBigEndian();
|
void testBigEndian();
|
||||||
void testLittleEndian();
|
void testLittleEndian();
|
||||||
|
void testWrappers();
|
||||||
void write(Poco::BinaryWriter& writer);
|
void write(Poco::BinaryWriter& writer);
|
||||||
void read(Poco::BinaryReader& reader);
|
void read(Poco::BinaryReader& reader);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user