From 22575c3a90fe84f56d3ed66bfa51de6d9fc3d8e8 Mon Sep 17 00:00:00 2001 From: Marian Krivos Date: Tue, 23 Aug 2011 06:38:04 +0000 Subject: [PATCH] trunk/branch integration: optimalization --- Foundation/include/Poco/Base64Decoder.h | 38 ++++++++----- Foundation/include/Poco/Base64Encoder.h | 42 ++++++++------ Foundation/include/Poco/BinaryReader.h | 73 +++++++++++++++++------- Foundation/include/Poco/BinaryWriter.h | 74 +++++++++++++++++-------- 4 files changed, 156 insertions(+), 71 deletions(-) diff --git a/Foundation/include/Poco/Base64Decoder.h b/Foundation/include/Poco/Base64Decoder.h index f8815534d..cc59c3ee7 100644 --- a/Foundation/include/Poco/Base64Decoder.h +++ b/Foundation/include/Poco/Base64Decoder.h @@ -49,24 +49,30 @@ namespace Poco { class Foundation_API Base64DecoderBuf: public UnbufferedStreamBuf - /// This streambuf base64-decodes all data read - /// from the istream connected to it. + /// This streambuf base64-decodes all data 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: - Base64DecoderBuf(std::istream& istr); + Base64DecoderBuf(std::istream& istr); ~Base64DecoderBuf(); private: int readFromDevice(); int readOne(); - unsigned char _group[3]; - int _groupLength; - int _groupIndex; - std::istream& _istr; - - static unsigned char IN_ENCODING[256]; - static bool IN_ENCODING_INIT; + unsigned char _group[3]; + int _groupLength; + int _groupIndex; + std::streambuf& _buf; + + static unsigned char IN_ENCODING[256]; + static bool IN_ENCODING_INIT; private: Base64DecoderBuf(const Base64DecoderBuf&); @@ -95,11 +101,17 @@ private: class Foundation_API Base64Decoder: public Base64DecoderIOS, public std::istream - /// This istream base64-decodes all data - /// read from the istream connected to it. + /// This istream base64-decodes all data + /// 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: - Base64Decoder(std::istream& istr); + Base64Decoder(std::istream& istr); ~Base64Decoder(); private: diff --git a/Foundation/include/Poco/Base64Encoder.h b/Foundation/include/Poco/Base64Encoder.h index 9bf331456..569773c39 100644 --- a/Foundation/include/Poco/Base64Encoder.h +++ b/Foundation/include/Poco/Base64Encoder.h @@ -49,12 +49,17 @@ namespace Poco { class Foundation_API Base64EncoderBuf: public UnbufferedStreamBuf - /// This streambuf base64-encodes all data written - /// to it and forwards it to a connected - /// ostream. + /// This streambuf base64-encodes all data written + /// to it and forwards it to a connected + /// 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: - Base64EncoderBuf(std::ostream& ostr); + Base64EncoderBuf(std::ostream& ostr); ~Base64EncoderBuf(); int close(); @@ -74,14 +79,14 @@ public: private: int writeToDevice(char c); - unsigned char _group[3]; - int _groupLength; - int _pos; - int _lineLength; - std::ostream& _ostr; - - static const unsigned char OUT_ENCODING[64]; - + unsigned char _group[3]; + int _groupLength; + int _pos; + int _lineLength; + std::streambuf& _buf; + + static const unsigned char OUT_ENCODING[64]; + friend class Base64DecoderBuf; Base64EncoderBuf(const Base64EncoderBuf&); @@ -114,12 +119,17 @@ class Foundation_API Base64Encoder: public Base64EncoderIOS, public std::ostream /// This ostream base64-encodes all data /// written to it and forwards it to /// a connected ostream. - /// Always call close() when done - /// writing data, to ensure proper - /// completion of the encoding operation. + /// Always call close() when done + /// writing data, to ensure proper + /// 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: - Base64Encoder(std::ostream& ostr); + Base64Encoder(std::ostream& ostr); ~Base64Encoder(); private: diff --git a/Foundation/include/Poco/BinaryReader.h b/Foundation/include/Poco/BinaryReader.h index 6c75a3da4..f4a8dacf9 100644 --- a/Foundation/include/Poco/BinaryReader.h +++ b/Foundation/include/Poco/BinaryReader.h @@ -41,17 +41,23 @@ #include "Poco/Foundation.h" +#include #include namespace Poco { +class TextEncoding; +class TextConverter; + + class Foundation_API BinaryReader - /// This class reads basic types in binary form into an input stream. - /// It provides an extractor-based interface similar to istream. - /// The reader also supports automatic conversion from big-endian - /// (network byte order) to little-endian and vice-versa. + /// This class reads basic types (and std::vectors thereof) + /// in binary form into an input stream. + /// It provides an extractor-based interface similar to istream. + /// 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. { public: @@ -64,11 +70,17 @@ public: 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); - /// Creates the BinaryReader. + BinaryReader(std::istream& istr, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER); + /// Creates the BinaryReader. - ~BinaryReader(); - /// Destroys the BinaryReader. + BinaryReader(std::istream& istr, TextEncoding& encoding, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER); + /// 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 >> (char& value); @@ -88,26 +100,46 @@ public: BinaryReader& operator >> (UInt64& value); #endif - BinaryReader& operator >> (std::string& value); + BinaryReader& operator >> (std::string& value); - void read7BitEncoded(UInt32& value); - /// Reads a 32-bit unsigned integer in compressed format. - /// See BinaryWriter::write7BitEncoded() for a description + template + BinaryReader& operator >> (std::vector& value) + { + 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. #if defined(POCO_HAVE_INT64) void read7BitEncoded(UInt64& value); /// Reads a 64-bit unsigned integer in compressed format. /// See BinaryWriter::write7BitEncoded() for a description - /// of the compression algorithm. + /// of the compression algorithm. #endif - void readRaw(int length, std::string& value); - /// Reads length bytes of raw data into value. + void readRaw(std::streamsize length, std::string& value); + /// Reads length bytes of raw data into value. - void readBOM(); - /// Reads a byte-order mark from the stream and configures - /// the reader for the encountered byte order. + void readRaw(char* buffer, std::streamsize length); + /// Reads length bytes of raw data into buffer. + + 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, /// written in host byte order. @@ -131,8 +163,9 @@ public: /// either BIG_ENDIAN_BYTE_ORDER or LITTLE_ENDIAN_BYTE_ORDER. private: - std::istream& _istr; - bool _flipBytes; + std::istream& _istr; + bool _flipBytes; + TextConverter* _pTextConverter; }; diff --git a/Foundation/include/Poco/BinaryWriter.h b/Foundation/include/Poco/BinaryWriter.h index fa1890489..b2120bb45 100644 --- a/Foundation/include/Poco/BinaryWriter.h +++ b/Foundation/include/Poco/BinaryWriter.h @@ -1,7 +1,7 @@ // // BinaryWriter.h // -// $Id: //poco/svn/Foundation/include/Poco/BinaryWriter.h#2 $ +// $Id: //poco/1.4/Foundation/include/Poco/BinaryWriter.h#2 $ // // Library: Foundation // Package: Streams @@ -41,17 +41,23 @@ #include "Poco/Foundation.h" +#include #include namespace Poco { +class TextEncoding; +class TextConverter; + + class Foundation_API BinaryWriter - /// This class writes basic types in binary form into an output stream. - /// It provides an inserter-based interface similar to ostream. - /// The writer also supports automatic conversion from big-endian - /// (network byte order) to little-endian and vice-versa. + /// This class writes basic types (and std::vectors of these) + /// in binary form into an output stream. + /// It provides an inserter-based interface similar to ostream. + /// 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. /// Be careful when exchanging data between systems with different /// 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 }; - BinaryWriter(std::ostream& ostr, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER); - /// Creates the BinaryWriter. + BinaryWriter(std::ostream& ostr, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER); + /// Creates the BinaryWriter. - ~BinaryWriter(); - /// Destroys the BinaryWriter. + BinaryWriter(std::ostream& ostr, TextEncoding& encoding, StreamByteOrder byteOrder = NATIVE_BYTE_ORDER); + /// 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 << (char value); @@ -93,12 +105,26 @@ public: BinaryWriter& operator << (UInt64 value); #endif - BinaryWriter& operator << (const std::string& value); - BinaryWriter& operator << (const char* value); - - 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 + BinaryWriter& operator << (const std::string& value); + BinaryWriter& operator << (const char* value); + + template + BinaryWriter& operator << (const std::vector& value) + { + Poco::UInt32 size(static_cast(value.size())); + + *this << size; + for (typename std::vector::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. /// The high bit of a byte indicates whether there are more bytes to be /// written after this one. @@ -120,12 +146,15 @@ public: /// This process is repeated until the entire integer has been written. #endif - void writeRaw(const std::string& rawData); - /// Writes the string as-is to the stream. + void writeRaw(const std::string& rawData); + /// 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(); - /// 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. + void writeBOM(); + /// 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 BinaryReader uses the byte-order mark to determine the byte-order /// of the stream. @@ -149,8 +178,9 @@ public: /// either BIG_ENDIAN_BYTE_ORDER or LITTLE_ENDIAN_BYTE_ORDER. private: - std::ostream& _ostr; - bool _flipBytes; + std::ostream& _ostr; + bool _flipBytes; + TextConverter* _pTextConverter; };