activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r494846 - in /incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/io: DataInputStream.cpp DataOutputStream.cpp
Date Wed, 10 Jan 2007 15:18:16 GMT
Author: tabish
Date: Wed Jan 10 07:18:16 2007
New Revision: 494846

URL: http://svn.apache.org/viewvc?view=rev&rev=494846
Log:
http://issues.apache.org/activemq/browse/AMQCPP-25

Rewrote most of the methods to more safely handle data on various platforms, and do its own
byte swapping as needed.

Modified:
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/io/DataInputStream.cpp
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/io/DataOutputStream.cpp

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/io/DataInputStream.cpp
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/io/DataInputStream.cpp?view=diff&rev=494846&r1=494845&r2=494846
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/io/DataInputStream.cpp
(original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/io/DataInputStream.cpp
Wed Jan 10 07:18:16 2007
@@ -135,17 +135,19 @@
     try {
         unsigned short value = 0;
 
-        //unsigned char byte1 = this->readByte();
-        //unsigned char byte2 = this->readByte();
-        //
-        //value |= (byte2 << 8 | byte1 << 0);
+        unsigned char byte1 = this->readByte();
+        unsigned char byte2 = this->readByte();
+        
+        value |= (byte1 << 8 | byte2 << 0);
+        
+        return value;
 
         //char* temp = (char*)&value;
         //
         //return (short)Endian::byteSwap( value );
 
-        this->readFully( ( unsigned char* )&value, 0, sizeof( unsigned short ) );
-        return Endian::byteSwap( value );
+        //this->readFully( ( unsigned char* )&value, 0, sizeof( unsigned short ) );
+        //return Endian::byteSwap( value );
     }
     AMQ_CATCH_RETHROW( IOException )
     AMQ_CATCHALL_THROW( IOException )
@@ -155,9 +157,19 @@
 unsigned short DataInputStream::readUnsignedShort() 
     throw ( io::IOException, io::EOFException ) {
     try {
-        unsigned short value;
-        this->readFully( ( unsigned char* )&value, 0, sizeof( unsigned short ) );
-        return Endian::byteSwap( value );
+
+        unsigned short value = 0;
+
+        unsigned char byte1 = this->readByte();
+        unsigned char byte2 = this->readByte();
+        
+        value |= (byte1 << 8 | byte2 << 0);
+        
+        return value;
+
+//        unsigned short value;
+//        this->readFully( ( unsigned char* )&value, 0, sizeof( unsigned short ) );
+//        return Endian::byteSwap( value );
     }
     AMQ_CATCH_RETHROW( IOException )
     AMQ_CATCHALL_THROW( IOException )
@@ -166,9 +178,21 @@
 ////////////////////////////////////////////////////////////////////////////////
 int DataInputStream::readInt() throw ( io::IOException, io::EOFException ) {
     try {
-        unsigned int value;    
-        this->readFully( ( unsigned char* )&value, 0, sizeof( unsigned int ) );
-        return (int)Endian::byteSwap( value );
+
+        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);
+        
+        return value;
+
+//        unsigned int value;    
+//        this->readFully( ( unsigned char* )&value, 0, sizeof( unsigned int ) );
+//        return (int)Endian::byteSwap( value );
     }
     AMQ_CATCH_RETHROW( IOException )
     AMQ_CATCHALL_THROW( IOException )
@@ -177,9 +201,14 @@
 ////////////////////////////////////////////////////////////////////////////////
 double DataInputStream::readDouble() throw ( io::IOException, io::EOFException ) {
     try {
-        double value;
-        this->readFully( ( unsigned char* )&value, 0, sizeof( double ) );
-        return Endian::byteSwap( value );
+
+        unsigned long long value = this->readLong();
+        
+        return *((double*)&value);
+
+//        double value;
+//        this->readFully( ( unsigned char* )&value, 0, sizeof( double ) );
+//        return Endian::byteSwap( value );
     }
     AMQ_CATCH_RETHROW( IOException )
     AMQ_CATCHALL_THROW( IOException )
@@ -188,9 +217,21 @@
 ////////////////////////////////////////////////////////////////////////////////
 float DataInputStream::readFloat() throw ( io::IOException, io::EOFException ) {
     try {
-        float value;
-        this->readFully( ( unsigned char* )&value, 0, sizeof( float ) );
-        return Endian::byteSwap( value );
+
+        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);
+
+        return *((float*)&value);
+
+//        float value;
+//        this->readFully( ( unsigned char* )&value, 0, sizeof( float ) );
+//        return Endian::byteSwap( value );
     }
     AMQ_CATCH_RETHROW( IOException )
     AMQ_CATCHALL_THROW( IOException )
@@ -200,9 +241,26 @@
 long long DataInputStream::readLong()
     throw ( io::IOException, io::EOFException ) {
     try {
-        unsigned long long value;
-        this->readFully( ( unsigned char* )&value, 0, sizeof( unsigned long long )
);
-        return (long long)Endian::byteSwap( value );
+
+        unsigned long long value = 0;
+
+        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;
+
+        value = ( byte1 << 56 | byte2 << 48 | byte3 << 40 | byte4 <<
32 |
+                  byte5 << 24 | byte6 << 16 | byte7 << 8  | byte8 <<
0 );
+
+        return value;
+
+//        unsigned long long value;
+//        this->readFully( ( unsigned char* )&value, 0, sizeof( unsigned long long
) );
+//        return (long long)Endian::byteSwap( value );
     }
     AMQ_CATCH_RETHROW( IOException )
     AMQ_CATCHALL_THROW( IOException )

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/io/DataOutputStream.cpp
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/io/DataOutputStream.cpp?view=diff&rev=494846&r1=494845&r2=494846
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/io/DataOutputStream.cpp
(original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/io/DataOutputStream.cpp
Wed Jan 10 07:18:16 2007
@@ -89,8 +89,8 @@
     try {
         unsigned char ivalue = 0;
         value == true ? ivalue = 1 : ivalue = 0;
-        ivalue = Endian::byteSwap( ivalue );
-        write( ( unsigned char* )&ivalue, sizeof( ivalue ) );
+
+        write( ivalue );
     }
     AMQ_CATCH_RETHROW( IOException )
     AMQ_CATCHALL_THROW( IOException )
@@ -99,8 +99,7 @@
 ////////////////////////////////////////////////////////////////////////////////
 void DataOutputStream::writeByte( unsigned char value ) throw ( IOException ) {
     try {
-        value = Endian::byteSwap( value );
-        write( ( unsigned char* )&value, sizeof( value ) );
+        write( value );
     }
     AMQ_CATCH_RETHROW( IOException )
     AMQ_CATCHALL_THROW( IOException )
@@ -108,14 +107,18 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 void DataOutputStream::writeShort( short value ) throw ( IOException ) {
-    writeUnsignedShort( (unsigned short)value );
+    try {
+        writeUnsignedShort( (unsigned short)value );
+    }
+    AMQ_CATCH_RETHROW( IOException )
+    AMQ_CATCHALL_THROW( IOException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void DataOutputStream::writeUnsignedShort( unsigned short value ) throw ( IOException ) {
     try {
-        value = Endian::byteSwap( value );
-        write( ( unsigned char* )&value, sizeof( value ) );
+        write( (unsigned char)( (value & 0xFF00) >> 8 ) );
+        write( (unsigned char)( (value & 0x00FF) >> 0 ) );
     }
     AMQ_CATCH_RETHROW( IOException )
     AMQ_CATCHALL_THROW( IOException )
@@ -124,8 +127,7 @@
 ////////////////////////////////////////////////////////////////////////////////
 void DataOutputStream::writeChar( char value ) throw ( IOException ) {
     try {
-        value = Endian::byteSwap( ( unsigned char )value );
-        write( ( unsigned char* )&value, sizeof( value ) );
+        write( value );
     }
     AMQ_CATCH_RETHROW( IOException )
     AMQ_CATCHALL_THROW( IOException )
@@ -134,8 +136,10 @@
 ////////////////////////////////////////////////////////////////////////////////
 void DataOutputStream::writeInt( int value ) throw ( IOException ) {
     try {
-        value = Endian::byteSwap( ( unsigned int )value );
-        write( ( unsigned char* )&value, sizeof( value ) );
+        write( (unsigned char)( (value & 0xFF000000) >> 24 ) );
+        write( (unsigned char)( (value & 0x00FF0000) >> 16 ) );
+        write( (unsigned char)( (value & 0x0000FF00) >> 8 ) );
+        write( (unsigned char)( (value & 0x000000FF) >> 0 ) );
     }
     AMQ_CATCH_RETHROW( IOException )
     AMQ_CATCHALL_THROW( IOException )
@@ -144,8 +148,14 @@
 ////////////////////////////////////////////////////////////////////////////////
 void DataOutputStream::writeLong( long long value ) throw ( IOException ) {
     try {
-        value = Endian::byteSwap( ( unsigned long long )value );
-        write( ( unsigned char* )&value, sizeof( value ) );
+        write( (unsigned char)( (value & 0xFF00000000000000ULL) >> 56 ) );
+        write( (unsigned char)( (value & 0x00FF000000000000ULL) >> 48 ) );
+        write( (unsigned char)( (value & 0x0000FF0000000000ULL) >> 40 ) );
+        write( (unsigned char)( (value & 0x000000FF00000000ULL) >> 32 ) );
+        write( (unsigned char)( (value & 0x00000000FF000000ULL) >> 24 ) );
+        write( (unsigned char)( (value & 0x0000000000FF0000ULL) >> 16 ) );
+        write( (unsigned char)( (value & 0x000000000000FF00ULL) >> 8 ) );
+        write( (unsigned char)( (value & 0x00000000000000FFULL) >> 0 ) );
     }
     AMQ_CATCH_RETHROW( IOException )
     AMQ_CATCHALL_THROW( IOException )
@@ -154,8 +164,7 @@
 ////////////////////////////////////////////////////////////////////////////////
 void DataOutputStream::writeFloat( float value ) throw ( IOException ) {
     try {
-        value = Endian::byteSwap( value );
-        write( ( unsigned char* )&value, sizeof( value ) );
+        this->writeInt( *((unsigned int*)&value) );
     }
     AMQ_CATCH_RETHROW( IOException )
     AMQ_CATCHALL_THROW( IOException )
@@ -164,8 +173,7 @@
 ////////////////////////////////////////////////////////////////////////////////
 void DataOutputStream::writeDouble( double value ) throw ( IOException ) {
     try {
-        value = Endian::byteSwap( value );
-        write( ( unsigned char* )&value, sizeof( value ) );
+        writeLong( *((unsigned long long*)&value) );
     }
     AMQ_CATCH_RETHROW( IOException )
     AMQ_CATCHALL_THROW( IOException )



Mime
View raw message