activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
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 GMT
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<char> buffer;
         buffer.resize( size );
@@ -284,12 +267,22 @@
 std::string DataInputStream::readUTF()
     throw ( io::IOException, io::EOFException ) {
     try {
+        std::vector<unsigned char> 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 );
+        }
+
     };
 
 }}



Mime
View raw message