trunk/branch integration: optimalization

This commit is contained in:
Marian Krivos
2011-08-23 06:38:28 +00:00
parent 22575c3a90
commit dd89dc0da6
4 changed files with 200 additions and 115 deletions

View File

@@ -1,7 +1,7 @@
// //
// Base64Decoder.cpp // Base64Decoder.cpp
// //
// $Id: //poco/svn/Foundation/src/Base64Decoder.cpp#2 $ // $Id: //poco/1.4/Foundation/src/Base64Decoder.cpp#2 $
// //
// Library: Foundation // Library: Foundation
// Package: Streams // Package: Streams
@@ -47,12 +47,17 @@ unsigned char Base64DecoderBuf::IN_ENCODING[256];
bool Base64DecoderBuf::IN_ENCODING_INIT = false; bool Base64DecoderBuf::IN_ENCODING_INIT = false;
namespace
{
static FastMutex mutex;
}
Base64DecoderBuf::Base64DecoderBuf(std::istream& istr): Base64DecoderBuf::Base64DecoderBuf(std::istream& istr):
_groupLength(0), _groupLength(0),
_groupIndex(0), _groupIndex(0),
_istr(istr) _buf(*istr.rdbuf())
{ {
static FastMutex mutex;
FastMutex::ScopedLock lock(mutex); FastMutex::ScopedLock lock(mutex);
if (!IN_ENCODING_INIT) if (!IN_ENCODING_INIT)
{ {
@@ -88,13 +93,13 @@ int Base64DecoderBuf::readFromDevice()
if ((c = readOne()) == -1) return -1; if ((c = readOne()) == -1) return -1;
buffer[0] = (unsigned char) c; buffer[0] = (unsigned char) c;
if (IN_ENCODING[buffer[0]] == 0xFF) throw DataFormatException(); if (IN_ENCODING[buffer[0]] == 0xFF) throw DataFormatException();
if ((c = readOne()) == -1) return -1; if ((c = readOne()) == -1) throw DataFormatException();
buffer[1] = (unsigned char) c; buffer[1] = (unsigned char) c;
if (IN_ENCODING[buffer[1]] == 0xFF) throw DataFormatException(); if (IN_ENCODING[buffer[1]] == 0xFF) throw DataFormatException();
if ((c = readOne()) == -1) return -1; if ((c = readOne()) == -1) throw DataFormatException();
buffer[2] = c; buffer[2] = c;
if (IN_ENCODING[buffer[2]] == 0xFF) throw DataFormatException(); if (IN_ENCODING[buffer[2]] == 0xFF) throw DataFormatException();
if ((c = readOne()) == -1) return -1; if ((c = readOne()) == -1) throw DataFormatException();
buffer[3] = c; buffer[3] = c;
if (IN_ENCODING[buffer[3]] == 0xFF) throw DataFormatException(); if (IN_ENCODING[buffer[3]] == 0xFF) throw DataFormatException();
@@ -116,9 +121,9 @@ int Base64DecoderBuf::readFromDevice()
int Base64DecoderBuf::readOne() int Base64DecoderBuf::readOne()
{ {
int ch = _istr.get(); int ch = _buf.sbumpc();
while (ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n') while (ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n')
ch = _istr.get(); ch = _buf.sbumpc();
return ch; return ch;
} }

View File

@@ -1,7 +1,7 @@
// //
// Base64Encoder.cpp // Base64Encoder.cpp
// //
// $Id: //poco/svn/Foundation/src/Base64Encoder.cpp#2 $ // $Id: //poco/1.4/Foundation/src/Base64Encoder.cpp#2 $
// //
// Library: Foundation // Library: Foundation
// Package: Streams // Package: Streams
@@ -57,7 +57,7 @@ Base64EncoderBuf::Base64EncoderBuf(std::ostream& ostr):
_groupLength(0), _groupLength(0),
_pos(0), _pos(0),
_lineLength(72), _lineLength(72),
_ostr(ostr) _buf(*ostr.rdbuf())
{ {
} }
@@ -88,58 +88,63 @@ int Base64EncoderBuf::getLineLength() const
int Base64EncoderBuf::writeToDevice(char c) int Base64EncoderBuf::writeToDevice(char c)
{ {
static const int eof = std::char_traits<char>::eof();
_group[_groupLength++] = (unsigned char) c; _group[_groupLength++] = (unsigned char) c;
if (_groupLength == 3) if (_groupLength == 3)
{ {
unsigned char idx; unsigned char idx;
idx = _group[0] >> 2; idx = _group[0] >> 2;
_ostr.put(OUT_ENCODING[idx]); if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4); idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
_ostr.put(OUT_ENCODING[idx]); if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[1] & 0x0F) << 2) | (_group[2] >> 6); idx = ((_group[1] & 0x0F) << 2) | (_group[2] >> 6);
_ostr.put(OUT_ENCODING[idx]); if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = _group[2] & 0x3F; idx = _group[2] & 0x3F;
_ostr.put(OUT_ENCODING[idx]); if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
_pos += 4; _pos += 4;
if (_lineLength > 0 && _pos >= _lineLength) if (_lineLength > 0 && _pos >= _lineLength)
{ {
_ostr << "\r\n"; if (_buf.sputc('\r') == eof) return eof;
if (_buf.sputc('\n') == eof) return eof;
_pos = 0; _pos = 0;
} }
_groupLength = 0; _groupLength = 0;
} }
return _ostr ? charToInt(c) : -1; return charToInt(c);
} }
int Base64EncoderBuf::close() int Base64EncoderBuf::close()
{ {
sync(); static const int eof = std::char_traits<char>::eof();
if (sync() == eof) return eof;
if (_groupLength == 1) if (_groupLength == 1)
{ {
_group[1] = 0; _group[1] = 0;
unsigned char idx; unsigned char idx;
idx = _group[0] >> 2; idx = _group[0] >> 2;
_ostr.put(OUT_ENCODING[idx]); if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4); idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
_ostr.put(OUT_ENCODING[idx]); if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
_ostr << "=="; if (_buf.sputc('=') == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
} }
else if (_groupLength == 2) else if (_groupLength == 2)
{ {
_group[2] = 0; _group[2] = 0;
unsigned char idx; unsigned char idx;
idx = _group[0] >> 2; idx = _group[0] >> 2;
_ostr.put(OUT_ENCODING[idx]); if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4); idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
_ostr.put(OUT_ENCODING[idx]); if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[1] & 0x0F) << 2) | (_group[2] >> 6); idx = ((_group[1] & 0x0F) << 2) | (_group[2] >> 6);
_ostr.put(OUT_ENCODING[idx]); if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
_ostr.put('='); if (_buf.sputc('=') == eof) return eof;
} }
_ostr.flush();
_groupLength = 0; _groupLength = 0;
return _ostr ? 0 : -1; return _buf.pubsync();
} }

View File

@@ -1,7 +1,7 @@
// //
// BinaryReader.cpp // BinaryReader.cpp
// //
// $Id: //poco/svn/Foundation/src/BinaryReader.cpp#2 $ // $Id: //poco/1.4/Foundation/src/BinaryReader.cpp#1 $
// //
// Library: Foundation // Library: Foundation
// Package: Streams // Package: Streams
@@ -36,13 +36,29 @@
#include "Poco/BinaryReader.h" #include "Poco/BinaryReader.h"
#include "Poco/ByteOrder.h" #include "Poco/ByteOrder.h"
#include "Poco/TextEncoding.h"
#include "Poco/TextConverter.h"
#include <algorithm>
namespace Poco { namespace Poco {
BinaryReader::BinaryReader(std::istream& istr, StreamByteOrder byteOrder): BinaryReader::BinaryReader(std::istream& istr, StreamByteOrder byteOrder):
_istr(istr) _istr(istr),
_pTextConverter(0)
{
#if defined(POCO_ARCH_BIG_ENDIAN)
_flipBytes = (byteOrder == LITTLE_ENDIAN_BYTE_ORDER);
#else
_flipBytes = (byteOrder == BIG_ENDIAN_BYTE_ORDER);
#endif
}
BinaryReader::BinaryReader(std::istream& istr, TextEncoding& encoding, StreamByteOrder byteOrder):
_istr(istr),
_pTextConverter(new TextConverter(encoding, Poco::TextEncoding::global()))
{ {
#if defined(POCO_ARCH_BIG_ENDIAN) #if defined(POCO_ARCH_BIG_ENDIAN)
_flipBytes = (byteOrder == LITTLE_ENDIAN_BYTE_ORDER); _flipBytes = (byteOrder == LITTLE_ENDIAN_BYTE_ORDER);
@@ -54,6 +70,7 @@ BinaryReader::BinaryReader(std::istream& istr, StreamByteOrder byteOrder):
BinaryReader::~BinaryReader() BinaryReader::~BinaryReader()
{ {
delete _pTextConverter;
} }
@@ -202,13 +219,20 @@ BinaryReader& BinaryReader::operator >> (std::string& value)
UInt32 size = 0; UInt32 size = 0;
read7BitEncoded(size); read7BitEncoded(size);
value.clear(); value.clear();
if (!_istr.good()) return *this;
value.reserve(size); value.reserve(size);
while (size--) while (size--)
{ {
char c; char c;
_istr.read(&c, 1); if (!_istr.read(&c, 1).good()) break;
value += c; value += c;
} }
if (_pTextConverter)
{
std::string converted;
_pTextConverter->convert(value, converted);
std::swap(value, converted);
}
return *this; return *this;
} }
@@ -255,19 +279,25 @@ void BinaryReader::read7BitEncoded(UInt64& value)
#endif #endif
void BinaryReader::readRaw(int length, std::string& value) void BinaryReader::readRaw(std::streamsize length, std::string& value)
{ {
value.clear(); value.clear();
value.reserve(length); value.reserve(static_cast<std::string::size_type>(length));
while (length--) while (length--)
{ {
char c; char c;
_istr.read(&c, 1); if (!_istr.read(&c, 1).good()) break;
value += c; value += c;
} }
} }
void BinaryReader::readRaw(char* buffer, std::streamsize length)
{
_istr.read(buffer, length);
}
void BinaryReader::readBOM() void BinaryReader::readBOM()
{ {
UInt16 bom; UInt16 bom;

View File

@@ -1,7 +1,7 @@
// //
// BinaryWriter.cpp // BinaryWriter.cpp
// //
// $Id: //poco/svn/Foundation/src/BinaryWriter.cpp#2 $ // $Id: //poco/1.4/Foundation/src/BinaryWriter.cpp#1 $
// //
// Library: Foundation // Library: Foundation
// Package: Streams // Package: Streams
@@ -36,6 +36,8 @@
#include "Poco/BinaryWriter.h" #include "Poco/BinaryWriter.h"
#include "Poco/ByteOrder.h" #include "Poco/ByteOrder.h"
#include "Poco/TextEncoding.h"
#include "Poco/TextConverter.h"
#include <cstring> #include <cstring>
@@ -43,7 +45,20 @@ namespace Poco {
BinaryWriter::BinaryWriter(std::ostream& ostr, StreamByteOrder byteOrder): BinaryWriter::BinaryWriter(std::ostream& ostr, StreamByteOrder byteOrder):
_ostr(ostr) _ostr(ostr),
_pTextConverter(0)
{
#if defined(POCO_ARCH_BIG_ENDIAN)
_flipBytes = (byteOrder == LITTLE_ENDIAN_BYTE_ORDER);
#else
_flipBytes = (byteOrder == BIG_ENDIAN_BYTE_ORDER);
#endif
}
BinaryWriter::BinaryWriter(std::ostream& ostr, TextEncoding& encoding, StreamByteOrder byteOrder):
_ostr(ostr),
_pTextConverter(new TextConverter(Poco::TextEncoding::global(), encoding))
{ {
#if defined(POCO_ARCH_BIG_ENDIAN) #if defined(POCO_ARCH_BIG_ENDIAN)
_flipBytes = (byteOrder == LITTLE_ENDIAN_BYTE_ORDER); _flipBytes = (byteOrder == LITTLE_ENDIAN_BYTE_ORDER);
@@ -55,6 +70,7 @@ BinaryWriter::BinaryWriter(std::ostream& ostr, StreamByteOrder byteOrder):
BinaryWriter::~BinaryWriter() BinaryWriter::~BinaryWriter()
{ {
delete _pTextConverter;
} }
@@ -255,10 +271,21 @@ BinaryWriter& BinaryWriter::operator << (UInt64 value)
BinaryWriter& BinaryWriter::operator << (const std::string& value) BinaryWriter& BinaryWriter::operator << (const std::string& value)
{
if (_pTextConverter)
{
std::string converted;
_pTextConverter->convert(value, converted);
UInt32 length = (UInt32) converted.size();
write7BitEncoded(length);
_ostr.write(converted.data(), length);
}
else
{ {
UInt32 length = (UInt32) value.size(); UInt32 length = (UInt32) value.size();
write7BitEncoded(length); write7BitEncoded(length);
_ostr.write(value.data(), length); _ostr.write(value.data(), length);
}
return *this; return *this;
} }
@@ -266,9 +293,21 @@ BinaryWriter& BinaryWriter::operator << (const std::string& value)
BinaryWriter& BinaryWriter::operator << (const char* value) BinaryWriter& BinaryWriter::operator << (const char* value)
{ {
poco_check_ptr (value); poco_check_ptr (value);
UInt32 length = (UInt32) std::strlen(value);
if (_pTextConverter)
{
std::string converted;
_pTextConverter->convert(value, static_cast<int>(std::strlen(value)), converted);
UInt32 length = (UInt32) converted.size();
write7BitEncoded(length);
_ostr.write(converted.data(), length);
}
else
{
UInt32 length = static_cast<UInt32>(std::strlen(value));
write7BitEncoded(length); write7BitEncoded(length);
_ostr.write(value, length); _ostr.write(value, length);
}
return *this; return *this;
} }
@@ -311,6 +350,12 @@ void BinaryWriter::writeRaw(const std::string& rawData)
} }
void BinaryWriter::writeRaw(const char* buffer, std::streamsize length)
{
_ostr.write(buffer, length);
}
void BinaryWriter::writeBOM() void BinaryWriter::writeBOM()
{ {
UInt16 value = 0xFEFF; UInt16 value = 0xFEFF;