activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r591481 - in /activemq/activemq-cpp/decaf/trunk/src/main/decaf/io: BufferedInputStream.cpp BufferedInputStream.h BufferedOutputStream.cpp BufferedOutputStream.h FilterOutputStream.h OutputStream.h
Date Fri, 02 Nov 2007 21:40:45 GMT
Author: tabish
Date: Fri Nov  2 14:40:45 2007
New Revision: 591481

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

Modified:
    activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedInputStream.cpp
    activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedInputStream.h
    activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedOutputStream.cpp
    activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedOutputStream.h
    activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/FilterOutputStream.h
    activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/OutputStream.h

Modified: activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedInputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedInputStream.cpp?rev=591481&r1=591480&r2=591481&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedInputStream.cpp (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedInputStream.cpp Fri Nov  2
14:40:45 2007
@@ -35,8 +35,15 @@
 BufferedInputStream::BufferedInputStream( InputStream* stream,
                                           std::size_t bufferSize,
                                           bool own )
+    throw ( lang::exceptions::IllegalArgumentException )
+
 : FilterInputStream( stream, own ) {
-    this->init( bufferSize );
+
+    try {
+        this->init( bufferSize );
+    }
+    DECAF_CATCH_RETHROW( IllegalArgumentException )
+    DECAF_CATCHALL_THROW( IllegalArgumentException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -50,6 +57,12 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 void BufferedInputStream::init( std::size_t bufferSize ){
+
+    if( bufferSize <= 0 ) {
+        throw new IllegalArgumentException(
+            __FILE__, __LINE__,
+            "BufferedInputStream::init - Size must be greater than zero");
+    }
 
     this->bufferSize = bufferSize;
 

Modified: activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedInputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedInputStream.h?rev=591481&r1=591480&r2=591481&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedInputStream.h (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedInputStream.h Fri Nov  2 14:40:45
2007
@@ -20,7 +20,6 @@
 
 #include <decaf/io/FilterInputStream.h>
 #include <decaf/lang/exceptions/IllegalArgumentException.h>
-#include <assert.h>
 
 namespace decaf{
 namespace io{
@@ -68,11 +67,12 @@
          * @param stream the target input stream
          * @param bufferSize the size for the internal buffer.
          * @param own indicates if we own the stream object, defaults to false.
-         * @throws IllegalArgumentException is the size is negative.
+         * @throws IllegalArgumentException is the size is zero.
          */
         BufferedInputStream( InputStream* stream,
                              std::size_t bufferSize,
-                             bool own = false );
+                             bool own = false )
+            throw ( lang::exceptions::IllegalArgumentException );
 
         virtual ~BufferedInputStream();
 

Modified: activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedOutputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedOutputStream.cpp?rev=591481&r1=591480&r2=591481&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedOutputStream.cpp (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedOutputStream.cpp Fri Nov 
2 14:40:45 2007
@@ -21,11 +21,12 @@
 using namespace std;
 using namespace decaf;
 using namespace decaf::io;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
 
 ////////////////////////////////////////////////////////////////////////////////
 BufferedOutputStream::BufferedOutputStream( OutputStream* stream, bool own )
-: FilterOutputStream( stream, own )
-{
+: FilterOutputStream( stream, own ) {
     // Default to 1k buffer.
     init( 1024 );
 }
@@ -34,23 +35,34 @@
 BufferedOutputStream::BufferedOutputStream( OutputStream* stream,
     std::size_t bufSize,
     bool own )
-: FilterOutputStream( stream, own )
-{
-    init( bufSize );
+        throw ( lang::exceptions::IllegalArgumentException )
+
+: FilterOutputStream( stream, own ) {
+
+    try {
+        this->init( bufSize );
+    }
+    DECAF_CATCH_RETHROW( IllegalArgumentException )
+    DECAF_CATCHALL_THROW( IllegalArgumentException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-BufferedOutputStream::~BufferedOutputStream()
-{
-    // Destroy the buffer.
-    if( buffer != NULL ){
-        delete [] buffer;
-        buffer = NULL;
+BufferedOutputStream::~BufferedOutputStream() {
+    try{
+        this->close();
     }
+    DECAF_CATCH_NOTHROW( IOException )
+    DECAF_CATCHALL_NOTHROW()
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-void BufferedOutputStream::init( std::size_t bufSize ){
+void BufferedOutputStream::init( std::size_t bufSize ) {
+
+    if( bufSize <= 0 ) {
+        throw new IllegalArgumentException(
+            __FILE__, __LINE__,
+            "BufferedOutputStream::init - Size must be greater than zero");
+    }
 
     this->bufferSize = bufSize;
 
@@ -61,16 +73,25 @@
 ////////////////////////////////////////////////////////////////////////////////
 void BufferedOutputStream::close() throw( lang::Exception ){
 
-    // Flush this stream.
-    flush();
+    // let parent close the inputStream
+    FilterOutputStream::close();
 
-    // Close the delegate stream.
-    outputStream->close();
+    // Destroy the buffer.
+    if( buffer != NULL ){
+        delete [] buffer;
+        buffer = NULL;
+    }
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void BufferedOutputStream::emptyBuffer() throw ( IOException ){
 
+    if( outputStream == NULL ) {
+        throw IOException(
+            __FILE__, __LINE__,
+            "BufferedOutputStream::emptyBuffer - OutputStream is closed" );
+    }
+
     if( head != tail ){
         outputStream->write( buffer+head, tail-head );
     }
@@ -80,44 +101,90 @@
 ////////////////////////////////////////////////////////////////////////////////
 void BufferedOutputStream::flush() throw ( IOException ){
 
-    // Empty the contents of the buffer to the output stream.
-    emptyBuffer();
+    try {
 
-    // Flush the output stream.
-    outputStream->flush();
+        if( isClosed() ){
+            throw IOException(
+                __FILE__, __LINE__,
+                "BufferedOutputStream::write - Stream is clsoed" );
+        }
+
+        // Empty the contents of the buffer to the output stream.
+        emptyBuffer();
+
+        // Flush the output stream.
+        outputStream->flush();
+    }
+    DECAF_CATCH_RETHROW( IOException )
+    DECAF_CATCHALL_THROW( IOException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void BufferedOutputStream::write( const unsigned char c ) throw ( IOException ){
 
-    if( tail >= bufferSize ){
-        emptyBuffer();
-    }
+    try{
+
+        if( isClosed() ){
+            throw IOException(
+                __FILE__, __LINE__,
+                "BufferedOutputStream::write - Stream is clsoed" );
+        }
+
+        if( tail >= bufferSize ){
+            emptyBuffer();
+        }
 
-    buffer[tail++] = c;
+        buffer[tail++] = c;
+    }
+    DECAF_CATCH_RETHROW( IOException )
+    DECAF_CATCHALL_THROW( IOException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 void BufferedOutputStream::write( const unsigned char* buffer, std::size_t len )
-    throw ( IOException )
-{
-    // Iterate until all the data is written.
-    for( std::size_t pos=0; pos < len; ){
+    throw ( IOException, lang::exceptions::NullPointerException ) {
 
-        if( tail >= bufferSize ){
-            emptyBuffer();
+    try{
+
+        if( isClosed() ){
+            throw IOException(
+                __FILE__, __LINE__,
+                "BufferedOutputStream::write - Stream is clsoed" );
+        }
+
+
+        if( buffer == NULL ) {
+            throw NullPointerException(
+                __FILE__, __LINE__,
+                "BufferedOutputStream::write - Buffer passed is Null.");
         }
 
-        // Get the number of bytes left to write.
-        std::size_t bytesToWrite = min( (int)bufferSize-tail, len-pos );
+        // Fast exit.
+        if( len == 0 ) {
+            return;
+        }
+
+        // Iterate until all the data is written.
+        for( std::size_t pos=0; pos < len; ){
+
+            if( tail >= bufferSize ){
+                emptyBuffer();
+            }
 
-        // Copy the data.
-        memcpy( this->buffer+tail, buffer+pos, bytesToWrite );
+            // Get the number of bytes left to write.
+            std::size_t bytesToWrite = min( (int)bufferSize-tail, len-pos );
 
-        // Increase the tail position.
-        tail += bytesToWrite;
+            // Copy the data.
+            memcpy( this->buffer+tail, buffer+pos, bytesToWrite );
 
-        // Decrease the number of bytes to write.
-        pos += bytesToWrite;
+            // Increase the tail position.
+            tail += bytesToWrite;
+
+            // Decrease the number of bytes to write.
+            pos += bytesToWrite;
+        }
     }
+    DECAF_CATCH_RETHROW( IOException )
+    DECAF_CATCH_RETHROW( NullPointerException )
+    DECAF_CATCHALL_THROW( IOException )
 }

Modified: activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedOutputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedOutputStream.h?rev=591481&r1=591480&r2=591481&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedOutputStream.h (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/BufferedOutputStream.h Fri Nov  2
14:40:45 2007
@@ -19,7 +19,7 @@
 #define _DECAF_IO_BUFFEREDOUTPUTSTREAM_H_
 
 #include <decaf/io/FilterOutputStream.h>
-#include <assert.h>
+#include <decaf/lang/exceptions/IllegalArgumentException.h>
 
 namespace decaf{
 namespace io{
@@ -67,7 +67,8 @@
          */
         BufferedOutputStream( OutputStream* stream,
                               std::size_t bufSize,
-                              bool own = false);
+                              bool own = false )
+            throw ( lang::exceptions::IllegalArgumentException );
 
         virtual ~BufferedOutputStream();
 
@@ -83,9 +84,10 @@
          * @param buffer The array of bytes to write.
          * @param len The number of bytes from the buffer to be written.
          * @throws IOException thrown if an error occurs.
+         * @throws NullPointerException thrown if buffer is Null.
          */
         virtual void write( const unsigned char* buffer, std::size_t len )
-            throw ( IOException );
+            throw ( IOException, lang::exceptions::NullPointerException );
 
         /**
          * Invokes flush on the target output stream.

Modified: activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/FilterOutputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/FilterOutputStream.h?rev=591481&r1=591480&r2=591481&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/FilterOutputStream.h (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/FilterOutputStream.h Fri Nov  2 14:40:45
2007
@@ -21,6 +21,7 @@
 #include <decaf/io/OutputStream.h>
 #include <decaf/io/IOException.h>
 #include <decaf/util/concurrent/Mutex.h>
+#include <decaf/lang/exceptions/NullPointerException.h>
 
 namespace decaf{
 namespace io{
@@ -45,8 +46,7 @@
      *
      *  DataOutputStream os = new DataOutputStream( new OutputStream(), true )
      */
-    class DECAF_API FilterOutputStream : public OutputStream
-    {
+    class DECAF_API FilterOutputStream : public OutputStream {
     protected:
 
         // The output Stream to wrap
@@ -58,6 +58,9 @@
         // Indicates if we own the wrapped stream
         bool own;
 
+        // Indicates that this stream was closed
+        bool closed;
+
     public:
 
         /**
@@ -69,11 +72,12 @@
         FilterOutputStream( OutputStream* outputStream, bool own = false ){
             this->outputStream = outputStream;
             this->own = own;
+            this->closed = false;
         }
 
         virtual ~FilterOutputStream() {
             try {
-                if( own == true ) delete outputStream;
+                this->close();
             }
             DECAF_CATCH_NOTHROW( IOException )
             DECAF_CATCHALL_NOTHROW( )
@@ -101,11 +105,20 @@
          * @param buffer The array of bytes to write.
          * @param len The number of bytes from the buffer to be written.
          * @throws IOException thrown if an error occurs.
+         * @throws NullPointerException thrown if buffer is Null.
          */
-        virtual void write( const unsigned char* buffer, std::size_t len ) throw ( IOException
) {
+        virtual void write( const unsigned char* buffer, std::size_t len )
+            throw ( IOException,
+                    lang::exceptions::NullPointerException ) {
             try {
-                for( std::size_t ix = 0; ix < len; ++ix )
-                {
+
+                if( buffer == NULL ) {
+                    throw lang::exceptions::NullPointerException(
+                        __FILE__, __LINE__,
+                        "FilterOutputStream::write - Buffer passed is Null.");
+                }
+
+                for( std::size_t ix = 0; ix < len; ++ix ) {
                     outputStream->write( buffer[ix] );
                 }
             }
@@ -128,13 +141,22 @@
         }
 
         /**
-         * Close the Stream, the FilterOutputStream simply calls the close
-         * method of the underlying stream
-         * @throws CMSException
+         * Close the Stream.  The close method of FilterOutputStream calls its
+         * flush method, and then calls the close method of its underlying output
+         * stream, it then destroys the output stream if it is the owner.
+         * @throws Exception
          */
         virtual void close() throw ( lang::Exception ) {
             try {
-                outputStream->close();
+                if( outputStream != NULL ) {
+                    outputStream->flush();
+                    outputStream->close();
+                    if( own ) {
+                        delete outputStream;
+                    }
+                    outputStream = NULL;
+                }
+                this->closed = true;
             }
             DECAF_CATCH_RETHROW( IOException )
             DECAF_CATCHALL_THROW( IOException )
@@ -200,6 +222,15 @@
          */
         virtual void notifyAll() throw( lang::Exception ){
             mutex.notifyAll();
+        }
+
+    protected:
+
+        /**
+         * @returns true if this stream has been closed.
+         */
+        virtual bool isClosed() const {
+            return this->closed;
         }
 
     };

Modified: activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/OutputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/OutputStream.h?rev=591481&r1=591480&r2=591481&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/OutputStream.h (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/decaf/io/OutputStream.h Fri Nov  2 14:40:45
2007
@@ -22,6 +22,7 @@
 #include <decaf/io/IOException.h>
 #include <decaf/util/concurrent/Synchronizable.h>
 #include <decaf/util/Config.h>
+#include <decaf/lang/exceptions/NullPointerException.h>
 
 namespace decaf{
 namespace io{
@@ -48,9 +49,10 @@
          * @param buffer The array of bytes to write.
          * @param len The number of bytes from the buffer to be written.
          * @throws IOException thrown if an error occurs.
+         * @throws NullPointerException thrown if buffer is Null.
          */
         virtual void write( const unsigned char* buffer, std::size_t len )
-            throw ( IOException ) = 0;
+            throw ( IOException, lang::exceptions::NullPointerException ) = 0;
 
         /**
          * Flushes any pending writes in this output stream.



Mime
View raw message