trunk/branch integration: optimalization

This commit is contained in:
Marian Krivos
2011-08-23 06:38:04 +00:00
parent a74e3c6be7
commit 22575c3a90
4 changed files with 156 additions and 71 deletions

View File

@@ -49,24 +49,30 @@ namespace Poco {
class Foundation_API Base64DecoderBuf: public UnbufferedStreamBuf class Foundation_API Base64DecoderBuf: public UnbufferedStreamBuf
/// This streambuf base64-decodes all data read /// This streambuf base64-decodes all data read
/// from the istream connected to it. /// from the istream connected to it.
///
/// Note: For performance reasons, the characters
/// are read directly from the given istream's
/// underlying streambuf, so the state
/// of the istream will not reflect that of
/// its streambuf.
{ {
public: public:
Base64DecoderBuf(std::istream& istr); Base64DecoderBuf(std::istream& istr);
~Base64DecoderBuf(); ~Base64DecoderBuf();
private: private:
int readFromDevice(); int readFromDevice();
int readOne(); int readOne();
unsigned char _group[3]; unsigned char _group[3];
int _groupLength; int _groupLength;
int _groupIndex; int _groupIndex;
std::istream& _istr; std::streambuf& _buf;
static unsigned char IN_ENCODING[256]; static unsigned char IN_ENCODING[256];
static bool IN_ENCODING_INIT; static bool IN_ENCODING_INIT;
private: private:
Base64DecoderBuf(const Base64DecoderBuf&); Base64DecoderBuf(const Base64DecoderBuf&);
@@ -95,11 +101,17 @@ private:
class Foundation_API Base64Decoder: public Base64DecoderIOS, public std::istream class Foundation_API Base64Decoder: public Base64DecoderIOS, public std::istream
/// This istream base64-decodes all data /// This istream base64-decodes all data
/// read from the istream connected to it. /// read from the istream connected to it.
///
/// Note: For performance reasons, the characters
/// are read directly from the given istream's
/// underlying streambuf, so the state
/// of the istream will not reflect that of
/// its streambuf.
{ {
public: public:
Base64Decoder(std::istream& istr); Base64Decoder(std::istream& istr);
~Base64Decoder(); ~Base64Decoder();
private: private:

View File

@@ -49,12 +49,17 @@ namespace Poco {
class Foundation_API Base64EncoderBuf: public UnbufferedStreamBuf class Foundation_API Base64EncoderBuf: public UnbufferedStreamBuf
/// This streambuf base64-encodes all data written /// This streambuf base64-encodes all data written
/// to it and forwards it to a connected /// to it and forwards it to a connected
/// ostream. /// ostream.
///
/// Note: The characters are directly written
/// to the ostream's streambuf, thus bypassing
/// the ostream. The ostream's state is therefore
/// not updated to match the buffer's state.
{ {
public: public:
Base64EncoderBuf(std::ostream& ostr); Base64EncoderBuf(std::ostream& ostr);
~Base64EncoderBuf(); ~Base64EncoderBuf();
int close(); int close();
@@ -74,14 +79,14 @@ public:
private: private:
int writeToDevice(char c); int writeToDevice(char c);
unsigned char _group[3]; unsigned char _group[3];
int _groupLength; int _groupLength;
int _pos; int _pos;
int _lineLength; int _lineLength;
std::ostream& _ostr; std::streambuf& _buf;
static const unsigned char OUT_ENCODING[64]; static const unsigned char OUT_ENCODING[64];
friend class Base64DecoderBuf; friend class Base64DecoderBuf;
Base64EncoderBuf(const Base64EncoderBuf&); Base64EncoderBuf(const Base64EncoderBuf&);
@@ -114,12 +119,17 @@ class Foundation_API Base64Encoder: public Base64EncoderIOS, public std::ostream
/// This ostream base64-encodes all data /// This ostream base64-encodes all data
/// written to it and forwards it to /// written to it and forwards it to
/// a connected ostream. /// a connected ostream.
/// Always call close() when done /// Always call close() when done
/// writing data, to ensure proper /// writing data, to ensure proper
/// completion of the encoding operation. /// completion of the encoding operation.
///
/// Note: The characters are directly written
/// to the ostream's streambuf, thus bypassing
/// the ostream. The ostream's state is therefore
/// not updated to match the buffer's state.
{ {
public: public:
Base64Encoder(std::ostream& ostr); Base64Encoder(std::ostream& ostr);
~Base64Encoder(); ~Base64Encoder();
private: private:

View File

@@ -41,17 +41,23 @@
#include "Poco/Foundation.h" #include "Poco/Foundation.h"
#include <vector>
#include <istream> #include <istream>
namespace Poco { namespace Poco {
class TextEncoding;
class TextConverter;
class Foundation_API BinaryReader class Foundation_API BinaryReader
/// This class reads basic types in binary form into an input stream. /// This class reads basic types (and std::vectors thereof)
/// It provides an extractor-based interface similar to istream. /// in binary form into an input stream.
/// The reader also supports automatic conversion from big-endian /// It provides an extractor-based interface similar to istream.
/// (network byte order) to little-endian and vice-versa. /// The reader also supports automatic conversion from big-endian
/// (network byte order) to little-endian and vice-versa.
/// Use a BinaryWriter to create a stream suitable for a BinaryReader. /// Use a BinaryWriter to create a stream suitable for a BinaryReader.
{ {
public: public:
@@ -64,11 +70,17 @@ public:
UNSPECIFIED_BYTE_ORDER = 4 /// unknown, byte-order will be determined by reading the byte-order mark UNSPECIFIED_BYTE_ORDER = 4 /// unknown, byte-order will be determined by reading the byte-order mark
}; };
BinaryReader(std::istream& istr, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER); BinaryReader(std::istream& istr, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER);
/// Creates the BinaryReader. /// Creates the BinaryReader.
~BinaryReader(); BinaryReader(std::istream& istr, TextEncoding& encoding, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER);
/// Destroys the BinaryReader. /// Creates the BinaryReader using the given TextEncoding.
///
/// Strings will be converted from the specified encoding
/// to the currently set global encoding (see Poco::TextEncoding::global()).
~BinaryReader();
/// Destroys the BinaryReader.
BinaryReader& operator >> (bool& value); BinaryReader& operator >> (bool& value);
BinaryReader& operator >> (char& value); BinaryReader& operator >> (char& value);
@@ -88,26 +100,46 @@ public:
BinaryReader& operator >> (UInt64& value); BinaryReader& operator >> (UInt64& value);
#endif #endif
BinaryReader& operator >> (std::string& value); BinaryReader& operator >> (std::string& value);
void read7BitEncoded(UInt32& value); template <typename T>
/// Reads a 32-bit unsigned integer in compressed format. BinaryReader& operator >> (std::vector<T>& value)
/// See BinaryWriter::write7BitEncoded() for a description {
Poco::UInt32 size(0);
T elem;
*this >> size;
if (!good()) return *this;
value.reserve(size);
while (this->good() && size-- > 0)
{
*this >> elem;
value.push_back(elem);
}
return *this;
}
void read7BitEncoded(UInt32& value);
/// Reads a 32-bit unsigned integer in compressed format.
/// See BinaryWriter::write7BitEncoded() for a description
/// of the compression algorithm. /// of the compression algorithm.
#if defined(POCO_HAVE_INT64) #if defined(POCO_HAVE_INT64)
void read7BitEncoded(UInt64& value); void read7BitEncoded(UInt64& value);
/// Reads a 64-bit unsigned integer in compressed format. /// Reads a 64-bit unsigned integer in compressed format.
/// See BinaryWriter::write7BitEncoded() for a description /// See BinaryWriter::write7BitEncoded() for a description
/// of the compression algorithm. /// of the compression algorithm.
#endif #endif
void readRaw(int length, std::string& value); void readRaw(std::streamsize length, std::string& value);
/// Reads length bytes of raw data into value. /// Reads length bytes of raw data into value.
void readBOM(); void readRaw(char* buffer, std::streamsize length);
/// Reads a byte-order mark from the stream and configures /// Reads length bytes of raw data into buffer.
/// the reader for the encountered byte order.
void readBOM();
/// Reads a byte-order mark from the stream and configures
/// the reader for the encountered byte order.
/// A byte-order mark is a 16-bit integer with a value of 0xFEFF, /// A byte-order mark is a 16-bit integer with a value of 0xFEFF,
/// written in host byte order. /// written in host byte order.
@@ -131,8 +163,9 @@ public:
/// either BIG_ENDIAN_BYTE_ORDER or LITTLE_ENDIAN_BYTE_ORDER. /// either BIG_ENDIAN_BYTE_ORDER or LITTLE_ENDIAN_BYTE_ORDER.
private: private:
std::istream& _istr; std::istream& _istr;
bool _flipBytes; bool _flipBytes;
TextConverter* _pTextConverter;
}; };

View File

@@ -1,7 +1,7 @@
// //
// BinaryWriter.h // BinaryWriter.h
// //
// $Id: //poco/svn/Foundation/include/Poco/BinaryWriter.h#2 $ // $Id: //poco/1.4/Foundation/include/Poco/BinaryWriter.h#2 $
// //
// Library: Foundation // Library: Foundation
// Package: Streams // Package: Streams
@@ -41,17 +41,23 @@
#include "Poco/Foundation.h" #include "Poco/Foundation.h"
#include <vector>
#include <ostream> #include <ostream>
namespace Poco { namespace Poco {
class TextEncoding;
class TextConverter;
class Foundation_API BinaryWriter class Foundation_API BinaryWriter
/// This class writes basic types in binary form into an output stream. /// This class writes basic types (and std::vectors of these)
/// It provides an inserter-based interface similar to ostream. /// in binary form into an output stream.
/// The writer also supports automatic conversion from big-endian /// It provides an inserter-based interface similar to ostream.
/// (network byte order) to little-endian and vice-versa. /// The writer also supports automatic conversion from big-endian
/// (network byte order) to little-endian and vice-versa.
/// Use a BinaryReader to read from a stream created by a BinaryWriter. /// Use a BinaryReader to read from a stream created by a BinaryWriter.
/// Be careful when exchanging data between systems with different /// Be careful when exchanging data between systems with different
/// data type sizes (e.g., 32-bit and 64-bit architectures), as the sizes /// data type sizes (e.g., 32-bit and 64-bit architectures), as the sizes
@@ -69,11 +75,17 @@ public:
LITTLE_ENDIAN_BYTE_ORDER = 3 /// little-endian byte-order LITTLE_ENDIAN_BYTE_ORDER = 3 /// little-endian byte-order
}; };
BinaryWriter(std::ostream& ostr, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER); BinaryWriter(std::ostream& ostr, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER);
/// Creates the BinaryWriter. /// Creates the BinaryWriter.
~BinaryWriter(); BinaryWriter(std::ostream& ostr, TextEncoding& encoding, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER);
/// Destroys the BinaryWriter. /// Creates the BinaryWriter using the given TextEncoding.
///
/// Strings will be converted from the currently set global encoding
/// (see Poco::TextEncoding::global()) to the specified encoding.
~BinaryWriter();
/// Destroys the BinaryWriter.
BinaryWriter& operator << (bool value); BinaryWriter& operator << (bool value);
BinaryWriter& operator << (char value); BinaryWriter& operator << (char value);
@@ -93,12 +105,26 @@ public:
BinaryWriter& operator << (UInt64 value); BinaryWriter& operator << (UInt64 value);
#endif #endif
BinaryWriter& operator << (const std::string& value); BinaryWriter& operator << (const std::string& value);
BinaryWriter& operator << (const char* value); BinaryWriter& operator << (const char* value);
void write7BitEncoded(UInt32 value); template <typename T>
/// Writes a 32-bit unsigned integer in a compressed format. BinaryWriter& operator << (const std::vector<T>& value)
/// The value is written out seven bits at a time, starting {
Poco::UInt32 size(static_cast<Poco::UInt32>(value.size()));
*this << size;
for (typename std::vector<T>::const_iterator it = value.begin(); it != value.end(); ++it)
{
*this << *it;
}
return *this;
}
void write7BitEncoded(UInt32 value);
/// Writes a 32-bit unsigned integer in a compressed format.
/// The value is written out seven bits at a time, starting
/// with the seven least-significant bits. /// with the seven least-significant bits.
/// The high bit of a byte indicates whether there are more bytes to be /// The high bit of a byte indicates whether there are more bytes to be
/// written after this one. /// written after this one.
@@ -120,12 +146,15 @@ public:
/// This process is repeated until the entire integer has been written. /// This process is repeated until the entire integer has been written.
#endif #endif
void writeRaw(const std::string& rawData); void writeRaw(const std::string& rawData);
/// Writes the string as-is to the stream. /// Writes the string as-is to the stream.
void writeRaw(const char* buffer, std::streamsize length);
/// Writes length raw bytes from the given buffer to the stream.
void writeBOM(); void writeBOM();
/// Writes a byte-order mark to the stream. A byte order mark is /// Writes a byte-order mark to the stream. A byte order mark is
/// a 16-bit integer with a value of 0xFEFF, written in host byte-order. /// a 16-bit integer with a value of 0xFEFF, written in host byte-order.
/// A BinaryReader uses the byte-order mark to determine the byte-order /// A BinaryReader uses the byte-order mark to determine the byte-order
/// of the stream. /// of the stream.
@@ -149,8 +178,9 @@ public:
/// either BIG_ENDIAN_BYTE_ORDER or LITTLE_ENDIAN_BYTE_ORDER. /// either BIG_ENDIAN_BYTE_ORDER or LITTLE_ENDIAN_BYTE_ORDER.
private: private:
std::ostream& _ostr; std::ostream& _ostr;
bool _flipBytes; bool _flipBytes;
TextConverter* _pTextConverter;
}; };