Return-Path: Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: (qmail 49528 invoked from network); 28 Jun 2007 17:40:42 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 28 Jun 2007 17:40:42 -0000 Received: (qmail 14639 invoked by uid 500); 28 Jun 2007 17:40:45 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 14620 invoked by uid 500); 28 Jun 2007 17:40:45 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 14611 invoked by uid 99); 28 Jun 2007 17:40:45 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Jun 2007 10:40:45 -0700 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Jun 2007 10:40:41 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 081FE1A981A; Thu, 28 Jun 2007 10:40:21 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r551633 - in /activemq/activemq-cpp/trunk/src/main/activemq/io: DataInputStream.cpp DataInputStream.h Date: Thu, 28 Jun 2007 17:40:20 -0000 To: commits@activemq.apache.org From: tabish@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070628174021.081FE1A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tabish Date: Thu Jun 28 10:40:20 2007 New Revision: 551633 URL: http://svn.apache.org/viewvc?view=rev&rev=551633 Log: https://issues.apache.org/activemq/browse/AMQCPP-93 Submitting optimized? DataInputStream Modified: activemq/activemq-cpp/trunk/src/main/activemq/io/DataInputStream.cpp activemq/activemq-cpp/trunk/src/main/activemq/io/DataInputStream.h Modified: activemq/activemq-cpp/trunk/src/main/activemq/io/DataInputStream.cpp URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/io/DataInputStream.cpp?view=diff&rev=551633&r1=551632&r2=551633 ============================================================================== --- activemq/activemq-cpp/trunk/src/main/activemq/io/DataInputStream.cpp (original) +++ activemq/activemq-cpp/trunk/src/main/activemq/io/DataInputStream.cpp Thu Jun 28 10:40:20 2007 @@ -82,10 +82,9 @@ bool DataInputStream::readBoolean() throw( io::IOException, io::EOFException ) { try { - - char value = 0; - this->readFully( ( unsigned char* )&value, 0, sizeof( char ) ); - return (char)( value != 0 ); + unsigned char value = 0; + readAllData( &value, sizeof(value) ); + return (bool)( value != 0 ); } AMQ_CATCH_RETHROW( EOFException ) AMQ_CATCH_RETHROW( IOException ) @@ -96,9 +95,8 @@ char DataInputStream::readByte() throw ( io::IOException, io::EOFException ) { try { - - char value = 0; - this->readFully( ( unsigned char* )&value, 0, sizeof( char ) ); + unsigned char value = 0; + readAllData( &value, sizeof(value) ); return (char)( value ); } AMQ_CATCH_RETHROW( EOFException ) @@ -110,10 +108,9 @@ unsigned char DataInputStream::readUnsignedByte() throw ( io::IOException, io::EOFException ) { try { - unsigned char value = 0; - this->readFully( ( unsigned char* )&value, 0, sizeof( unsigned char ) ); - return (char)( value ); + readAllData( &value, sizeof(value) ); + return value; } AMQ_CATCH_RETHROW( EOFException ) AMQ_CATCH_RETHROW( IOException ) @@ -123,9 +120,8 @@ //////////////////////////////////////////////////////////////////////////////// char DataInputStream::readChar() throw ( io::IOException, io::EOFException ) { try { - - char value = 0; - this->readFully( ( unsigned char* )&value, 0, sizeof( char ) ); + unsigned char value = 0; + readAllData( &value, sizeof(value) ); return (char)( value ); } AMQ_CATCH_RETHROW( EOFException ) @@ -136,14 +132,10 @@ //////////////////////////////////////////////////////////////////////////////// short DataInputStream::readShort() throw ( io::IOException, io::EOFException ) { try { - - unsigned short value = 0; - - unsigned char byte1 = this->readByte(); - unsigned char byte2 = this->readByte(); - - value |= (byte1 << 8 | byte2 << 0); - + short value = 0; + unsigned char buffer[sizeof(value)] = {0}; + readAllData( buffer, sizeof(value) ); + value |= (buffer[0] << 8 | buffer[1] << 0); return value; } AMQ_CATCH_RETHROW( EOFException ) @@ -155,14 +147,10 @@ unsigned short DataInputStream::readUnsignedShort() throw ( io::IOException, io::EOFException ) { try { - unsigned short value = 0; - - unsigned char byte1 = this->readByte(); - unsigned char byte2 = this->readByte(); - - value |= (byte1 << 8 | byte2 << 0); - + unsigned char buffer[sizeof(value)] = {0}; + readAllData( buffer, sizeof(value) ); + value |= (buffer[0] << 8 | buffer[1] << 0); return value; } AMQ_CATCH_RETHROW( EOFException ) @@ -173,16 +161,11 @@ //////////////////////////////////////////////////////////////////////////////// int DataInputStream::readInt() throw ( io::IOException, io::EOFException ) { try { - unsigned int value = 0; - - unsigned char byte1 = this->readByte(); - unsigned char byte2 = this->readByte(); - unsigned char byte3 = this->readByte(); - unsigned char byte4 = this->readByte(); - - value |= (byte1 << 24 | byte2 << 16 | byte3 << 8 | byte4 << 0); - + unsigned char buffer[sizeof(value)] = {0}; + readAllData( buffer, sizeof(value) ); + value |= (buffer[0] << 24 | buffer[1] << 16 | + buffer[2] << 8 | buffer[3] << 0); return value; } AMQ_CATCH_RETHROW( EOFException ) @@ -193,7 +176,6 @@ //////////////////////////////////////////////////////////////////////////////// double DataInputStream::readDouble() throw ( io::IOException, io::EOFException ) { try { - unsigned long long lvalue = this->readLong(); double value = 0.0; memcpy( &value, &lvalue, sizeof( unsigned long long ) ); @@ -207,7 +189,6 @@ //////////////////////////////////////////////////////////////////////////////// float DataInputStream::readFloat() throw ( io::IOException, io::EOFException ) { try { - unsigned int lvalue = this->readInt(); float value = 0.0f; memcpy( &value, &lvalue, sizeof( unsigned int ) ); @@ -222,17 +203,20 @@ long long DataInputStream::readLong() throw ( io::IOException, io::EOFException ) { try { - unsigned long long value = 0; + unsigned char buffer[sizeof(value)] = {0}; + readAllData( buffer, sizeof(value) ); - unsigned long long byte1 = this->readByte() & 0x00000000000000FFULL; - unsigned long long byte2 = this->readByte() & 0x00000000000000FFULL; - unsigned long long byte3 = this->readByte() & 0x00000000000000FFULL; - unsigned long long byte4 = this->readByte() & 0x00000000000000FFULL; - unsigned long long byte5 = this->readByte() & 0x00000000000000FFULL; - unsigned long long byte6 = this->readByte() & 0x00000000000000FFULL; - unsigned long long byte7 = this->readByte() & 0x00000000000000FFULL; - unsigned long long byte8 = this->readByte() & 0x00000000000000FFULL; + // Have to do it this way because on Solaris and Cygwin we get all + // kinds of warnings when shifting a byte up into a long long. + unsigned long long byte1 = buffer[0] & 0x00000000000000FFULL; + unsigned long long byte2 = buffer[1] & 0x00000000000000FFULL; + unsigned long long byte3 = buffer[2] & 0x00000000000000FFULL; + unsigned long long byte4 = buffer[3] & 0x00000000000000FFULL; + unsigned long long byte5 = buffer[4] & 0x00000000000000FFULL; + unsigned long long byte6 = buffer[5] & 0x00000000000000FFULL; + unsigned long long byte7 = buffer[6] & 0x00000000000000FFULL; + unsigned long long byte8 = buffer[7] & 0x00000000000000FFULL; value = ( byte1 << 56 | byte2 << 48 | byte3 << 40 | byte4 << 32 | byte5 << 24 | byte6 << 16 | byte7 << 8 | byte8 << 0 ); @@ -248,7 +232,6 @@ std::string DataInputStream::readString() throw ( io::IOException, io::EOFException ) { try { - size_t size = 1024; std::vector buffer; buffer.resize( size ); @@ -284,12 +267,22 @@ std::string DataInputStream::readUTF() throw ( io::IOException, io::EOFException ) { try { + std::vector buffer; + unsigned short length = readUnsignedShort(); + buffer.resize(length + 1); // Add one for a null charactor. + + std::size_t n = 0; + while( n < length ) { + std::size_t count = inputStream->read( &buffer[n], (length - n) ); + if( count == (std::size_t)-1 ) { + throw EOFException( + __FILE__, __LINE__, + "DataInputStream::readUTF - Reached EOF" ); + } + n += count; + } - std::string buffer; - unsigned short len = readUnsignedShort(); - buffer.resize(len); - readFully( (unsigned char*)buffer.c_str(), 0, len ); - return buffer; + return (char*)&buffer[0]; } AMQ_CATCH_RETHROW( EOFException ) AMQ_CATCH_RETHROW( IOException ) Modified: activemq/activemq-cpp/trunk/src/main/activemq/io/DataInputStream.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/io/DataInputStream.h?view=diff&rev=551633&r1=551632&r2=551633 ============================================================================== --- activemq/activemq-cpp/trunk/src/main/activemq/io/DataInputStream.h (original) +++ activemq/activemq-cpp/trunk/src/main/activemq/io/DataInputStream.h Thu Jun 28 10:40:20 2007 @@ -356,6 +356,26 @@ throw( io::IOException, exceptions::UnsupportedOperationException ); + private: + + // Used internally to reliable get data from the underlying stream + inline void readAllData( unsigned char* buffer, + std::size_t length ) + throw ( io::IOException, + io::EOFException ) { + + std::size_t n = 0; + do{ + std::size_t count = inputStream->read( &buffer[n], length - n ); + if( count == (std::size_t)-1 ) { + throw EOFException( + __FILE__, __LINE__, + "DataInputStream::readLong - Reached EOF" ); + } + n += count; + } while( n < length ); + } + }; }}