You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2012/10/04 00:02:36 UTC

svn commit: r1393791 - in /activemq/activemq-cpp/trunk/activemq-cpp/src/main: activemq/transport/tcp/TcpTransport.cpp decaf/io/FilterInputStream.cpp decaf/io/FilterOutputStream.cpp

Author: tabish
Date: Wed Oct  3 22:02:36 2012
New Revision: 1393791

URL: http://svn.apache.org/viewvc?rev=1393791&view=rev
Log:
Polish the code and ensure that all close methods for members gets called and data is freed before throwing IOException in close() better destructor behavior also.

Modified:
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/tcp/TcpTransport.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterInputStream.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterOutputStream.cpp

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/tcp/TcpTransport.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/tcp/TcpTransport.cpp?rev=1393791&r1=1393790&r2=1393791&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/tcp/TcpTransport.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/tcp/TcpTransport.cpp Wed Oct  3 22:02:36 2012
@@ -109,22 +109,26 @@ void TcpTransport::connect(const decaf::
         // Get the write buffer size.
         int outputBufferSize = Integer::parseInt(properties.getProperty("outputBufferSize", "8192"));
 
-        Pointer<InputStream> inputStream(socket->getInputStream());
-        Pointer<OutputStream> outputStream(socket->getOutputStream());
+        // We don't own these ever, socket object owns.
+        InputStream* socketIStream = socket->getInputStream();
+        OutputStream* sokcetOStream = socket->getOutputStream();
 
-        // If tcp tracing was enabled, wrap the iostreams with logging streams
+        Pointer<InputStream> inputStream;
+        Pointer<OutputStream> outputStream;
+
+        // If tcp tracing was enabled, wrap the input / output streams with logging streams
         if (properties.getProperty("transport.tcpTracingEnabled", "false") == "true") {
             // Wrap with logging stream, we don't own the wrapped streams
-            inputStream.reset(new LoggingInputStream(inputStream.release()));
-            outputStream.reset(new LoggingOutputStream(outputStream.release()));
+            inputStream.reset(new LoggingInputStream(socketIStream));
+            outputStream.reset(new LoggingOutputStream(sokcetOStream));
 
             // Now wrap with the Buffered streams, we own the source streams
             inputStream.reset(new BufferedInputStream(inputStream.release(), inputBufferSize, true));
             outputStream.reset(new BufferedOutputStream(outputStream.release(), outputBufferSize, true));
         } else {
             // Wrap with the Buffered streams, we don't own the source streams
-            inputStream.reset(new BufferedInputStream(inputStream.release(), inputBufferSize));
-            outputStream.reset(new BufferedOutputStream(outputStream.release(), outputBufferSize));
+            inputStream.reset(new BufferedInputStream(socketIStream, inputBufferSize));
+            outputStream.reset(new BufferedOutputStream(sokcetOStream, outputBufferSize));
         }
 
         // Now wrap the Buffered Streams with DataInput based streams.  We own

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterInputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterInputStream.cpp?rev=1393791&r1=1393790&r2=1393791&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterInputStream.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterInputStream.cpp Wed Oct  3 22:02:36 2012
@@ -26,8 +26,8 @@ using namespace decaf::lang;
 using namespace decaf::lang::exceptions;
 
 ////////////////////////////////////////////////////////////////////////////////
-FilterInputStream::FilterInputStream( InputStream* inputStream, bool own ) :
-    InputStream(), inputStream( inputStream ), own( own ), closed( inputStream == NULL ? true : false ) {
+FilterInputStream::FilterInputStream(InputStream* inputStream, bool own) :
+    InputStream(), inputStream(inputStream), own(own), closed(inputStream == NULL ? true : false) {
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -35,14 +35,16 @@ FilterInputStream::~FilterInputStream() 
 
     try {
         this->close();
+    }
+    DECAF_CATCHALL_NOTHROW()
 
-        if( own ) {
+    try {
+        if (own) {
             delete inputStream;
         }
         inputStream = NULL;
     }
-    DECAF_CATCH_NOTHROW( IOException )
-    DECAF_CATCHALL_NOTHROW( )
+    DECAF_CATCHALL_NOTHROW()
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -50,37 +52,34 @@ int FilterInputStream::available() const
 
     try {
 
-        if( isClosed() ) {
-            throw IOException(
-                __FILE__, __LINE__,
-                "FilterInputStream::available - Stream is closed" );
+        if (isClosed()) {
+            throw IOException(__FILE__, __LINE__, "FilterInputStream::available - Stream is closed");
         }
 
         return inputStream->available();
     }
-    DECAF_CATCH_RETHROW( IOException )
-    DECAF_CATCHALL_THROW( IOException )
+    DECAF_CATCH_RETHROW(IOException)
+    DECAF_CATCHALL_THROW(IOException)
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void FilterInputStream::close() {
 
     try {
-        if( !closed && inputStream != NULL ) {
+        if (!closed && inputStream != NULL) {
             inputStream->close();
         }
         this->closed = true;
     }
-    DECAF_CATCH_RETHROW( IOException )
-    DECAF_CATCHALL_THROW( IOException )
+    DECAF_CATCH_RETHROW(IOException)
+    DECAF_CATCHALL_THROW(IOException)
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-void FilterInputStream::mark( int readLimit ) {
+void FilterInputStream::mark(int readLimit) {
     try {
-
-        if( !isClosed() ) {
-            inputStream->mark( readLimit );
+        if (!isClosed()) {
+            inputStream->mark(readLimit);
         }
     }
     DECAF_CATCHALL_NOTHROW()
@@ -88,26 +87,21 @@ void FilterInputStream::mark( int readLi
 
 ////////////////////////////////////////////////////////////////////////////////
 void FilterInputStream::reset() {
-
     try {
-
-        if( isClosed() ) {
-            throw IOException(
-                __FILE__, __LINE__,
-                "FilterInputStream::reset - Stream is closed" );
+        if (isClosed()) {
+            throw IOException(__FILE__, __LINE__, "FilterInputStream::reset - Stream is closed");
         }
 
         return inputStream->reset();
     }
-    DECAF_CATCH_RETHROW( IOException )
-    DECAF_CATCHALL_THROW( IOException )
+    DECAF_CATCH_RETHROW(IOException)
+    DECAF_CATCHALL_THROW(IOException)
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 bool FilterInputStream::markSupported() const {
-
     try {
-        if( !isClosed() ) {
+        if (!isClosed()) {
             return inputStream->markSupported();
         }
     }
@@ -116,21 +110,19 @@ bool FilterInputStream::markSupported() 
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-long long FilterInputStream::skip( long long num ) {
+long long FilterInputStream::skip(long long num) {
 
     try {
 
-        if( isClosed() ) {
-            throw IOException(
-                __FILE__, __LINE__,
-                "FilterInputStream::skip - Stream is closed" );
+        if (isClosed()) {
+            throw IOException(__FILE__, __LINE__, "FilterInputStream::skip - Stream is closed");
         }
 
-        return inputStream->skip( num );
+        return inputStream->skip(num);
     }
-    DECAF_CATCH_RETHROW( UnsupportedOperationException )
-    DECAF_CATCH_RETHROW( IOException )
-    DECAF_CATCHALL_THROW( IOException )
+    DECAF_CATCH_RETHROW(UnsupportedOperationException)
+    DECAF_CATCH_RETHROW(IOException)
+    DECAF_CATCHALL_THROW(IOException)
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -138,53 +130,47 @@ int FilterInputStream::doReadByte() {
 
     try {
 
-        if( isClosed() ) {
-            throw IOException(
-                __FILE__, __LINE__,
-                "FilterInputStream::doReadByte - Stream is closed" );
+        if (isClosed()) {
+            throw IOException(__FILE__, __LINE__, "FilterInputStream::doReadByte - Stream is closed");
         }
 
         return inputStream->read();
     }
-    DECAF_CATCH_RETHROW( IOException )
-    DECAF_CATCHALL_THROW( IOException )
+    DECAF_CATCH_RETHROW(IOException)
+    DECAF_CATCHALL_THROW(IOException)
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-int FilterInputStream::doReadArray( unsigned char* buffer, int size ) {
+int FilterInputStream::doReadArray(unsigned char* buffer, int size) {
 
     try {
 
-        if( isClosed() ) {
-            throw IOException(
-                __FILE__, __LINE__,
-                "FilterInputStream::doReadArray - Stream is closed" );
+        if (isClosed()) {
+            throw IOException(__FILE__, __LINE__, "FilterInputStream::doReadArray - Stream is closed");
         }
 
-        return doReadArrayBounded( buffer, size, 0, size );
+        return doReadArrayBounded(buffer, size, 0, size);
     }
-    DECAF_CATCH_RETHROW( IOException )
-    DECAF_CATCH_RETHROW( NullPointerException )
-    DECAF_CATCHALL_THROW( IOException )
+    DECAF_CATCH_RETHROW(IOException)
+    DECAF_CATCH_RETHROW(NullPointerException)
+    DECAF_CATCHALL_THROW(IOException)
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-int FilterInputStream::doReadArrayBounded( unsigned char* buffer, int size, int offset, int length ) {
+int FilterInputStream::doReadArrayBounded(unsigned char* buffer, int size, int offset, int length) {
 
     try {
 
-        if( isClosed() ) {
-            throw IOException(
-                __FILE__, __LINE__,
-                "FilterInputStream::doReadArrayBounded - Stream is closed" );
+        if (isClosed()) {
+            throw IOException(__FILE__, __LINE__, "FilterInputStream::doReadArrayBounded - Stream is closed");
         }
 
-        return inputStream->read( buffer, size, offset, length );
+        return inputStream->read(buffer, size, offset, length);
     }
-    DECAF_CATCH_RETHROW( IOException )
-    DECAF_CATCH_RETHROW( NullPointerException )
-    DECAF_CATCH_RETHROW( IndexOutOfBoundsException )
-    DECAF_CATCHALL_THROW( IOException )
+    DECAF_CATCH_RETHROW(IOException)
+    DECAF_CATCH_RETHROW(NullPointerException)
+    DECAF_CATCH_RETHROW(IndexOutOfBoundsException)
+    DECAF_CATCHALL_THROW(IOException)
 }
 
 ////////////////////////////////////////////////////////////////////////////////

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterOutputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterOutputStream.cpp?rev=1393791&r1=1393790&r2=1393791&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterOutputStream.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterOutputStream.cpp Wed Oct  3 22:02:36 2012
@@ -28,139 +28,128 @@ using namespace decaf::lang;
 using namespace decaf::lang::exceptions;
 
 ////////////////////////////////////////////////////////////////////////////////
-FilterOutputStream::FilterOutputStream( OutputStream* outputStream, bool own ) :
-    OutputStream(), outputStream( outputStream ), own( own ), closed( outputStream == NULL ? true : false ) {
+FilterOutputStream::FilterOutputStream(OutputStream* outputStream, bool own) :
+    OutputStream(), outputStream(outputStream), own(own), closed(outputStream == NULL ? true : false) {
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 FilterOutputStream::~FilterOutputStream() {
     try {
         this->close();
+    }
+    DECAF_CATCHALL_NOTHROW()
 
-        if( own ) {
+    try {
+        if (own) {
             delete outputStream;
         }
         outputStream = NULL;
     }
-    DECAF_CATCH_NOTHROW( IOException )
-    DECAF_CATCHALL_NOTHROW( )
+    DECAF_CATCHALL_NOTHROW()
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-void FilterOutputStream::doWriteByte( unsigned char c ) {
+void FilterOutputStream::doWriteByte(unsigned char c) {
     try {
 
-        if( isClosed()  ) {
-            throw IOException(
-                __FILE__, __LINE__,
-                "FilterOutputStream::write - Stream is closed" );
+        if (isClosed()) {
+            throw IOException(__FILE__, __LINE__, "FilterOutputStream::write - Stream is closed");
         }
 
-        this->outputStream->write( c );
+        this->outputStream->write(c);
     }
-    DECAF_CATCH_RETHROW( IOException )
-    DECAF_CATCHALL_THROW( IOException )
+    DECAF_CATCH_RETHROW(IOException)
+    DECAF_CATCHALL_THROW(IOException)
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-void FilterOutputStream::doWriteArray( const unsigned char* buffer, int size ) {
+void FilterOutputStream::doWriteArray(const unsigned char* buffer, int size) {
 
     try {
 
-        if( isClosed() ) {
-            throw IOException(
-                __FILE__, __LINE__,
-                "FilterOutputStream::write - Stream is closed" );
+        if (isClosed()) {
+            throw IOException(__FILE__, __LINE__, "FilterOutputStream::write - Stream is closed");
         }
 
-        this->doWriteArrayBounded( buffer, size, 0, size );
+        this->doWriteArrayBounded(buffer, size, 0, size);
     }
-    DECAF_CATCH_RETHROW( IOException )
-    DECAF_CATCH_RETHROW( NullPointerException )
-    DECAF_CATCH_RETHROW( IndexOutOfBoundsException )
-    DECAF_CATCHALL_THROW( IOException )
+    DECAF_CATCH_RETHROW(IOException)
+    DECAF_CATCH_RETHROW(NullPointerException)
+    DECAF_CATCH_RETHROW(IndexOutOfBoundsException)
+    DECAF_CATCHALL_THROW(IOException)
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-void FilterOutputStream::doWriteArrayBounded( const unsigned char* buffer, int size, int offset, int length ) {
+void FilterOutputStream::doWriteArrayBounded(const unsigned char* buffer, int size, int offset, int length) {
 
     try {
 
-        if( isClosed() ) {
-            throw IOException(
-                __FILE__, __LINE__,
-                "FilterOutputStream::write - Stream is closed" );
+        if (isClosed()) {
+            throw IOException(__FILE__, __LINE__, "FilterOutputStream::write - Stream is closed");
         }
 
-        if( buffer == NULL ) {
-            throw decaf::lang::exceptions::NullPointerException(
-                __FILE__, __LINE__,
-                "FilterOutputStream::write - Buffer passed is Null.");
+        if (buffer == NULL) {
+            throw decaf::lang::exceptions::NullPointerException(__FILE__, __LINE__, "FilterOutputStream::write - Buffer passed is Null.");
         }
 
-        if( size < 0 ) {
-            throw IndexOutOfBoundsException(
-                __FILE__, __LINE__, "size parameter out of Bounds: %d.", size );
+        if (size < 0) {
+            throw IndexOutOfBoundsException(__FILE__, __LINE__, "size parameter out of Bounds: %d.", size);
         }
 
-        if( offset > size || offset < 0 ) {
-            throw IndexOutOfBoundsException(
-                __FILE__, __LINE__, "offset parameter out of Bounds: %d.", offset );
+        if (offset > size || offset < 0) {
+            throw IndexOutOfBoundsException(__FILE__, __LINE__, "offset parameter out of Bounds: %d.", offset);
         }
 
-        if( length < 0 || length > size - offset ) {
-            throw IndexOutOfBoundsException(
-                __FILE__, __LINE__, "length parameter out of Bounds: %d.", length );
+        if (length < 0 || length > size - offset) {
+            throw IndexOutOfBoundsException(__FILE__, __LINE__, "length parameter out of Bounds: %d.", length);
         }
 
         // Calls the doWriteByte method since subclasses may over override that method.
-        for( int ix = offset; ix < offset + length; ++ix ) {
-            this->doWriteByte( buffer[ix] );
+        for (int ix = offset; ix < offset + length; ++ix) {
+            this->doWriteByte(buffer[ix]);
         }
     }
-    DECAF_CATCH_RETHROW( IOException )
-    DECAF_CATCH_RETHROW( NullPointerException )
-    DECAF_CATCH_RETHROW( IndexOutOfBoundsException )
-    DECAF_CATCHALL_THROW( IOException )
+    DECAF_CATCH_RETHROW(IOException)
+    DECAF_CATCH_RETHROW(NullPointerException)
+    DECAF_CATCH_RETHROW(IndexOutOfBoundsException)
+    DECAF_CATCHALL_THROW(IOException)
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void FilterOutputStream::flush() {
     try {
 
-        if( isClosed() ) {
-            throw IOException(
-                __FILE__, __LINE__,
-                "FilterOutputStream::flush - Stream is closed" );
+        if (isClosed()) {
+            throw IOException(__FILE__, __LINE__, "FilterOutputStream::flush - Stream is closed");
         }
 
         this->outputStream->flush();
     }
-    DECAF_CATCH_RETHROW( IOException )
-    DECAF_CATCHALL_THROW( IOException )
+    DECAF_CATCH_RETHROW(IOException)
+    DECAF_CATCHALL_THROW(IOException)
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void FilterOutputStream::close() {
     try {
-        if( !this->closed && this->outputStream != NULL ) {
+        if (!this->closed && this->outputStream != NULL) {
             this->outputStream->flush();
             this->outputStream->close();
         }
         this->closed = true;
     }
-    DECAF_CATCH_RETHROW( IOException )
-    DECAF_CATCHALL_THROW( IOException )
+    DECAF_CATCH_RETHROW(IOException)
+    DECAF_CATCHALL_THROW(IOException)
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 std::string FilterOutputStream::toString() const {
 
-    if( this->outputStream != NULL ) {
+    if (this->outputStream != NULL) {
         return this->outputStream->toString();
     }
 
-    return typeid( this ).name();
+    return typeid(this).name();
 }
 
 ////////////////////////////////////////////////////////////////////////////////