activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r595332 - in /activemq/activemq-cpp/decaf/trunk/src/main: Makefile.am decaf/internal/nio/ByteArray.cpp decaf/internal/nio/ByteArray.h
Date Thu, 15 Nov 2007 15:35:13 GMT
Author: tabish
Date: Thu Nov 15 07:35:12 2007
New Revision: 595332

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

Starting the NIO implementation

Modified:
    activemq/activemq-cpp/decaf/trunk/src/main/Makefile.am
    activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArray.cpp
    activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArray.h

Modified: activemq/activemq-cpp/decaf/trunk/src/main/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/Makefile.am?rev=595332&r1=595331&r2=595332&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/Makefile.am (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/Makefile.am Thu Nov 15 07:35:12 2007
@@ -23,6 +23,7 @@
    decaf/internal/util/NumberConverter.cpp \
    decaf/internal/util/FloatingPointParser.cpp \
    decaf/internal/util/HexStringParser.cpp \
+   decaf/internal/nio/ByteArray.cpp \
    decaf/net/ServerSocket.cpp \
    decaf/net/SocketOutputStream.cpp \
    decaf/net/BufferedSocket.cpp \
@@ -185,6 +186,7 @@
    decaf/internal/util/HexStringParser.h \
    decaf/internal/util/BitOps.h \
    decaf/internal/util/BigInt.h \
+   decaf/internal/nio/ByteArray.h \
    decaf/nio/Buffer.h \
    decaf/nio/ByteBuffer.h \
    decaf/nio/BufferOverflowException.h \

Modified: activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArray.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArray.cpp?rev=595332&r1=595331&r2=595332&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArray.cpp (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArray.cpp Thu Nov 15
07:35:12 2007
@@ -17,25 +17,137 @@
 
 #include "ByteArray.h"
 
+#include <decaf/lang/Math.h>
+
 using namespace decaf;
+using namespace decaf::nio;
 using namespace decaf::internal;
 using namespace decaf::internal::nio;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
 
 ////////////////////////////////////////////////////////////////////////////////
-ByteArray::ByteArray( unsigned char* buffer, std::size_t size, bool own ) {
+ByteArray::ByteArray( unsigned char* array, std::size_t capacity, bool own )
+    throw( lang::exceptions::NullPointerException ) {
 
-    if( buffer == NULL ) {
-        throw NullPointerException(
-            __FILE__, __LINE__,
-            "ByteArray::ByteArray - Passed Buffer is null" );
-    }
+    try{
+
+        if( array == NULL ) {
+            throw NullPointerException(
+                __FILE__, __LINE__,
+                "ByteArray::ByteArray - Passed Buffer is null" );
+        }
 
-    this->buffer = buffer;
-    this->size = size;
-    this->own = own;
+        this->array = array;
+        this->capacity = capacity;
+        this->own = own;
+    }
+    DECAF_CATCH_RETHROW( NullPointerException )
+    DECAF_CATCHALL_THROW( NullPointerException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 ByteArray::~ByteArray() {
 
+    try{
+
+        if( own ) {
+            delete array;
+        }
+    }
+    DECAF_CATCH_NOTHROW( Exception )
+    DECAF_CATCHALL_NOTHROW()
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArray::read( unsigned char* buffer, std::size_t offset, std::size_t length )
+    throw( NullPointerException, BufferUnderflowException ) {
+
+    try{
+
+        if( length == 0 ) {
+            return;
+        }
+
+        if( buffer == NULL ) {
+            throw NullPointerException(
+                __FILE__, __LINE__,
+                "ByteArray::write - Passed buffer is null" );
+        }
+
+        if( ( offset + length ) > this->capacity ) {
+            throw BufferUnderflowException(
+                __FILE__, __LINE__,
+                "ByteArray::read - Not enough data to fill request." );
+        }
+
+        // Read, starting at offset, length number of bytes to Buffer
+        memcpy( buffer, this->array + offset, length );
+    }
+    DECAF_CATCH_RETHROW( NullPointerException )
+    DECAF_CATCH_RETHROW( BufferUnderflowException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferUnderflowException )
+    DECAF_CATCHALL_THROW( BufferUnderflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArray::write( unsigned char* buffer, std::size_t offset, std::size_t length )
+    throw( NullPointerException, BufferOverflowException ) {
+
+    try{
+
+        if( length == 0 ) {
+            return;
+        }
+
+        if( buffer == NULL ) {
+            throw NullPointerException(
+                __FILE__, __LINE__,
+                "ByteArray::write - Passed buffer is null" );
+        }
+
+        if( ( offset + length ) > this->capacity ) {
+            throw BufferOverflowException(
+                __FILE__, __LINE__,
+                "ByteArray::read - Not room in array to hold request." );
+        }
+
+        // Write, starting at offset, length number of bytes from buffer.
+        memcpy( this->array + offset, buffer, length );
+    }
+    DECAF_CATCH_RETHROW( NullPointerException )
+    DECAF_CATCH_RETHROW( BufferOverflowException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferOverflowException )
+    DECAF_CATCHALL_THROW( BufferOverflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArray::resize( std::size_t capacity )
+    throw ( lang::exceptions::InvalidStateException ) {
+
+    try{
+
+        if( !own ) {
+            throw InvalidStateException(
+                __FILE__, __LINE__,
+                "ByteArray::resize - Not the array owner, can't resize" );
+        }
+
+        // Save old state
+        std::size_t oldCapacity = this->capacity;
+        unsigned char* oldArray = this->array;
+
+        // Resize and copy as much of the old as we can back and delete old array
+        this->array = new unsigned char[capacity];
+        this->capacity = capacity;
+        memcpy( this->array, oldArray, Math::min( (int)oldCapacity, (int)capacity ) );
+        delete oldArray;
+    }
+    DECAF_CATCH_RETHROW( InvalidStateException )
+    DECAF_CATCHALL_THROW( InvalidStateException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArray::clear() {
+    memset( this->array, this->capacity, 0 );
 }

Modified: activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArray.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArray.h?rev=595332&r1=595331&r2=595332&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArray.h (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArray.h Thu Nov 15 07:35:12
2007
@@ -18,7 +18,10 @@
 #ifndef _DECAF_INTERNAL_NIO_BYTEARRAY_H_
 #define _DECAF_INTERNAL_NIO_BYTEARRAY_H_
 
+#include <decaf/lang/exceptions/InvalidStateException.h>
 #include <decaf/lang/exceptions/NullPointerException.h>
+#include <decaf/nio/BufferUnderflowException.h>
+#include <decaf/nio/BufferOverflowException.h>
 
 namespace decaf{
 namespace internal{
@@ -35,7 +38,7 @@
     private:
 
         // Buffer to read and write to, may be shared with other instances.
-        unsigned char* buffer;
+        unsigned char* array;
 
         // Size of the Buffer
         std::size_t capacity;
@@ -49,11 +52,91 @@
     public:
 
         /**
-         *
+         * Creates a byte array object that wraps the given array.  If the own flag
+         * is set then it will delete this array when this object is deleted.
+         * @param array - array to wrap
+         * @param capacity - size of the array, this is the limit we read and write to.
+         * @param own - is this class now the owner of the pointer.
          * @throws NullPointerException if buffer is NULL
          */
-        ByteArray( unsigned char* buffer, std::size_t size, bool own = false );
+        ByteArray( unsigned char* array, std::size_t capacity, bool own = false )
+            throw( lang::exceptions::NullPointerException );
+
+        /**
+         * Create a new byte array object which allocates an internal buffer with
+         * the given capacity.
+         * @param capacity - amount of space to
+         */
+        ByteArray( std::size_t capacity );
+
         virtual ~ByteArray();
+
+        /**
+         * Gets the capacity of the underlying array.
+         * @return the size the array.
+         */
+        virtual std::size_t getCapactiy() const {
+            return this->capacity;
+        }
+
+        /**
+         * Gets the pointer to the array we are wrapping.  Changes to the data in this
+         * array are reflected by all ByteArray objects that point to this array.
+         * @returns an unsigned char* pointer to the array this object wraps.
+         */
+        virtual unsigned char* getByteArray() {
+            return this->array;
+        }
+
+        /**
+         * Reads from the Byte array starting at the specified offset and reading
+         * the specified length.  If the length is greater than the capacity of this
+         * underlying byte array then an BufferUnderflowException is thrown.
+         * @param buffer - the buffer to read data from this array into.
+         * @param offset - postition in this array to start reading from.
+         * @param length - the amount of data to read from this array.
+         * @throws NullPointerException if buffer is null
+         * @throws BufferUnderflowException if there is not enought data to read
+         * because the offset or the length is greater than the size of this array.
+         */
+        virtual void read( unsigned char* buffer, std::size_t offset, std::size_t length
)
+            throw( decaf::lang::exceptions::NullPointerException,
+                   decaf::nio::BufferUnderflowException );
+
+        /**
+         * Writes from the Byte array given, starting at the specified offset and writing
+         * the specified amoutn of data into this objects internal array..  If the length
+         * is greater than the capacity of this underlying byte array then an
+         * BufferOverflowException is thrown.
+         * @param buffer - the buffer to write get data written into this array.
+         * @param offset - postition in this array to start writing from.
+         * @param length - the amount of data to write to this array.
+         * @throws NullPointerException if buffer is null
+         * @throws BufferOverflowException if the amount of data to be written to this
+         * array or the offset given are larger than this array's capacity.
+         */
+        virtual void write( unsigned char* buffer, std::size_t offset, std::size_t length
)
+            throw( decaf::lang::exceptions::NullPointerException,
+                   decaf::nio::BufferOverflowException );
+
+        /**
+         * Resizes the underlying array to the new given capacity, preserving all the
+         * Data that was previouly in the array, unless the resize is smaller than the
+         * current size in which case only the data that will fit into the new array is
+         * preserved.
+         * <p>
+         * A ByteArray can only be resized when it owns the underlying array, if it does
+         * not then it will throw an IllegalStateException.
+         * @param capacity - the new capacity of the array.
+         * @throws InvalidStateException if this object does not own the buffer.
+         */
+        virtual void resize( std::size_t capacity )
+            throw ( lang::exceptions::InvalidStateException );
+
+        /**
+         * Clear all data from that Array, setting the underlying bytes to zero.
+         */
+        virtual void clear();
 
     };
 



Mime
View raw message