activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r652104 [14/29] - in /activemq/activemq-cpp/trunk: ./ m4/ src/examples/ src/examples/consumers/ src/main/ src/main/decaf/ src/main/decaf/internal/ src/main/decaf/internal/net/ src/main/decaf/internal/nio/ src/main/decaf/internal/util/ src/m...
Date Tue, 29 Apr 2008 20:52:37 GMT
Added: activemq/activemq-cpp/trunk/src/main/decaf/nio/Buffer.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/nio/Buffer.h?rev=652104&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/nio/Buffer.h (added)
+++ activemq/activemq-cpp/trunk/src/main/decaf/nio/Buffer.h Tue Apr 29 13:52:30 2008
@@ -0,0 +1,275 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _DECAF_NIO_BUFFER_H_
+#define _DECAF_NIO_BUFFER_H_
+
+#include <decaf/lang/exceptions/IllegalArgumentException.h>
+#include <decaf/nio/InvalidMarkException.h>
+
+namespace decaf{
+namespace nio{
+
+    /**
+     * A container for data of a specific primitive type.
+     *
+     * A buffer is a linear, finite sequence of elements of a specific primitive
+     * type. Aside from its content, the essential properties of a buffer are
+     * its capacity, limit, and position:
+     *
+     * A buffer's capacity is the number of elements it contains. The capacity of a
+     * buffer is never negative and never changes.
+     *
+     * A buffer's limit is the index of the first element that should not be read
+     * or written. A buffer's limit is never negative and is never greater than its
+     * capacity.
+     *
+     * A buffer's position is the index of the next element to be read or written.
+     * A buffer's position is never negative and is never greater than its limit.
+     *
+     * There is one subclass of this class for each non-boolean primitive type.
+     *
+     * Transferring data:
+     *   Each subclass of this class defines two categories of get and put operations:
+     *   * Relative operations read or write one or more elements starting at the
+     *     current position and then increment the position by the number of
+     *     elements transferred. If the requested transfer exceeds the limit then a
+     *     relative get operation throws a BufferUnderflowException and a relative
+     *     put operation throws a BufferOverflowException; in either case, no data
+     *     is transferred.
+     *   * Absolute operations take an explicit element index and do not affect the
+     *     position. Absolute get and put operations throw an IndexOutOfBoundsException
+     *     if the index argument exceeds the limit.
+     *
+     *  Data may also, of course, be transferred in to or out of a buffer by the I/O
+     *  operations of an appropriate channel, which are always relative to the current
+     *  position.
+     *
+     * Marking and resetting:
+     *
+     *   A buffer's mark is the index to which its position will be reset when the
+     *   reset method is invoked. The mark is not always defined, but when it is
+     *   defined it is never negative and is never greater than the position. If the
+     *   mark is defined then it is discarded when the position or the limit is
+     *   adjusted to a value smaller than the mark. If the mark is not defined then
+     *   invoking the reset method causes an InvalidMarkException to be thrown.
+     *
+     * Invariants:
+     *
+     *   The following invariant holds for the mark, position, limit, and capacity values:
+     *     0 <= mark <= position <= limit <= capacity
+     *
+     *   A newly-created buffer always has a position of zero and a mark that is
+     *   undefined.  The initial limit may be zero, or it may be some other value that
+     *   depends upon the type of the buffer and the manner in which it is constructed.
+     *   The initial content of a buffer is, in general, undefined.
+     *
+     * Clearing, flipping, and rewinding:
+     *
+     *   In addition to methods for accessing the position, limit, and capacity values
+     *   and for marking and resetting, this class also defines the following operations
+     *   upon buffers:
+     *
+     *    clear() makes a buffer ready for a new sequence of channel-read or relative
+     *    put operations: It sets the limit to the capacity and the position to zero.
+     *
+     *    flip() makes a buffer ready for a new sequence of channel-write or relative
+     *    get operations: It sets the limit to the current position and then sets the
+     *    position to zero.
+     *
+     *    rewind() makes a buffer ready for re-reading the data that it already
+     *    contains: It leaves the limit unchanged and sets the position to zero.
+     *
+     * Read-only buffers:
+     *
+     *   Every buffer is readable, but not every buffer is writable. The mutation
+     *   methods of each buffer class are specified as optional operations that will
+     *   throw a ReadOnlyBufferException when invoked upon a read-only buffer. A
+     *   read-only buffer does not allow its content to be changed, but its mark,
+     *   position, and limit values are mutable. Whether or not a buffer is read-only
+     *   may be determined by invoking its isReadOnly method.
+     *
+     * Thread safety:
+     *
+     *   Buffers are not safe for use by multiple concurrent threads. If a buffer is to
+     *   be used by more than one thread then access to the buffer should be controlled
+     *   by appropriate synchronization.
+     *
+     * Invocation chaining:
+     *
+     *   Methods in this class that do not otherwise have a value to return are specified
+     *   to return the buffer upon which they are invoked. This allows method invocations
+     *   to be chained; for example, the sequence of statements
+     *
+     *     b.flip();
+     *     b.position(23);
+     *     b.limit(42);
+     *
+     *   can be replaced by the single, more compact statement
+     *     b.flip().position(23).limit(42);
+     */
+    class DECAF_API Buffer {
+    protected:
+
+        mutable std::size_t _position;
+        std::size_t _capacity;
+        std::size_t _limit;
+        std::size_t _mark;
+        bool _markSet;
+
+    public:
+
+        Buffer( std::size_t capactiy );
+        Buffer( const Buffer& other );
+        virtual ~Buffer() {}
+
+    public:
+
+        /**
+         * @returns this buffer's capacity.
+         */
+        virtual std::size_t capacity() const {
+            return this->_capacity;
+        }
+
+        /**
+         * @returns the current position in the buffer
+         */
+        virtual std::size_t position() const {
+            return this->_position;
+        }
+
+        /**
+         * Sets this buffer's position. If the mark is defined and larger than the
+         * new position then it is discarded.
+         * @param newPosition - the new postion in the buffer to set.
+         * @returns A reference to This buffer
+         * @throws IllegalArgumentException if preconditions on the new pos don't hold.
+         */
+        virtual Buffer& position( std::size_t newPosition )
+            throw( lang::exceptions::IllegalArgumentException );
+
+        /**
+         * @returns this buffers Limit
+         */
+        virtual std::size_t limit() const {
+            return this->_limit;
+        }
+
+        /**
+         * Sets this buffer's limit. If the position is larger than the new limit then
+         * it is set to the new limit. If the mark is defined and larger than the new
+         * limit then it is discarded.
+         * @param newLimit - The new limit value; must be no larger than this
+         * buffer's capacity
+         * @returns A reference to This buffer
+         * @throws IllegalArgumentException if preconditions on the new pos don't hold.
+         */
+        virtual Buffer& limit( std::size_t newLimit )
+            throw( lang::exceptions::IllegalArgumentException );
+
+        /**
+         * Sets this buffer's mark at its position.
+         * @returns a reference to this buffer.
+         */
+        virtual Buffer& mark();
+
+        /**
+         * Resets this buffer's position to the previously-marked position.
+         * @returns a reference to this buffer.
+         * @throws InvalidMarkException - If the mark has not been set
+         */
+        virtual Buffer& reset() throw( InvalidMarkException );
+
+        /**
+         * Clears this buffer. The position is set to zero, the limit is set to the
+         * capacity, and the mark is discarded.
+         * <p>
+         * Invoke this method before using a sequence of channel-read or put operations
+         * to fill this buffer. For example:
+         *
+         *    buf.clear();     // Prepare buffer for reading
+         *    in.read(buf);    // Read data
+         *
+         * This method does not actually erase the data in the buffer, but it is named
+         * as if it did because it will most often be used in situations in which that
+         * might as well be the case.
+         * @returns a reference to this buffer.
+         */
+        virtual Buffer& clear();
+
+        /**
+         * Flips this buffer. The limit is set to the current position and then the
+         * position is set to zero. If the mark is defined then it is discarded.
+         * <p>
+         * After a sequence of channel-read or put operations, invoke this method to
+         * prepare for a sequence of channel-write or relative get operations. For
+         * example:
+         *
+         *     buf.put(magic);    // Prepend header
+         *     in.read(buf);      // Read data into rest of buffer
+         *     buf.flip();        // Flip buffer
+         *     out.write(buf);    // Write header + data to channel
+         *
+         * This method is often used in conjunction with the compact method when
+         * transferring data from one place to another.
+         * @returns a reference to this buffer.
+         */
+        virtual Buffer& flip();
+
+        /**
+         * Rewinds this buffer. The position is set to zero and the mark is discarded.
+         * <p>
+         * Invoke this method before a sequence of channel-write or get operations,
+         * assuming that the limit has already been set appropriately. For example:
+         *
+         *    out.write(buf);    // Write remaining data
+         *    buf.rewind();      // Rewind buffer
+         *    buf.get(array);    // Copy data into array
+         *
+         * @returns a reference to this buffer.
+         */
+        virtual Buffer& rewind();
+
+        /**
+         * Returns the number of elements between the current position and the limit.
+         * @returns The number of elements remaining in this buffer
+         */
+        virtual std::size_t remaining() const {
+            return _limit - _position;
+        }
+
+        /**
+         * Tells whether there are any elements between the current position and the limit.
+         * @returns true if, and only if, there is at least one element remaining in
+         * this buffer
+         */
+        virtual bool hasRemaining() const {
+            return remaining() != 0;
+        }
+
+        /**
+         * Tells whether or not this buffer is read-only.
+         * @returns true if, and only if, this buffer is read-only
+         */
+        virtual bool isReadOnly() const = 0;
+
+    };
+
+}}
+
+#endif /*_DECAF_NIO_BUFFER_H_*/

Added: activemq/activemq-cpp/trunk/src/main/decaf/nio/BufferOverflowException.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/nio/BufferOverflowException.h?rev=652104&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/nio/BufferOverflowException.h (added)
+++ activemq/activemq-cpp/trunk/src/main/decaf/nio/BufferOverflowException.h Tue Apr 29 13:52:30 2008
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _DECAF_NIO_BUFFEROVERFLOWEXCEPTION_H_
+#define _DECAF_NIO_BUFFEROVERFLOWEXCEPTION_H_
+
+#include <decaf/util/Config.h>
+#include <decaf/lang/Exception.h>
+
+namespace decaf{
+namespace nio{
+
+    class DECAF_API BufferOverflowException : public lang::Exception {
+    public:
+
+        /**
+         * Default Constructor
+         */
+        BufferOverflowException() throw() {}
+
+        /**
+         * Copy Constructor
+         * @param ex the exception to copy
+         */
+        BufferOverflowException( const lang::Exception& ex ) throw()
+        : lang::Exception() {
+            *(lang::Exception*)this = ex;
+        }
+
+        /**
+         * Copy Constructor
+         * @param ex the exception to copy, which is an instance of this type
+         */
+        BufferOverflowException( const BufferOverflowException& ex ) throw()
+        : lang::Exception() {
+            *(lang::Exception*)this = ex;
+        }
+
+        /**
+         * Consturctor
+         * @param file name of the file were the exception occured.
+         * @param lineNumber line where the exception occured
+         * @param msg the message that was generated
+         */
+        BufferOverflowException( const char* file, const int lineNumber,
+                                 const char* msg, ... ) throw()
+        : lang::Exception() {
+
+            va_list vargs;
+            va_start( vargs, msg );
+            buildMessage( msg, vargs );
+
+            // Set the first mark for this exception.
+            setMark( file, lineNumber );
+        }
+
+        /**
+         * Clones this exception.  This is useful for cases where you need
+         * to preserve the type of the original exception as well as the message.
+         * All subclasses should override.
+         */
+        virtual BufferOverflowException* clone() const {
+            return new BufferOverflowException( *this );
+        }
+
+        virtual ~BufferOverflowException() throw() {}
+
+    };
+
+}}
+
+#endif /*_DECAF_NIO_BUFFEROVERFLOWEXCEPTION_H_*/

Added: activemq/activemq-cpp/trunk/src/main/decaf/nio/BufferUnderflowException.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/nio/BufferUnderflowException.h?rev=652104&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/nio/BufferUnderflowException.h (added)
+++ activemq/activemq-cpp/trunk/src/main/decaf/nio/BufferUnderflowException.h Tue Apr 29 13:52:30 2008
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _DECAF_NIO_BUFFERUNDERFLOWEXCEPTION_H_
+#define _DECAF_NIO_BUFFERUNDERFLOWEXCEPTION_H_
+
+#include <decaf/util/Config.h>
+#include <decaf/lang/Exception.h>
+
+namespace decaf{
+namespace nio{
+
+    class DECAF_API BufferUnderflowException : public lang::Exception {
+    public:
+
+        /**
+         * Default Constructor
+         */
+        BufferUnderflowException() throw() {}
+
+        /**
+         * Copy Constructor
+         * @param ex the exception to copy
+         */
+        BufferUnderflowException( const lang::Exception& ex ) throw()
+        : lang::Exception() {
+            *(lang::Exception*)this = ex;
+        }
+
+        /**
+         * Copy Constructor
+         * @param ex the exception to copy, which is an instance of this type
+         */
+        BufferUnderflowException( const BufferUnderflowException& ex ) throw()
+        : lang::Exception() {
+            *(lang::Exception*)this = ex;
+        }
+
+        /**
+         * Consturctor
+         * @param file name of the file were the exception occured.
+         * @param lineNumber line where the exception occured
+         * @param msg the message that was generated
+         */
+        BufferUnderflowException( const char* file, const int lineNumber,
+                                  const char* msg, ... ) throw()
+        : lang::Exception() {
+            va_list vargs;
+            va_start( vargs, msg );
+            buildMessage( msg, vargs );
+
+            // Set the first mark for this exception.
+            setMark( file, lineNumber );
+        }
+
+        /**
+         * Clones this exception.  This is useful for cases where you need
+         * to preserve the type of the original exception as well as the message.
+         * All subclasses should override.
+         */
+        virtual BufferUnderflowException* clone() const {
+            return new BufferUnderflowException( *this );
+        }
+
+        virtual ~BufferUnderflowException() throw() {}
+
+    };
+
+}}
+
+#endif /*_DECAF_NIO_BUFFERUNDERFLOWEXCEPTION_H_*/

Added: activemq/activemq-cpp/trunk/src/main/decaf/nio/ByteBuffer.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/nio/ByteBuffer.cpp?rev=652104&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/nio/ByteBuffer.cpp (added)
+++ activemq/activemq-cpp/trunk/src/main/decaf/nio/ByteBuffer.cpp Tue Apr 29 13:52:30 2008
@@ -0,0 +1,314 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ByteBuffer.h"
+#include "decaf/internal/nio/BufferFactory.h"
+#include "decaf/lang/Short.h"
+#include "decaf/lang/Integer.h"
+#include "decaf/lang/Long.h"
+#include "decaf/lang/Float.h"
+#include "decaf/lang/Double.h"
+
+using namespace std;
+using namespace decaf;
+using namespace decaf::nio;
+using namespace decaf::internal::nio;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
+
+////////////////////////////////////////////////////////////////////////////////
+ByteBuffer::ByteBuffer( std::size_t capacity ) : Buffer( capacity ) {
+}
+
+////////////////////////////////////////////////////////////////////////////////
+ByteBuffer* ByteBuffer::allocate( std::size_t capacity ) {
+
+    try{
+
+        return BufferFactory::createByteBuffer( capacity );
+    }
+    DECAF_CATCH_RETHROW( Exception )
+    DECAF_CATCHALL_THROW( Exception )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+ByteBuffer* ByteBuffer::wrap( unsigned char* buffer, std::size_t offset, std::size_t length )
+    throw( lang::exceptions::NullPointerException ) {
+
+    try{
+
+        if( buffer == NULL ) {
+            throw NullPointerException(
+                __FILE__, __LINE__,
+                "ByteBuffer::wrap - Passed Buffer is Null.");
+        }
+
+        return BufferFactory::createByteBuffer( buffer, offset, length );
+    }
+    DECAF_CATCH_RETHROW( NullPointerException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, NullPointerException )
+    DECAF_CATCHALL_THROW( NullPointerException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+ByteBuffer* ByteBuffer::wrap( std::vector<unsigned char>& buffer ) {
+
+    try{
+
+        if( buffer.empty() ) {
+            throw NullPointerException(
+                __FILE__, __LINE__,
+                "ByteBuffer::wrap - Passed Buffer is Empty.");
+        }
+
+        return BufferFactory::createByteBuffer( &buffer[0], 0, buffer.size() );
+    }
+    DECAF_CATCH_RETHROW( NullPointerException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, NullPointerException )
+    DECAF_CATCHALL_THROW( NullPointerException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+ByteBuffer& ByteBuffer::get( std::vector<unsigned char> buffer )
+    throw ( BufferUnderflowException ) {
+
+    try{
+
+        if( !buffer.empty() ) {
+            this->get( &buffer[0], 0, buffer.size() );
+        }
+
+        return *this;
+    }
+    DECAF_CATCH_RETHROW( BufferUnderflowException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferUnderflowException )
+    DECAF_CATCHALL_THROW( BufferUnderflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+ByteBuffer& ByteBuffer::get( unsigned char* buffer,
+                             std::size_t offset,
+                             std::size_t length )
+    throw( BufferUnderflowException, NullPointerException ) {
+
+    try{
+
+        if( length == 0 ) {
+            return *this;
+        }
+
+        if( buffer == NULL ) {
+            throw NullPointerException(
+                __FILE__, __LINE__,
+                "ByteBuffer::get - Passed Buffer is Null.");
+        }
+
+        if( length > this->remaining() ) {
+            throw BufferUnderflowException(
+                __FILE__, __LINE__,
+                "ByteBuffer::get - Not Enough Data to Fill Request.");
+        }
+
+        // read length bytes starting from the offset
+        for( std::size_t ix = 0; ix < length; ++ix ) {
+            buffer[ix + offset] = this->get();
+        }
+
+        return *this;
+    }
+    DECAF_CATCH_RETHROW( BufferUnderflowException )
+    DECAF_CATCH_RETHROW( NullPointerException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferUnderflowException )
+    DECAF_CATCHALL_THROW( BufferUnderflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+ByteBuffer& ByteBuffer::put( ByteBuffer& src )
+    throw( BufferOverflowException, ReadOnlyBufferException, IllegalArgumentException ) {
+
+    try{
+
+        if( this == &src ) {
+            throw IllegalArgumentException(
+                __FILE__, __LINE__,
+                "ByteBuffer::put - Can't put Self" );
+        }
+
+        if( this->isReadOnly() ) {
+            throw ReadOnlyBufferException(
+                __FILE__, __LINE__,
+                "ByteBuffer::put - This buffer is Read Only.");
+        }
+
+        if( src.remaining() > this->remaining() ) {
+            throw BufferOverflowException(
+                __FILE__, __LINE__,
+                "ByteBuffer::put - Not enough space remaining to put src." );
+        }
+
+        while( src.hasRemaining() ) {
+            this->put( src.get() );
+        }
+
+        return *this;
+    }
+    DECAF_CATCH_RETHROW( BufferOverflowException )
+    DECAF_CATCH_RETHROW( ReadOnlyBufferException )
+    DECAF_CATCH_RETHROW( IllegalArgumentException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferOverflowException )
+    DECAF_CATCHALL_THROW( BufferOverflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+ByteBuffer& ByteBuffer::put( const unsigned char* buffer,
+                             std::size_t offset,
+                             std::size_t length )
+    throw( BufferOverflowException, ReadOnlyBufferException, NullPointerException ) {
+
+    try{
+
+        if( length == 0 ) {
+            return *this;
+        }
+
+        if( this->isReadOnly() ) {
+            throw ReadOnlyBufferException(
+                __FILE__, __LINE__,
+                "ByteBuffer::put - This buffer is Read Only.");
+        }
+
+        if( buffer == NULL ) {
+            throw NullPointerException(
+                __FILE__, __LINE__,
+                "ByteBuffer::put - Passed Buffer is Null.");
+        }
+
+        if( length > this->remaining() ) {
+            throw BufferOverflowException(
+                __FILE__, __LINE__,
+                "ByteBuffer::put - Not Enough space to store requested Data.");
+        }
+
+        // read length bytes starting from the offset
+        for( std::size_t ix = 0; ix < length; ++ix ) {
+            this->put( buffer[ix + offset] );
+        }
+
+        return *this;
+    }
+    DECAF_CATCH_RETHROW( BufferOverflowException )
+    DECAF_CATCH_RETHROW( ReadOnlyBufferException )
+    DECAF_CATCH_RETHROW( NullPointerException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferOverflowException )
+    DECAF_CATCHALL_THROW( BufferOverflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+ByteBuffer& ByteBuffer::put( std::vector<unsigned char>& buffer )
+    throw( BufferOverflowException, ReadOnlyBufferException ) {
+
+    try{
+
+        if( !buffer.empty() ) {
+            this->put( &buffer[0], 0, buffer.size() );
+        }
+
+        return *this;
+    }
+    DECAF_CATCH_RETHROW( BufferOverflowException )
+    DECAF_CATCH_RETHROW( ReadOnlyBufferException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferOverflowException )
+    DECAF_CATCHALL_THROW( BufferOverflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int ByteBuffer::compareTo( const ByteBuffer& value ) const {
+
+    try{
+
+        std::size_t compareRemaining =
+            ( remaining() < value.remaining() ) ?
+                remaining() : value.remaining();
+
+        std::size_t thisPos = this->position();
+        std::size_t otherPos = value.position();
+
+        unsigned char thisByte, otherByte = 0;
+
+        while( compareRemaining > 0 ) {
+            thisByte = this->get( thisPos );
+            otherByte = value.get( otherPos );
+
+            if( thisByte != otherByte ) {
+                return thisByte < otherByte ? -1 : 1;
+            }
+
+            thisPos++;
+            otherPos++;
+            compareRemaining--;
+        }
+
+        return (int)( remaining() - value.remaining() );
+    }
+    DECAF_CATCH_RETHROW( Exception )
+    DECAF_CATCHALL_THROW( Exception )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool ByteBuffer::equals( const ByteBuffer& value ) const {
+
+    try{
+
+        if( this->remaining() != value.remaining()) {
+            return false;
+        }
+
+        std::size_t myPosition = this->position();
+        std::size_t otherPosition = value.position();
+
+        while( myPosition < this->limit() ) {
+            if( get( myPosition++ ) != value.get( otherPosition++ ) ) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+    DECAF_CATCH_RETHROW( Exception )
+    DECAF_CATCHALL_THROW( Exception )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool ByteBuffer::operator==( const ByteBuffer& value ) const {
+    return this->equals( value );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool ByteBuffer::operator<( const ByteBuffer& value ) const {
+    return this->compareTo( value ) == -1;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::string ByteBuffer::toString() const {
+
+    std::ostringstream stream;
+    stream << "ByteBuffer";
+    stream << ", status: capacity=" << this->capacity();
+    stream << " position=" << this->position();
+    stream << " limit=" << this->limit();
+    return stream.str();
+}

Added: activemq/activemq-cpp/trunk/src/main/decaf/nio/ByteBuffer.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/nio/ByteBuffer.h?rev=652104&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/nio/ByteBuffer.h (added)
+++ activemq/activemq-cpp/trunk/src/main/decaf/nio/ByteBuffer.h Tue Apr 29 13:52:30 2008
@@ -0,0 +1,813 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _DECAF_NIO_BYTEBUFFER_H_
+#define _DECAF_NIO_BYTEBUFFER_H_
+
+#include <decaf/nio/Buffer.h>
+#include <decaf/lang/Comparable.h>
+#include <decaf/lang/exceptions/NullPointerException.h>
+#include <decaf/lang/exceptions/IndexOutOfBoundsException.h>
+#include <decaf/nio/BufferUnderflowException.h>
+#include <decaf/nio/BufferOverflowException.h>
+#include <decaf/nio/ReadOnlyBufferException.h>
+#include <decaf/internal/nio/ByteArrayPerspective.h>
+
+namespace decaf{
+namespace nio{
+
+	class CharBuffer;
+	class DoubleBuffer;
+	class FloatBuffer;
+	class ShortBuffer;
+	class IntBuffer;
+	class LongBuffer;
+
+	/**
+	 * This class defines six categories of operations upon byte buffers:
+	 *
+	 *  1. Absolute and relative get and put methods that read and write single bytes;
+	 *  2. Relative bulk get methods that transfer contiguous sequences of bytes from
+	 *     this buffer into an array;
+	 *  3. Relative bulk put methods that transfer contiguous sequences of bytes from
+	 *     a byte array or some other byte buffer into this buffer;
+	 *  4. Absolute and relative get and put methods that read and write values of other
+	 *     primitive types, translating them to and from sequences of bytes in a
+	 *     particular byte order;
+	 *  5. Methods for creating view buffers, which allow a byte buffer to be viewed as
+	 *     a buffer containing values of some other primitive type; and
+	 *  6. Methods for compacting, duplicating, and slicing a byte buffer.
+	 *
+	 * Byte buffers can be created either by allocation, which allocates space for the
+	 * buffer's content, or by wrapping an existing byte array into a buffer.
+	 *
+	 * Access to binary data:
+	 *
+	 * This class defines methods for reading and writing values of all other primitive
+	 * types, except boolean. Primitive values are translated to (or from) sequences of
+	 * bytes according to the buffer's current byte order.
+	 *
+	 * For access to heterogeneous binary data, that is, sequences of values of
+	 * different types, this class defines a family of absolute and relative get and
+	 * put methods for each type. For 32-bit floating-point values, for example, this
+	 * class defines:
+	 *
+	 *   float getFloat()
+	 *   float getFloat(int index)
+	 *   void  putFloat(float f)
+	 *   void  putFloat(int index, float f)
+	 *
+	 * Corresponding methods are defined for the types char, short, int, long, and
+	 * double. The index parameters of the absolute get and put methods are in terms
+	 * of bytes rather than of the type being read or written.
+	 *
+	 * For access to homogeneous binary data, that is, sequences of values of the same
+	 * type, this class defines methods that can create views of a given byte buffer.
+	 * A view buffer is simply another buffer whose content is backed by the byte buffer.
+	 * Changes to the byte buffer's content will be visible in the view buffer, and vice
+	 * versa; the two buffers' position, limit, and mark values are independent. The
+	 * asFloatBuffer method, for example, creates an instance of the FloatBuffer class
+	 * that is backed by the byte buffer upon which the method is invoked. Corresponding
+	 * view-creation methods are defined for the types char, short, int, long, and double.
+	 *
+	 * View buffers have two important advantages over the families of type-specific
+	 * get and put methods described above:
+	 *
+	 *   A view buffer is indexed not in terms of bytes but rather in terms of the
+	 *   type-specific size of its values;
+	 *
+	 *   A view buffer provides relative bulk get and put methods that can transfer
+	 *   contiguous sequences of values between a buffer and an array or some other
+	 *   buffer of the same type; and
+	 *
+	 */
+	class DECAF_API ByteBuffer : public Buffer,
+								 public lang::Comparable<ByteBuffer> {
+	protected:
+
+		/**
+		 * Creates a ByteBuffer object that has its backing array allocated internally
+		 * and is then owned and deleted when this object is deleted.  The array is
+		 * initially created with all elements initialized to zero.
+		 * @param capacity - size of the array, this is the limit we read and write to.
+		 */
+		ByteBuffer( std::size_t capacity );
+
+	public:
+
+		virtual ~ByteBuffer() {}
+
+		/**
+		 * @returns a std::string describing this object
+		 */
+		virtual std::string toString() const;
+
+		/**
+		 * Relative bulk get method.
+		 * <p>
+		 * This method transfers bytes from this buffer into the given destination
+		 * vector. An invocation of this method of the form src.get(a) behaves in
+		 * exactly the same way as the invocation.  The vector must be sized to the
+		 * amount of data that is to be read, that is to say, the caller should call
+		 * buffer.resize( N ) before calling this get method.
+		 * @returns a reference to this Byte Buffer
+		 * @throws BufferUnderflowException - If there are fewer than length bytes
+		 * remaining in this buffer
+		 */
+		ByteBuffer& get( std::vector<unsigned char> buffer )
+			throw ( BufferUnderflowException );
+
+		/**
+		 * Relative bulk get method.
+		 * <p>
+		 * This method transfers bytes from this buffer into the given destination array.
+		 * If there are fewer bytes remaining in the buffer than are required to satisfy
+		 * the request, that is, if length > remaining(), then no bytes are transferred
+		 * and a BufferUnderflowException is thrown.
+		 * <p>
+		 * Otherwise, this method copies length bytes from this buffer into the given
+		 * array, starting at the current position of this buffer and at the given offset
+		 * in the array. The position of this buffer is then incremented by length.
+		 * <p>
+		 * @param buffer - pointer to an allocated buffer to fill
+		 * @param offset - position in the buffer to start filling
+		 * @param length - amount of data to put in the passed buffer
+		 * @returns a reference to this Buffer
+		 * @throws BufferUnderflowException - If there are fewer than length bytes
+		 * remaining in this buffer
+		 * @throws NullPointerException if the passed buffer is null.
+		 */
+		ByteBuffer& get( unsigned char* buffer,
+						 std::size_t offset,
+						 std::size_t length )
+			throw( BufferUnderflowException,
+				   lang::exceptions::NullPointerException );
+
+		/**
+		 * This method transfers the bytes remaining in the given source buffer into
+		 * this buffer. If there are more bytes remaining in the source buffer than in
+		 * this buffer, that is, if src.remaining() > remaining(), then no bytes are
+		 * transferred and a BufferOverflowException is thrown.
+		 * <p>
+		 * Otherwise, this method copies n = src.remaining() bytes from the given
+		 * buffer into this buffer, starting at each buffer's current position. The
+		 * positions of both buffers are then incremented by n.
+		 * @param src - the buffer to take bytes from an place in this one.
+		 * @returns a reference to this buffer
+		 * @throws BufferOverflowException - If there is insufficient space in this
+		 * buffer for the remaining bytes in the source buffer
+		 * @throws IllegalArgumentException - If the source buffer is this buffer
+		 * @throws ReadOnlyBufferException - If this buffer is read-only
+		 */
+		ByteBuffer& put( ByteBuffer& src )
+			throw( BufferOverflowException, ReadOnlyBufferException,
+				   lang::exceptions::IllegalArgumentException );
+
+		/**
+		 * This method transfers bytes into this buffer from the given source array.
+		 * If there are more bytes to be copied from the array than remain in this buffer,
+		 * that is, if length > remaining(), then no bytes are transferred and a
+		 * BufferOverflowException is thrown.
+		 * <p>
+		 * Otherwise, this method copies length bytes from the given array into this
+		 * buffer, starting at the given offset in the array and at the current position
+		 * of this buffer. The position of this buffer is then incremented by length.
+		 * @param buffer - The array from which bytes are to be read
+		 * @param offset - The offset within the array of the first byte to be read;
+		 * @param length - The number of bytes to be read from the given array
+		 * @returns a reference to this buffer
+		 * @throws BufferOverflowException - If there is insufficient space in this buffer
+		 * @throws ReadOnlyBufferException - If this buffer is read-only
+		 * @throws NullPointerException if the passed buffer is null.
+		 */
+		ByteBuffer& put( const unsigned char* buffer,
+						 std::size_t offset,
+						 std::size_t length )
+			throw( BufferOverflowException, ReadOnlyBufferException,
+				   lang::exceptions::NullPointerException );
+
+		/**
+		 * This method transfers the entire content of the given source byte array into
+		 * this buffer.  This is the same as calling put( &buffer[0], 0, buffer.size()
+		 * @pparam buffer - The buffer whose contents are copied to this ByteBuffer
+		 * @returns a reference to this buffer
+		 * @throws BufferOverflowException - If there is insufficient space in this buffer
+		 * @throws ReadOnlyBufferException - If this buffer is read-only
+		 */
+		ByteBuffer& put( std::vector<unsigned char>& buffer )
+			throw( BufferOverflowException, ReadOnlyBufferException );
+
+	public:   // Abstract Methods
+
+		/**
+		 * Tells whether or not this buffer is read-only.
+		 * @returns true if, and only if, this buffer is read-only
+		 */
+		virtual bool isReadOnly() const = 0;
+
+		/**
+		 * Returns the byte array that backs this buffer
+		 * <p>
+		 * Modifications to this buffer's content will cause the returned array's
+		 * content to be modified, and vice versa.
+		 * <p>
+		 * Invoke the hasArray method before invoking this method in order to ensure
+		 * that this buffer has an accessible backing array.
+		 * @returns The array that backs this buffer
+		 * @throws ReadOnlyBufferException - If this buffer is backed by an array but
+		 * is read-only
+		 * @throws UnsupportedOperationException - If this buffer is not backed by an
+		 * accessible array
+		 */
+		virtual unsigned char* array()
+			throw( ReadOnlyBufferException,
+				   lang::exceptions::UnsupportedOperationException ) = 0;
+
+		/**
+		 * Returns the offset within this buffer's backing array of the first element
+		 * of the buffer.
+		 * <p>
+		 * If this buffer is backed by an array then buffer position p corresponds to
+		 * array index p + arrayOffset().
+		 * <p>
+		 * Invoke the hasArray method before invoking this method in order to ensure
+		 * that this buffer has an accessible backing array.
+		 * @returns The offset within this buffer's array of the first element of
+		 * the buffer
+		 * @throws ReadOnlyBufferException - If this buffer is backed by an array but
+		 * is read-only
+		 * @throws UnsupportedOperationException - If this buffer is not backed by an
+		 * accessible array
+		 */
+		virtual std::size_t arrayOffset() const
+			throw( ReadOnlyBufferException,
+				   lang::exceptions::UnsupportedOperationException ) = 0;
+
+		/**
+		 * Tells whether or not this buffer is backed by an accessible byte array.
+		 * If this method returns true then the array and arrayOffset methods may safely
+		 * be invoked.  Subclasses should override this method if they do not have a
+		 * backing array as this class always returns true.
+		 * @returns true if, and only if, this buffer is backed by an array and is not
+		 * read-only
+		 */
+		virtual bool hasArray() const = 0;
+
+		/**
+		 * Creates a view of this byte buffer as a char buffer.
+		 * <p>
+		 * The content of the new buffer will start at this buffer's current position.
+		 * Changes to this buffer's content will be visible in the new buffer, and vice
+		 * versa; the two buffers' position, limit, and mark values will be independent.
+		 * <p>
+		 * The new buffer's position will be zero, its capacity and its limit will be
+		 * the number of bytes remaining in this buffer, and its mark will be undefined.
+		 * The new buffer will be read-only if, and only if, this buffer is read-only.
+		 * @returns the new Char Buffer, which the caller then owns.
+		 */
+		virtual CharBuffer* asCharBuffer() const = 0;
+
+		/**
+		 * Creates a view of this byte buffer as a double buffer.
+		 * <p>
+		 * The content of the new buffer will start at this buffer's current position.
+		 * Changes to this buffer's content will be visible in the new buffer, and vice
+		 * versa; the two buffers' position, limit, and mark values will be independent.
+		 * <p>
+		 * The new buffer's position will be zero, its capacity and its limit will be
+		 * the number of bytes remaining in this buffer divided by eight, and its mark
+		 * will be undefined. The new buffer will be read-only if, and only if, this
+		 * buffer is read-only.
+		 * @returns the new double Buffer, which the caller then owns.
+		 */
+		virtual DoubleBuffer* asDoubleBuffer() const = 0;
+
+		/**
+		 * Creates a view of this byte buffer as a float buffer.
+		 * <p>
+		 * The content of the new buffer will start at this buffer's current position.
+		 * Changes to this buffer's content will be visible in the new buffer, and vice
+		 * versa; the two buffers' position, limit, and mark values will be independent.
+		 * <p>
+		 * The new buffer's position will be zero, its capacity and its limit will be
+		 * the number of bytes remaining in this buffer divided by four, and its mark
+		 * will be undefined. The new buffer will be read-only if, and only if, this
+		 * buffer is read-only.
+		 * @returns the new float Buffer, which the caller then owns.
+		 */
+		virtual FloatBuffer* asFloatBuffer() const = 0;
+
+		/**
+		 * Creates a view of this byte buffer as a int buffer.
+		 * <p>
+		 * The content of the new buffer will start at this buffer's current position.
+		 * Changes to this buffer's content will be visible in the new buffer, and vice
+		 * versa; the two buffers' position, limit, and mark values will be independent.
+		 * <p>
+		 * The new buffer's position will be zero, its capacity and its limit will be
+		 * the number of bytes remaining in this buffer divided by four, and its mark
+		 * will be undefined. The new buffer will be read-only if, and only if, this
+		 * buffer is read-only.
+		 * @returns the new int Buffer, which the caller then owns.
+		 */
+		virtual IntBuffer* asIntBuffer() const = 0;
+
+		/**
+		 * Creates a view of this byte buffer as a long buffer.
+		 * <p>
+		 * The content of the new buffer will start at this buffer's current position.
+		 * Changes to this buffer's content will be visible in the new buffer, and vice
+		 * versa; the two buffers' position, limit, and mark values will be independent.
+		 * <p>
+		 * The new buffer's position will be zero, its capacity and its limit will be
+		 * the number of bytes remaining in this buffer divided by eight, and its mark
+		 * will be undefined. The new buffer will be read-only if, and only if, this
+		 * buffer is read-only.
+		 * @returns the new long Buffer, which the caller then owns.
+		 */
+		virtual LongBuffer* asLongBuffer() const = 0;
+
+		/**
+		 * Creates a view of this byte buffer as a short buffer.
+		 * <p>
+		 * The content of the new buffer will start at this buffer's current position.
+		 * Changes to this buffer's content will be visible in the new buffer, and vice
+		 * versa; the two buffers' position, limit, and mark values will be independent.
+		 * <p>
+		 * The new buffer's position will be zero, its capacity and its limit will be
+		 * the number of bytes remaining in this buffer divided by two, and its mark
+		 * will be undefined. The new buffer will be read-only if, and only if, this
+		 * buffer is read-only.
+		 * @returns the new short Buffer, which the caller then owns.
+		 */
+		virtual ShortBuffer* asShortBuffer() const = 0;
+
+		/**
+		 * Creates a new, read-only byte buffer that shares this buffer's content.
+		 * <p>
+		 * The content of the new buffer will be that of this buffer. Changes to this
+		 * buffer's content will be visible in the new buffer; the new buffer itself,
+		 * however, will be read-only and will not allow the shared content to be
+		 * modified. The two buffers' position, limit, and mark values will be
+		 * independent.
+		 * <p>
+		 * If this buffer is itself read-only then this method behaves in exactly the
+		 * same way as the duplicate method.
+		 * <p>
+		 * The new buffer's capacity, limit, position, and mark values will be
+		 * identical to those of this buffer.
+		 * @return The new, read-only byte buffer which the caller then owns.
+		 */
+		virtual ByteBuffer* asReadOnlyBuffer() const = 0;
+
+		/**
+		 * Compacts this buffer
+		 * <p>
+		 * The bytes between the buffer's current position and its limit, if any, are
+		 * copied to the beginning of the buffer. That is, the byte at index
+		 * p = position() is copied to index zero, the byte at index p + 1 is copied
+		 * to index one, and so forth until the byte at index limit() - 1 is copied
+		 * to index n = limit() - 1 - p. The buffer's position is then set to n+1 and
+		 * its limit is set to its capacity. The mark, if defined, is discarded.
+		 * <p>
+		 * The buffer's position is set to the number of bytes copied, rather than to
+		 * zero, so that an invocation of this method can be followed immediately by
+		 * an invocation of another relative put method.
+		 * @returns a reference to this ByteBuffer
+		 * @throws ReadOnlyBufferException - If this buffer is read-only
+		 */
+		virtual ByteBuffer& compact() throw( ReadOnlyBufferException ) = 0;
+
+		/**
+		 * Creates a new byte buffer that shares this buffer's content.
+		 * <p>
+		 * The content of the new buffer will be that of this buffer. Changes to this
+		 * buffer's content will be visible in the new buffer, and vice versa; the two
+		 * buffers' position, limit, and mark values will be independent.
+		 * <p>
+		 * The new buffer's capacity, limit, position, and mark values will be identical
+		 * to those of this buffer. The new buffer will be read-only if, and only if,
+		 * this buffer is read-only.
+		 * @returns a new Byte Buffer which the caller owns.
+		 */
+		virtual ByteBuffer* duplicate() = 0;
+
+		/**
+		 * Relative get method. Reads the byte at this buffer's current position, and
+		 * then increments the position.
+		 * @returns The byte at the buffer's current position
+		 * @throws BufferUnderflowException - If the buffer's current position is not
+		 * smaller than its limit
+		 */
+		virtual unsigned char get() const throw( BufferUnderflowException ) = 0;
+
+		/**
+		 * Absolute get method. Reads the byte at the given index.
+		 * @param index - the index in the Buffer where the byte is to be read
+		 * @returns the byte that is located at the given index
+		 * @throws IndexOutOfBoundsException - If index is not smaller than the
+		 * buffer's limit
+		 */
+		virtual unsigned char get( std::size_t index ) const
+			throw ( lang::exceptions::IndexOutOfBoundsException ) = 0;
+
+		/**
+		 * Reads the next byte at this buffer's current position, and then increments
+		 * the position by one
+		 * @returns the next char in the buffer..
+		 * @throws BufferUnderflowException - If there are no more bytes remaining in
+		 * this buffer, meaning we have reached the set limit.
+		 */
+		virtual char getChar() throw( BufferUnderflowException ) = 0;
+
+		/**
+		 * Reads one byte at the given index and returns it
+		 * @param index - the index in the Buffer where the byte is to be read
+		 * @returns the char at the given index in the buffer
+		 * @throws IndexOutOfBoundsException - If index is not smaller than the
+		 * buffer's limit
+		 */
+		virtual char getChar( std::size_t index ) const
+			throw ( lang::exceptions::IndexOutOfBoundsException ) = 0;
+
+		/**
+		 * Reads the next eight bytes at this buffer's current position, and then
+		 * increments the position by that amount.
+		 * @returns the next double in the buffer..
+		 * @throws BufferUnderflowException - If there are no more bytes remaining in
+		 * this buffer, meaning we have reached the set limit.
+		 */
+		virtual double getDouble() throw( BufferUnderflowException ) = 0;
+
+		/**
+		 * Reads eight bytes at the given index and returns it
+		 * @param index - the index in the Buffer where the bytes are to be read
+		 * @returns the double at the given index in the buffer
+		 * @throws IndexOutOfBoundsException - If there are not enough bytes
+		 * remaining to fill the requested Data Type
+		 */
+		virtual double getDouble( std::size_t index ) const
+			throw ( lang::exceptions::IndexOutOfBoundsException ) = 0;
+
+		/**
+		 * Reads the next four bytes at this buffer's current position, and then
+		 * increments the position by that amount.
+		 * @returns the next float in the buffer..
+		 * @throws BufferUnderflowException - If there are no more bytes remaining in
+		 * this buffer, meaning we have reached the set limit.
+		 */
+		virtual float getFloat() throw( BufferUnderflowException ) = 0;
+
+		/**
+		 * Reads four bytes at the given index and returns it
+		 * @param index - the index in the Buffer where the bytes are to be read
+		 * @returns the float at the given index in the buffer
+		 * @throws IndexOutOfBoundsException - If there are not enough bytes
+		 * remaining to fill the requested Data Type
+		 */
+		virtual float getFloat( std::size_t index ) const
+			throw ( lang::exceptions::IndexOutOfBoundsException ) = 0;
+
+		/**
+		 * Reads the next eight bytes at this buffer's current position, and then
+		 * increments the position by that amount.
+		 * @returns the next long long in the buffer..
+		 * @throws BufferUnderflowException - If there are no more bytes remaining in
+		 * this buffer, meaning we have reached the set limit.
+		 */
+		virtual long long getLong() throw( BufferUnderflowException ) = 0;
+
+		/**
+		 * Reads eight bytes at the given index and returns it
+		 * @param index - the index in the Buffer where the bytes are to be read
+		 * @returns the long long at the given index in the buffer
+		 * @throws IndexOutOfBoundsException - If there are not enough bytes
+		 * remaining to fill the requested Data Type
+		 */
+		virtual long long getLong( std::size_t index ) const
+			throw ( lang::exceptions::IndexOutOfBoundsException ) = 0;
+
+		/**
+		 * Reads the next four bytes at this buffer's current position, and then
+		 * increments the position by that amount.
+		 * @returns the next int in the buffer..
+		 * @throws BufferUnderflowException - If there are no more bytes remaining in
+		 * this buffer, meaning we have reached the set limit.
+		 */
+		virtual int getInt() throw( BufferUnderflowException ) = 0;
+
+		/**
+		 * Reads four bytes at the given index and returns it
+		 * @param index - the index in the Buffer where the bytes are to be read
+		 * @returns the int at the given index in the buffer
+		 * @throws IndexOutOfBoundsException - If there are not enough bytes
+		 * remaining to fill the requested Data Type
+		 */
+		virtual int getInt( std::size_t index ) const
+			throw ( lang::exceptions::IndexOutOfBoundsException ) = 0;
+
+		/**
+		 * Reads the next two bytes at this buffer's current position, and then
+		 * increments the position by that amount.
+		 * @returns the next short in the buffer..
+		 * @throws BufferUnderflowException - If there are no more bytes remaining in
+		 * this buffer, meaning we have reached the set limit.
+		 */
+		virtual short getShort() throw( BufferUnderflowException ) = 0;
+
+		/**
+		 * Reads two bytes at the given index and returns it
+		 * @param index - the index in the Buffer where the bytes are to be read
+		 * @returns the short at the given index in the buffer
+		 * @throws IndexOutOfBoundsException - If there are not enough bytes
+		 * remaining to fill the requested Data Type
+		 */
+		virtual short getShort( std::size_t index ) const
+			throw ( lang::exceptions::IndexOutOfBoundsException ) = 0;
+
+		/**
+		 * Writes the given byte into this buffer at the current position, and then
+		 * increments the position.
+		 * @param value - the byte value to be written
+		 * @returns a reference to this buffer
+		 * @throws BufferOverflowException - If this buffer's current position is not
+		 * smaller than its limit
+		 * @throws ReadOnlyBufferException - If this buffer is read-only
+		 */
+		virtual ByteBuffer& put( unsigned char value )
+			throw( BufferOverflowException, ReadOnlyBufferException ) = 0;
+
+		/**
+		 * Writes the given byte into this buffer at the given index.
+		 * @param index - position in the Buffer to write the data
+		 * @param value - the byte to write.
+		 * @returns a reference to this buffer
+		 * @throw IndexOutOfBoundsException - If index greater than the buffer's limit
+		 * minus the size of the type being written.
+		 * @throw ReadOnlyBufferException - If this buffer is read-only
+		 */
+		virtual ByteBuffer& put( std::size_t index, unsigned char value )
+			throw( lang::exceptions::IndexOutOfBoundsException,
+				   ReadOnlyBufferException ) = 0;
+
+		/**
+		 * Writes one byte containing the given value, into this buffer at the
+		 * current position, and then increments the position by one.
+		 * @param value - The value to be written
+		 * @returns a reference to this buffer
+		 * @throw BufferOverflowException - If there are fewer than bytes remaining
+		 * in this buffer than the size of the data to be written
+		 * @throw ReadOnlyBufferException - If this buffer is read-only
+		 */
+		virtual ByteBuffer& putChar( char value )
+			throw( BufferOverflowException, ReadOnlyBufferException ) = 0;
+
+		/**
+		 * Writes one byte containing the given value, into this buffer at the
+		 * given index.
+		 * @param index - position in the Buffer to write the data
+		 * @param value - the value to write.
+		 * @returns a reference to this buffer
+		 * @throw IndexOutOfBoundsException - If index greater than the buffer's limit
+		 * minus the size of the type being written.
+		 * @throw ReadOnlyBufferException - If this buffer is read-only
+		 */
+		virtual ByteBuffer& putChar( std::size_t index, char value )
+			throw( lang::exceptions::IndexOutOfBoundsException,
+				   ReadOnlyBufferException ) = 0;
+
+		/**
+		 * Writes eight bytes containing the given value, into this buffer at the
+		 * current position, and then increments the position by eight.
+		 * @param value - The value to be written
+		 * @returns a reference to this buffer
+		 * @throw BufferOverflowException - If there are fewer than bytes remaining
+		 * in this buffer than the size of the data to be written
+		 * @throw ReadOnlyBufferException - If this buffer is read-only
+		 */
+		virtual ByteBuffer& putDouble( double value )
+			throw( BufferOverflowException, ReadOnlyBufferException ) = 0;
+
+		/**
+		 * Writes eight bytes containing the given value, into this buffer at the
+		 * given index.
+		 * @param index - position in the Buffer to write the data
+		 * @param value - the value to write.
+		 * @returns a reference to this buffer
+		 * @throw IndexOutOfBoundsException - If index greater than the buffer's limit
+		 * minus the size of the type being written.
+		 * @throw ReadOnlyBufferException - If this buffer is read-only
+		 */
+		virtual ByteBuffer& putDouble( std::size_t index, double value )
+			throw( lang::exceptions::IndexOutOfBoundsException,
+				   ReadOnlyBufferException ) = 0;
+
+		/**
+		 * Writes four bytes containing the given value, into this buffer at the
+		 * current position, and then increments the position by eight.
+		 * @param value - The value to be written
+		 * @returns a reference to this buffer
+		 * @throw BufferOverflowException - If there are fewer than bytes remaining
+		 * in this buffer than the size of the data to be written
+		 * @throw ReadOnlyBufferException - If this buffer is read-only
+		 */
+		virtual ByteBuffer& putFloat( float value )
+			throw( BufferOverflowException, ReadOnlyBufferException ) = 0;
+
+		/**
+		 * Writes four bytes containing the given value, into this buffer at the
+		 * given index.
+		 * @param index - position in the Buffer to write the data
+		 * @param value - the value to write.
+		 * @returns a reference to this buffer
+		 * @throw IndexOutOfBoundsException - If index greater than the buffer's limit
+		 * minus the size of the type being written.
+		 * @throw ReadOnlyBufferException - If this buffer is read-only
+		 */
+		virtual ByteBuffer& putFloat( std::size_t index, float value )
+			throw( lang::exceptions::IndexOutOfBoundsException,
+				   ReadOnlyBufferException ) = 0;
+
+		/**
+		 * Writes eight bytes containing the given value, into this buffer at the
+		 * current position, and then increments the position by eight.
+		 * @param value - The value to be written
+		 * @returns a reference to this buffer
+		 * @throw BufferOverflowException - If there are fewer than bytes remaining
+		 * in this buffer than the size of the data to be written
+		 * @throw ReadOnlyBufferException - If this buffer is read-only
+		 */
+		virtual ByteBuffer& putLong( long long value )
+			throw( BufferOverflowException, ReadOnlyBufferException ) = 0;
+
+		/**
+		 * Writes eight bytes containing the given value, into this buffer at the
+		 * given index.
+		 * @param index - position in the Buffer to write the data
+		 * @param value - the value to write.
+		 * @returns a reference to this buffer
+		 * @throw IndexOutOfBoundsException - If index greater than the buffer's limit
+		 * minus the size of the type being written.
+		 * @throw ReadOnlyBufferException - If this buffer is read-only
+		 */
+		virtual ByteBuffer& putLong( std::size_t index, long long value )
+			throw( lang::exceptions::IndexOutOfBoundsException,
+				   ReadOnlyBufferException ) = 0;
+
+		/**
+		 * Writes four bytes containing the given value, into this buffer at the
+		 * current position, and then increments the position by eight.
+		 * @param value - The value to be written
+		 * @returns a reference to this buffer
+		 * @throw BufferOverflowException - If there are fewer than bytes remaining
+		 * in this buffer than the size of the data to be written
+		 * @throw ReadOnlyBufferException - If this buffer is read-only
+		 */
+		virtual ByteBuffer& putInt( int value )
+			throw( BufferOverflowException, ReadOnlyBufferException ) = 0;
+
+		/**
+		 * Writes four bytes containing the given value, into this buffer at the
+		 * given index.
+		 * @param index - position in the Buffer to write the data
+		 * @param value - the value to write.
+		 * @returns a reference to this buffer
+		 * @throw IndexOutOfBoundsException - If index greater than the buffer's limit
+		 * minus the size of the type being written.
+		 * @throw ReadOnlyBufferException - If this buffer is read-only
+		 */
+		virtual ByteBuffer& putInt( std::size_t index, int value )
+			throw( lang::exceptions::IndexOutOfBoundsException,
+				   ReadOnlyBufferException ) = 0;
+
+		/**
+		 * Writes two bytes containing the given value, into this buffer at the
+		 * current position, and then increments the position by eight.
+		 * @param value - The value to be written
+		 * @returns a reference to this buffer
+		 * @throw BufferOverflowException - If there are fewer than bytes remaining
+		 * in this buffer than the size of the data to be written
+		 * @throw ReadOnlyBufferException - If this buffer is read-only
+		 */
+		virtual ByteBuffer& putShort( short value )
+			throw( BufferOverflowException, ReadOnlyBufferException ) = 0;
+
+		/**
+		 * Writes two bytes containing the given value, into this buffer at the
+		 * given index.
+		 * @param index - position in the Buffer to write the data
+		 * @param value - the value to write.
+		 * @returns a reference to this buffer
+		 * @throw IndexOutOfBoundsException - If index greater than the buffer's limit
+		 * minus the size of the type being written.
+		 * @throw ReadOnlyBufferException - If this buffer is read-only
+		 */
+		virtual ByteBuffer& putShort( std::size_t index, short value )
+			throw( lang::exceptions::IndexOutOfBoundsException,
+				   ReadOnlyBufferException ) = 0;
+
+		/**
+		 * Creates a new byte buffer whose content is a shared subsequence of this
+		 * buffer's content.  The content of the new buffer will start at this buffer's
+		 * current position. Changes to this buffer's content will be visible in the new
+		 * buffer, and vice versa; the two buffers' position, limit, and mark values will
+		 * be independent.
+		 * <p>
+		 * The new buffer's position will be zero, its capacity and its limit will be the
+		 * number of bytes remaining in this buffer, and its mark will be undefined. The
+		 * new buffer will be read-only if, and only if, this buffer is read-only.
+		 * @returns the newly create ByteBuffer which the caller owns.
+		 */
+		virtual ByteBuffer* slice() const = 0;
+
+	public:  // Comparable
+
+		/**
+		 * Compares this object with the specified object for order. Returns a
+		 * negative integer, zero, or a positive integer as this object is less
+		 * than, equal to, or greater than the specified object.
+		 * @param value - the Object to be compared.
+		 * @returns a negative integer, zero, or a positive integer as this
+		 * object is less than, equal to, or greater than the specified object.
+		 */
+		virtual int compareTo( const ByteBuffer& value ) const;
+
+		/**
+		 * @return true if this value is considered equal to the passed value.
+		 */
+		virtual bool equals( const ByteBuffer& value ) const;
+
+		/**
+		 * Compares equality between this object and the one passed.
+		 * @param value - the value to be compared to this one.
+		 * @return true if this object is equal to the one passed.
+		 */
+		virtual bool operator==( const ByteBuffer& value ) const;
+
+		/**
+		 * Compares this object to another and returns true if this object
+		 * is considered to be less than the one passed.  This
+		 * @param value - the value to be compared to this one.
+		 * @return true if this object is equal to the one passed.
+		 */
+		virtual bool operator<( const ByteBuffer& value ) const;
+
+	public:
+
+		/**
+		 * Allocates a new byte buffer whose position will be zero its limit will
+		 * be its capacity and its mark is not set.
+		 * @param capacity - the internal buffer's capacity.
+		 * @returns a newly allocated ByteBuffer which the caller owns.
+		 */
+		static ByteBuffer* allocate( std::size_t capacity );
+
+		/**
+		 * Wraps the passed buffer with a new ByteBuffer.
+		 * <p>
+		 * The new buffer will be backed by the given byte array; that is, modifications
+		 * to the buffer will cause the array to be modified and vice versa. The new
+		 * buffer's capacity will be array.length, its position will be offset, its limit
+		 * will be offset + length, and its mark will be undefined. Its backing array
+		 * will be the given array, and its array offset will be zero.
+		 * @param buffer - The array that will back the new buffer
+		 * @param offset - The offset of the subarray to be used
+		 * @param length - The length of the subarray to be used
+		 * @returns a new ByteBuffer that is backed by buffer, caller owns.
+		 */
+		static ByteBuffer* wrap( unsigned char* array, std::size_t offset, std::size_t length )
+			throw( lang::exceptions::NullPointerException );
+
+		/**
+		 * Wraps the passed STL Byte Vector in a ByteBuffer.
+		 * <p>
+		 * The new buffer will be backed by the given byte array; modifications to the
+		 * buffer will cause the array to be modified and vice versa. The new buffer's
+		 * capacity and limit will be buffer.size(), its position will be zero, and its
+		 * mark will be undefined. Its backing array will be the given array, and its
+		 * array offset will be zero.
+		 * @param buffer - The vector that will back the new buffer, the vector must
+		 * have been sized to the desired size already by calling vector.resize( N ).
+		 * @returns a new ByteBuffer that is backed by buffer, caller owns.
+		 */
+		static ByteBuffer* wrap( std::vector<unsigned char>& buffer );
+
+	};
+
+}}
+
+#endif /*_DECAF_NIO_BYTEBUFFER_H_*/

Added: activemq/activemq-cpp/trunk/src/main/decaf/nio/CharBuffer.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/nio/CharBuffer.cpp?rev=652104&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/nio/CharBuffer.cpp (added)
+++ activemq/activemq-cpp/trunk/src/main/decaf/nio/CharBuffer.cpp Tue Apr 29 13:52:30 2008
@@ -0,0 +1,464 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "CharBuffer.h"
+#include <decaf/lang/Character.h>
+#include <decaf/lang/Math.h>
+#include "decaf/internal/nio/BufferFactory.h"
+
+using namespace std;
+using namespace decaf;
+using namespace decaf::nio;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
+using namespace decaf::internal::nio;
+
+////////////////////////////////////////////////////////////////////////////////
+CharBuffer::CharBuffer( std::size_t capacity )
+ :  Buffer( capacity ) {
+
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CharBuffer* CharBuffer::allocate( std::size_t capacity ) {
+
+    try{
+
+        return BufferFactory::createCharBuffer( capacity );
+    }
+    DECAF_CATCH_RETHROW( Exception )
+    DECAF_CATCHALL_THROW( Exception )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CharBuffer* CharBuffer::wrap( char* buffer, std::size_t offset, std::size_t length )
+    throw( lang::exceptions::NullPointerException ) {
+
+    try{
+
+        if( buffer == NULL ) {
+            throw NullPointerException(
+                __FILE__, __LINE__,
+                "CharBuffer::wrap - Passed Buffer is Null.");
+        }
+
+        return BufferFactory::createCharBuffer( buffer, offset, length );
+    }
+    DECAF_CATCH_RETHROW( NullPointerException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, NullPointerException )
+    DECAF_CATCHALL_THROW( NullPointerException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CharBuffer* CharBuffer::wrap( std::vector<char>& buffer ) {
+
+    try{
+
+        if( buffer.empty() ) {
+            throw NullPointerException(
+                __FILE__, __LINE__,
+                "CharBuffer::wrap - Passed Buffer is Empty.");
+        }
+
+        return BufferFactory::createCharBuffer( &buffer[0], 0, buffer.size() );
+    }
+    DECAF_CATCH_RETHROW( NullPointerException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, NullPointerException )
+    DECAF_CATCHALL_THROW( NullPointerException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::string CharBuffer::toString() const {
+
+    std::string strbuf;
+
+    for( std::size_t i = this->position(); i < this->limit(); i++ ) {
+        strbuf.append( Character::valueOf( get( i ) ).toString() );
+    }
+
+    return strbuf;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CharBuffer& CharBuffer::append( char value )
+    throw ( BufferOverflowException, ReadOnlyBufferException ) {
+
+    try{
+
+        this->put( value );
+
+        return *this;
+    }
+    DECAF_CATCH_RETHROW( BufferOverflowException )
+    DECAF_CATCH_RETHROW( ReadOnlyBufferException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferOverflowException )
+    DECAF_CATCHALL_THROW( BufferOverflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CharBuffer& CharBuffer::append( const CharSequence* value )
+    throw ( BufferOverflowException, ReadOnlyBufferException ) {
+
+    try{
+
+        if( value != NULL ) {
+            return this->put( value->toString() );
+        }
+
+        return this->put( "null" );
+    }
+    DECAF_CATCH_RETHROW( BufferOverflowException )
+    DECAF_CATCH_RETHROW( ReadOnlyBufferException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferOverflowException )
+    DECAF_CATCHALL_THROW( BufferOverflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CharBuffer& CharBuffer::append( const CharSequence* value, std::size_t start, std::size_t end )
+    throw ( decaf::lang::exceptions::IndexOutOfBoundsException,
+            BufferOverflowException, ReadOnlyBufferException ) {
+
+    try{
+
+        if( value != NULL ) {
+            CharSequence* temp = value->subSequence( start, end );
+            this->append( temp );
+            delete temp;
+
+            return *this;
+        }
+
+        return this->put( "null", start, end );
+    }
+    DECAF_CATCH_RETHROW( IndexOutOfBoundsException )
+    DECAF_CATCH_RETHROW( BufferOverflowException )
+    DECAF_CATCH_RETHROW( ReadOnlyBufferException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferOverflowException )
+    DECAF_CATCHALL_THROW( BufferOverflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+char CharBuffer::charAt( std::size_t index ) const
+    throw( decaf::lang::exceptions::IndexOutOfBoundsException ) {
+
+    try{
+
+        return this->get( index );
+    }
+    DECAF_CATCH_RETHROW( IndexOutOfBoundsException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, IndexOutOfBoundsException )
+    DECAF_CATCHALL_THROW( IndexOutOfBoundsException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CharBuffer& CharBuffer::get( std::vector<char> buffer )
+    throw ( BufferUnderflowException ) {
+
+    try{
+
+        if( !buffer.empty() ) {
+            this->get( &buffer[0], 0, buffer.size() );
+        }
+        return *this;
+    }
+    DECAF_CATCH_RETHROW( BufferUnderflowException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferUnderflowException )
+    DECAF_CATCHALL_THROW( BufferUnderflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CharBuffer& CharBuffer::get( char* buffer, std::size_t offset, std::size_t length )
+    throw( BufferUnderflowException,
+           lang::exceptions::NullPointerException ) {
+
+    try{
+
+        if( length == 0 ) {
+            return *this;
+        }
+
+        if( buffer == NULL ) {
+            throw NullPointerException(
+                __FILE__, __LINE__,
+                "CharBuffer::get - Passed Buffer is Null" );
+        }
+
+        if( length > remaining() ) {
+            throw BufferUnderflowException(
+                __FILE__, __LINE__,
+                "CharBuffer::get - Not enough data to fill length = %d", length );
+        }
+
+        for( std::size_t ix = 0; ix < length; ++ix ){
+            buffer[offset + ix] = this->get();
+        }
+
+        return *this;
+    }
+    DECAF_CATCH_RETHROW( NullPointerException )
+    DECAF_CATCH_RETHROW( BufferUnderflowException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferUnderflowException )
+    DECAF_CATCHALL_THROW( BufferUnderflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CharBuffer& CharBuffer::put( CharBuffer& src )
+    throw( BufferOverflowException, ReadOnlyBufferException,
+           lang::exceptions::IllegalArgumentException ) {
+
+    try{
+
+        if( this == &src ) {
+            throw IllegalArgumentException(
+                __FILE__, __LINE__,
+                "CharBuffer::put - Can't put Self" );
+        }
+
+        if( this->isReadOnly() ) {
+            throw ReadOnlyBufferException(
+                __FILE__, __LINE__,
+                "CharBuffer::put - This buffer is Read Only.");
+        }
+
+        if( src.remaining() > this->remaining() ) {
+            throw BufferOverflowException(
+                __FILE__, __LINE__,
+                "CharBuffer::put - Not enough space remaining to put src." );
+        }
+
+        while( src.hasRemaining() ) {
+            this->put( src.get() );
+        }
+
+        return *this;
+    }
+    DECAF_CATCH_RETHROW( BufferOverflowException )
+    DECAF_CATCH_RETHROW( ReadOnlyBufferException )
+    DECAF_CATCH_RETHROW( IllegalArgumentException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferOverflowException )
+    DECAF_CATCHALL_THROW( BufferOverflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CharBuffer& CharBuffer::put( const char* buffer, std::size_t offset, std::size_t length )
+    throw( BufferOverflowException, ReadOnlyBufferException,
+           lang::exceptions::NullPointerException ) {
+
+    try{
+
+        if( length == 0 ) {
+            return *this;
+        }
+
+        if( this->isReadOnly() ) {
+            throw ReadOnlyBufferException(
+                __FILE__, __LINE__,
+                "CharBuffer::put - This buffer is Read Only.");
+        }
+
+        if( buffer == NULL ) {
+            throw NullPointerException(
+                __FILE__, __LINE__,
+                "CharBuffer::put - Passed Buffer is Null.");
+        }
+
+        if( length > this->remaining() ) {
+            throw BufferOverflowException(
+                __FILE__, __LINE__,
+                "CharBuffer::put - Not Enough space to store requested Data.");
+        }
+
+        // read length bytes starting from the offset
+        for( std::size_t ix = 0; ix < length; ++ix ) {
+            this->put( buffer[ix + offset] );
+        }
+
+        return *this;
+    }
+    DECAF_CATCH_RETHROW( BufferOverflowException )
+    DECAF_CATCH_RETHROW( ReadOnlyBufferException )
+    DECAF_CATCH_RETHROW( NullPointerException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferOverflowException )
+    DECAF_CATCHALL_THROW( BufferOverflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CharBuffer& CharBuffer::put( std::vector<char>& buffer )
+    throw( BufferOverflowException, ReadOnlyBufferException ) {
+
+    try{
+
+        if( !buffer.empty() ) {
+            this->put( &buffer[0], 0, buffer.size() );
+        }
+
+        return *this;
+    }
+    DECAF_CATCH_RETHROW( BufferOverflowException )
+    DECAF_CATCH_RETHROW( ReadOnlyBufferException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferOverflowException )
+    DECAF_CATCHALL_THROW( BufferOverflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CharBuffer& CharBuffer::put( const std::string& src, std::size_t start, std::size_t end )
+    throw( BufferOverflowException, ReadOnlyBufferException,
+           decaf::lang::exceptions::IndexOutOfBoundsException ) {
+
+    try{
+
+        if( ( start > end ) || ( src.size() < ( end - start ) ) ) {
+            throw IndexOutOfBoundsException(
+                __FILE__, __LINE__,
+                "CharBuffer::put - invalid start and end pos; start = %d, end = %d",
+                start, end );
+        }
+
+        if( start > src.size() || end > src.size() ) {
+            throw IndexOutOfBoundsException(
+                __FILE__, __LINE__,
+                "CharBuffer::put - invalid start and end pos; start = %d, end = %d",
+                start, end );
+        }
+
+        this->put( src.substr( start, end-start ) );
+
+        return *this;
+    }
+    DECAF_CATCH_RETHROW( IndexOutOfBoundsException )
+    DECAF_CATCH_RETHROW( BufferOverflowException )
+    DECAF_CATCH_RETHROW( ReadOnlyBufferException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferOverflowException )
+    DECAF_CATCHALL_THROW( BufferOverflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CharBuffer& CharBuffer::put( const std::string& src )
+    throw( BufferOverflowException, ReadOnlyBufferException ) {
+
+    try{
+
+        if( !src.empty() ) {
+            this->put( src.c_str(), 0, src.size() );
+        }
+
+        return *this;
+    }
+    DECAF_CATCH_RETHROW( BufferOverflowException )
+    DECAF_CATCH_RETHROW( ReadOnlyBufferException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, BufferOverflowException )
+    DECAF_CATCHALL_THROW( BufferOverflowException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::size_t CharBuffer::read( CharBuffer* target )
+    throw ( decaf::lang::exceptions::NullPointerException,
+            decaf::lang::exceptions::IllegalArgumentException,
+            ReadOnlyBufferException ) {
+
+    try{
+
+        if( target == this ) {
+            throw IllegalArgumentException(
+                __FILE__, __LINE__,
+                "CharBuffer::read - Cannot read to self" );
+        }
+
+        if( target == NULL ) {
+            throw NullPointerException(
+                __FILE__, __LINE__,
+                "CharBuffer::read - Null CharBuffer Passed" );
+        }
+
+        if( this->remaining() == 0 ) {
+            return target->remaining() == 0 ? 0 : string::npos;
+        }
+
+        std::size_t result = (std::size_t)Math::min(
+                (int)target->remaining(),
+                (int)this->remaining() );
+        char* chars = new char[result];
+        get( chars, 0, result );
+        target->put( chars, 0, result );
+        delete [] chars;
+
+        return result;
+    }
+    DECAF_CATCH_RETHROW( NullPointerException )
+    DECAF_CATCH_RETHROW( IllegalArgumentException )
+    DECAF_CATCH_RETHROW( ReadOnlyBufferException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, IllegalArgumentException )
+    DECAF_CATCHALL_THROW( IllegalArgumentException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int CharBuffer::compareTo( const CharBuffer& value ) const {
+
+    int compareRemaining = Math::min( (int)remaining(), (int)value.remaining() );
+
+    std::size_t thisPos = this->position();
+    std::size_t otherPos = value.position();
+    char thisByte, otherByte;
+
+    while( compareRemaining > 0 ) {
+
+        thisByte = get( thisPos );
+        otherByte = value.get( otherPos );
+
+        if( thisByte != otherByte ) {
+            return thisByte < otherByte ? -1 : 1;
+        }
+
+        thisPos++;
+        otherPos++;
+        compareRemaining--;
+    }
+
+    return remaining() - value.remaining();
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool CharBuffer::equals( const CharBuffer& value ) const {
+
+    if( &value == this ) {
+        return true;
+    }
+
+    if( this->remaining() != value.remaining() ) {
+        return false;
+    }
+
+    std::size_t myPosition = this->position();
+    std::size_t otherPosition = value.position();
+    bool equalSoFar = true;
+
+    while( equalSoFar && ( myPosition < this->limit() ) ) {
+        equalSoFar = get( myPosition++ ) == value.get( otherPosition++ );
+    }
+
+    return equalSoFar;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool CharBuffer::operator==( const CharBuffer& value ) const {
+    return this->equals( value );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool CharBuffer::operator<( const CharBuffer& value ) const {
+    return this->compareTo( value ) < 0 ? true : false;
+}



Mime
View raw message