Return-Path: Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: (qmail 47300 invoked from network); 31 May 2007 11:56:34 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 31 May 2007 11:56:34 -0000 Received: (qmail 45902 invoked by uid 500); 31 May 2007 11:56:38 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 45851 invoked by uid 500); 31 May 2007 11:56:38 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 45842 invoked by uid 99); 31 May 2007 11:56:38 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 31 May 2007 04:56:38 -0700 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 31 May 2007 04:56:26 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id BA6C31A981A; Thu, 31 May 2007 04:56:05 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r543121 [1/6] - in /activemq/activemq-cpp/trunk/src/decaf: io/ lang/ lang/exceptions/ net/ util/ util/concurrent/ util/logging/ Date: Thu, 31 May 2007 11:56:03 -0000 To: commits@activemq.apache.org From: tabish@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070531115605.BA6C31A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tabish Date: Thu May 31 04:55:59 2007 New Revision: 543121 URL: http://svn.apache.org/viewvc?view=rev&rev=543121 Log: http://issues.apache.org/activemq/browse/AMQCPP-103 Building Decaf lib, remove sub folder from the activemq lib dir, going to top level. Added: activemq/activemq-cpp/trunk/src/decaf/io/BlockingByteArrayInputStream.cpp activemq/activemq-cpp/trunk/src/decaf/io/BlockingByteArrayInputStream.h activemq/activemq-cpp/trunk/src/decaf/io/BufferedInputStream.cpp activemq/activemq-cpp/trunk/src/decaf/io/BufferedInputStream.h activemq/activemq-cpp/trunk/src/decaf/io/BufferedOutputStream.cpp activemq/activemq-cpp/trunk/src/decaf/io/BufferedOutputStream.h activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayInputStream.cpp activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayInputStream.h activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayOutputStream.cpp activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayOutputStream.h activemq/activemq-cpp/trunk/src/decaf/io/Closeable.h activemq/activemq-cpp/trunk/src/decaf/io/DataInputStream.cpp activemq/activemq-cpp/trunk/src/decaf/io/DataInputStream.h activemq/activemq-cpp/trunk/src/decaf/io/DataOutputStream.cpp activemq/activemq-cpp/trunk/src/decaf/io/DataOutputStream.h activemq/activemq-cpp/trunk/src/decaf/io/EOFException.h activemq/activemq-cpp/trunk/src/decaf/io/FilterInputStream.h activemq/activemq-cpp/trunk/src/decaf/io/FilterOutputStream.h activemq/activemq-cpp/trunk/src/decaf/io/IOException.h activemq/activemq-cpp/trunk/src/decaf/io/InputStream.h activemq/activemq-cpp/trunk/src/decaf/io/OutputStream.h activemq/activemq-cpp/trunk/src/decaf/io/Reader.h activemq/activemq-cpp/trunk/src/decaf/io/StandardErrorOutputStream.h activemq/activemq-cpp/trunk/src/decaf/io/Writer.h activemq/activemq-cpp/trunk/src/decaf/lang/Boolean.h activemq/activemq-cpp/trunk/src/decaf/lang/Character.h activemq/activemq-cpp/trunk/src/decaf/lang/Exception.cpp activemq/activemq-cpp/trunk/src/decaf/lang/Exception.h activemq/activemq-cpp/trunk/src/decaf/lang/Integer.h activemq/activemq-cpp/trunk/src/decaf/lang/Long.h activemq/activemq-cpp/trunk/src/decaf/lang/Math.h activemq/activemq-cpp/trunk/src/decaf/lang/Number.h activemq/activemq-cpp/trunk/src/decaf/lang/Runnable.h activemq/activemq-cpp/trunk/src/decaf/lang/Thread.cpp activemq/activemq-cpp/trunk/src/decaf/lang/Thread.h activemq/activemq-cpp/trunk/src/decaf/lang/Throwable.h activemq/activemq-cpp/trunk/src/decaf/lang/exceptions/ExceptionDefines.h activemq/activemq-cpp/trunk/src/decaf/lang/exceptions/IllegalArgumentException.h activemq/activemq-cpp/trunk/src/decaf/lang/exceptions/IllegalMonitorStateException.h activemq/activemq-cpp/trunk/src/decaf/lang/exceptions/IllegalStateException.h activemq/activemq-cpp/trunk/src/decaf/lang/exceptions/IndexOutOfBoundsException.h activemq/activemq-cpp/trunk/src/decaf/lang/exceptions/InterruptedException.h activemq/activemq-cpp/trunk/src/decaf/lang/exceptions/InvalidStateException.h activemq/activemq-cpp/trunk/src/decaf/lang/exceptions/NoSuchElementException.h activemq/activemq-cpp/trunk/src/decaf/lang/exceptions/NullPointerException.h activemq/activemq-cpp/trunk/src/decaf/lang/exceptions/RuntimeException.h activemq/activemq-cpp/trunk/src/decaf/lang/exceptions/UnsupportedOperationException.h activemq/activemq-cpp/trunk/src/decaf/net/BufferedSocket.cpp activemq/activemq-cpp/trunk/src/decaf/net/BufferedSocket.h activemq/activemq-cpp/trunk/src/decaf/net/ServerSocket.cpp activemq/activemq-cpp/trunk/src/decaf/net/ServerSocket.h activemq/activemq-cpp/trunk/src/decaf/net/Socket.h activemq/activemq-cpp/trunk/src/decaf/net/SocketError.cpp activemq/activemq-cpp/trunk/src/decaf/net/SocketError.h activemq/activemq-cpp/trunk/src/decaf/net/SocketException.h activemq/activemq-cpp/trunk/src/decaf/net/SocketInputStream.cpp activemq/activemq-cpp/trunk/src/decaf/net/SocketInputStream.h activemq/activemq-cpp/trunk/src/decaf/net/SocketOutputStream.cpp activemq/activemq-cpp/trunk/src/decaf/net/SocketOutputStream.h activemq/activemq-cpp/trunk/src/decaf/net/TcpSocket.cpp activemq/activemq-cpp/trunk/src/decaf/net/TcpSocket.h activemq/activemq-cpp/trunk/src/decaf/util/Config.h activemq/activemq-cpp/trunk/src/decaf/util/Date.cpp activemq/activemq-cpp/trunk/src/decaf/util/Date.h activemq/activemq-cpp/trunk/src/decaf/util/concurrent/Concurrent.h activemq/activemq-cpp/trunk/src/decaf/util/concurrent/Lock.h activemq/activemq-cpp/trunk/src/decaf/util/concurrent/Mutex.cpp activemq/activemq-cpp/trunk/src/decaf/util/concurrent/Mutex.h activemq/activemq-cpp/trunk/src/decaf/util/concurrent/Synchronizable.h activemq/activemq-cpp/trunk/src/decaf/util/logging/ConsoleHandler.h activemq/activemq-cpp/trunk/src/decaf/util/logging/Filter.h activemq/activemq-cpp/trunk/src/decaf/util/logging/Formatter.h activemq/activemq-cpp/trunk/src/decaf/util/logging/Handler.h activemq/activemq-cpp/trunk/src/decaf/util/logging/LogManager.cpp activemq/activemq-cpp/trunk/src/decaf/util/logging/LogManager.h activemq/activemq-cpp/trunk/src/decaf/util/logging/LogRecord.h activemq/activemq-cpp/trunk/src/decaf/util/logging/LogWriter.cpp activemq/activemq-cpp/trunk/src/decaf/util/logging/LogWriter.h activemq/activemq-cpp/trunk/src/decaf/util/logging/Logger.cpp activemq/activemq-cpp/trunk/src/decaf/util/logging/Logger.h activemq/activemq-cpp/trunk/src/decaf/util/logging/LoggerCommon.h activemq/activemq-cpp/trunk/src/decaf/util/logging/LoggerDefines.h activemq/activemq-cpp/trunk/src/decaf/util/logging/LoggerHierarchy.cpp activemq/activemq-cpp/trunk/src/decaf/util/logging/LoggerHierarchy.h activemq/activemq-cpp/trunk/src/decaf/util/logging/MarkBlockLogger.h activemq/activemq-cpp/trunk/src/decaf/util/logging/PropertiesChangeListener.h activemq/activemq-cpp/trunk/src/decaf/util/logging/SimpleFormatter.h activemq/activemq-cpp/trunk/src/decaf/util/logging/SimpleLogger.cpp activemq/activemq-cpp/trunk/src/decaf/util/logging/SimpleLogger.h activemq/activemq-cpp/trunk/src/decaf/util/logging/StreamHandler.h Added: activemq/activemq-cpp/trunk/src/decaf/io/BlockingByteArrayInputStream.cpp URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/io/BlockingByteArrayInputStream.cpp?view=auto&rev=543121 ============================================================================== --- activemq/activemq-cpp/trunk/src/decaf/io/BlockingByteArrayInputStream.cpp (added) +++ activemq/activemq-cpp/trunk/src/decaf/io/BlockingByteArrayInputStream.cpp Thu May 31 04:55:59 2007 @@ -0,0 +1,152 @@ +/* + * 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 "BlockingByteArrayInputStream.h" +#include + +using namespace std; +using namespace decaf; +using namespace decaf::io; +using namespace decaf::lang; + +//////////////////////////////////////////////////////////////////////////////// +BlockingByteArrayInputStream::BlockingByteArrayInputStream(){ + pos = buffer.end(); + closing = false; +} + +//////////////////////////////////////////////////////////////////////////////// +BlockingByteArrayInputStream::BlockingByteArrayInputStream( + const unsigned char* buffer, + std::size_t bufferSize ){ + + closing = false; + setByteArray( buffer, bufferSize ); +} + +//////////////////////////////////////////////////////////////////////////////// +BlockingByteArrayInputStream::~BlockingByteArrayInputStream(){ +} + +//////////////////////////////////////////////////////////////////////////////// +void BlockingByteArrayInputStream::setByteArray( const unsigned char* lbuffer, + std::size_t lbufferSize ){ + synchronized( this ){ + + // Remove old data + this->buffer.clear(); + + // Copy data to internal buffer. + for( std::size_t ix = 0; ix < lbufferSize; ++ix ) + { + this->buffer.push_back(lbuffer[ix]); + } + + // Begin at the Beginning. + pos = this->buffer.begin(); + + // Notify any listening threds that there is now data available. + notifyAll(); + } +} + +//////////////////////////////////////////////////////////////////////////////// +void BlockingByteArrayInputStream::close() throw ( lang::Exception ){ + + synchronized( this ){ + + // Indicate that we're shutting down. + closing = true; + + // Clear out the buffer. + buffer.clear(); + + // Notify that this stream is shutting down. + notifyAll(); + } +} + +//////////////////////////////////////////////////////////////////////////////// +unsigned char BlockingByteArrayInputStream::read() throw ( IOException ){ + + synchronized( this ){ + + while( !closing ){ + + if( pos != buffer.end() ){ + return *(pos++); + } + + // Wait for more data + wait(); + } + + throw IOException( __FILE__, __LINE__, "close occurred during read" ); + } + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// +std::size_t BlockingByteArrayInputStream::read( unsigned char* buffer, + std::size_t bufferSize ) + throw ( IOException ){ + synchronized( this ){ + + std::size_t ix = 0; + + for( ; ix < bufferSize && !closing; ++ix, ++pos) + { + if(pos == this->buffer.end()) + { + // Wait for more data to come in. + wait(); + } + + if( !closing ){ + buffer[ix] = *(pos); + } + } + + if( closing ){ + throw IOException( + __FILE__, __LINE__, "close occurred during read" ); + } + + return ix; + } + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// +std::size_t BlockingByteArrayInputStream::skip( std::size_t num ) + throw ( io::IOException, lang::exceptions::UnsupportedOperationException ){ + + std::size_t ix = 0; + + synchronized( this ){ + + // Increment the pos until we'v skipped the desired num + // or we've hit the end of the buffer. + for( ; ix < num && !closing && pos != buffer.end(); ++ix, ++pos) {} + } + + return ix; +} + + Added: activemq/activemq-cpp/trunk/src/decaf/io/BlockingByteArrayInputStream.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/io/BlockingByteArrayInputStream.h?view=auto&rev=543121 ============================================================================== --- activemq/activemq-cpp/trunk/src/decaf/io/BlockingByteArrayInputStream.h (added) +++ activemq/activemq-cpp/trunk/src/decaf/io/BlockingByteArrayInputStream.h Thu May 31 04:55:59 2007 @@ -0,0 +1,209 @@ +/* + * 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_IO_BLOCKINGBYTEARRAYINPUTSTREAM_H_ +#define _DECAF_IO_BLOCKINGBYTEARRAYINPUTSTREAM_H_ + +#include +#include +#include + +namespace decaf{ +namespace io{ + + /** + * This is a blocking version of a byte buffer stream. Read operations + * block until the requested data becomes available in the internal + * buffer via a call to setByteArray. + */ + class BlockingByteArrayInputStream : public InputStream + { + private: + + /** + * Default buffer to use, if none provided. + */ + std::vector buffer; + + /** + * iterator to current position in buffer. + */ + std::vector::const_iterator pos; + + /** + * Synchronization object. + */ + util::concurrent::Mutex mutex; + + /** + * Indicates that this stream is in the process of shutting + * down. + */ + bool closing; + + public: + + /** + * Default Constructor - uses a default internal buffer + */ + BlockingByteArrayInputStream(); + + /** + * Constructor that initializes the internal buffer. + * @see setByteArray. + */ + BlockingByteArrayInputStream( const unsigned char* buffer, + std::size_t bufferSize ); + + /** + * Destructor + */ + virtual ~BlockingByteArrayInputStream(); + + /** + * Sets the data that this reader uses. Replaces any existing + * data and resets the read index to the beginning of the buffer. + * When this method is called, it notifies any other threads that + * data is now available to be read. + * @param buffer The new data to be copied to the internal buffer. + * @param bufferSize The size of the new buffer. + */ + virtual void setByteArray( const unsigned char* buffer, + std::size_t bufferSize ); + + /** + * Waits on a signal from this object, which is generated + * by a call to Notify. Must have this object locked before + * calling. + * @throws Exception + */ + virtual void lock() throw( lang::Exception ){ + mutex.lock(); + } + + /** + * Unlocks the object. + * @throws Exception + */ + virtual void unlock() throw( lang::Exception ){ + mutex.unlock(); + } + + /** + * Waits on a signal from this object, which is generated + * by a call to Notify. Must have this object locked before + * calling. + * @throws Exception + */ + virtual void wait() throw( lang::Exception ){ + mutex.wait(); + } + + /** + * Waits on a signal from this object, which is generated + * by a call to Notify. Must have this object locked before + * calling. This wait will timeout after the specified time + * interval. + * @param time in millisecsonds to wait, or WAIT_INIFINITE + * @throws Exception + */ + virtual void wait( unsigned long millisecs ) throw( lang::Exception ){ + mutex.wait(millisecs); + } + + /** + * Signals a waiter on this object that it can now wake + * up and continue. Must have this object locked before + * calling. + * @throws Exception + */ + virtual void notify() throw( lang::Exception ){ + mutex.notify(); + } + + /** + * Signals the waiters on this object that it can now wake + * up and continue. Must have this object locked before + * calling. + * @throws Exception + */ + virtual void notifyAll() throw( lang::Exception ){ + mutex.notifyAll(); + } + + /** + * Indicates the number of bytes available to be read without + * blocking. + * @return the data available in the internal buffer. + * @throws IOException if an error occurs. + */ + virtual std::size_t available() const throw ( IOException ){ + return std::distance( pos, buffer.end() ); + } + + /** + * Reads a single byte from the buffer. This operation will + * block until data has been added to the buffer via a call + * to setByteArray. + * @return the next byte. + * @throws IOException if an error occurs. + */ + virtual unsigned char read() throw ( IOException ); + + /** + * Reads an array of bytes from the buffer. If the desired amount + * of data is not currently available, this operation + * will block until the appropriate amount of data is available + * in the buffer via a call to setByteArray. + * @param buffer (out) the target buffer + * @param bufferSize the size of the output buffer. + * @return the number of bytes read. + * @throws IOException f an error occurs. + */ + virtual std::size_t read( unsigned char* buffer, std::size_t bufferSize ) + throw ( IOException ); + + /** + * Closes the target input stream. + * @throws IOException if an error occurs. + */ + virtual void close() throw ( lang::Exception ); + + /** + * Skips over and discards n bytes of data from this input stream. The + * skip method may, for a variety of reasons, end up skipping over some + * smaller number of bytes, possibly 0. This may result from any of a + * number of conditions; reaching end of file before n bytes have been + * skipped is only one possibility. The actual number of bytes skipped + * is returned. If n is negative, no bytes are skipped. + *

+ * The skip method of InputStream creates a byte array and then + * repeatedly reads into it until n bytes have been read or the end + * of the stream has been reached. Subclasses are encouraged to + * provide a more efficient implementation of this method. + * @param num - the number of bytes to skip + * @returns total butes skipped + * @throws IOException if an error occurs + */ + virtual std::size_t skip( std::size_t num ) + throw ( io::IOException, lang::exceptions::UnsupportedOperationException ); + + }; + +}} + +#endif /*_DECAF_IO_BLOCKINGBYTEARRAYINPUTSTREAM_H_*/ Added: activemq/activemq-cpp/trunk/src/decaf/io/BufferedInputStream.cpp URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/io/BufferedInputStream.cpp?view=auto&rev=543121 ============================================================================== --- activemq/activemq-cpp/trunk/src/decaf/io/BufferedInputStream.cpp (added) +++ activemq/activemq-cpp/trunk/src/decaf/io/BufferedInputStream.cpp Thu May 31 04:55:59 2007 @@ -0,0 +1,202 @@ +/* + * 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 "BufferedInputStream.h" +#include + +using namespace std; +using namespace decaf; +using namespace decaf::io; +using namespace decaf::lang; + +//////////////////////////////////////////////////////////////////////////////// +BufferedInputStream::BufferedInputStream( InputStream* stream, bool own ) +: FilterInputStream( stream, own ) +{ + // Default to a 1k buffer. + init( 1024 ); +} + +//////////////////////////////////////////////////////////////////////////////// +BufferedInputStream::BufferedInputStream( InputStream* stream, + std::size_t bufferSize, + bool own ) +: FilterInputStream( stream, own ) +{ + init( bufferSize ); +} + +//////////////////////////////////////////////////////////////////////////////// +BufferedInputStream::~BufferedInputStream() +{ + // Destroy the buffer. + if( buffer != NULL ){ + delete [] buffer; + buffer = NULL; + } +} + +//////////////////////////////////////////////////////////////////////////////// +void BufferedInputStream::init( std::size_t bufferSize ){ + + this->bufferSize = bufferSize; + + // Create the buffer and initialize the head and tail positions. + buffer = new unsigned char[bufferSize]; + head = 0; + tail = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +unsigned char BufferedInputStream::read() throw ( IOException ){ + + try{ + // If there's no data left, reset to pointers to the beginning of the + // buffer. + normalizeBuffer(); + + // If we don't have any data buffered yet - read as much as + // we can. + if( isEmpty() ){ + bufferData(); + } + + // Get the next character. + char returnValue = buffer[head++]; + + return returnValue; + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +std::size_t BufferedInputStream::read( unsigned char* targetBuffer, + std::size_t targetBufferSize ) throw ( IOException ){ + + try{ + // If there's no data left, reset to pointers to the beginning of the + // buffer. + normalizeBuffer(); + + // If we still haven't filled the output buffer AND there is data + // on the input stream to be read, read a buffer's + // worth from the stream. + std::size_t totalRead = 0; + while( totalRead < targetBufferSize ){ + + // Get the remaining bytes to copy. + std::size_t bytesToCopy = min( tail-head, (targetBufferSize-totalRead) ); + + // Copy the data to the output buffer. + memcpy( targetBuffer+totalRead, this->buffer+head, bytesToCopy ); + + // Increment the total bytes read. + totalRead += bytesToCopy; + + // Increment the head position. + head += bytesToCopy; + + // If the buffer is now empty, reset the positions to the + // head of the buffer. + normalizeBuffer(); + + // If we still haven't satisified the request, + // read more data. + if( totalRead < targetBufferSize ){ + + // Buffer as much data as we can. + bufferData(); + } + } + + // Return the total number of bytes read. + return totalRead; + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +std::size_t BufferedInputStream::skip( std::size_t num ) + throw ( IOException, lang::exceptions::UnsupportedOperationException ){ + + try{ + // If there's no data left, reset to pointers to the beginning of the + // buffer. + normalizeBuffer(); + + // loop until we've skipped the desired number of bytes + std::size_t totalSkipped = 0; + while( totalSkipped < num ){ + + // Get the remaining bytes to copy. + std::size_t bytesToSkip = min( tail-head, num-totalSkipped ); + + // Increment the head position. + head += bytesToSkip; + + // If the buffer is now empty, reset the positions to the + // head of the buffer. + normalizeBuffer(); + + // If we still haven't satisified the request, + // read more data. + if( totalSkipped < num ){ + + // Buffer as much data as we can. + bufferData(); + } + } + + // Return the total number of bytes read. + return totalSkipped; + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void BufferedInputStream::bufferData() throw ( IOException ){ + + try{ + if( getUnusedBytes() == 0 ){ + throw IOException( __FILE__, __LINE__, + "BufferedInputStream::bufferData - buffer full" ); + } + + // Get the number of bytes currently available on the input stream + // that could be read without blocking. + std::size_t available = inputStream->available(); + + // Calculate the number of bytes that we can read. Always >= 1 byte! + std::size_t bytesToRead = max( (std::size_t)1, min( available, getUnusedBytes() ) ); + + // Read the bytes from the input stream. + std::size_t bytesRead = inputStream->read( getTail(), bytesToRead ); + if( bytesRead == 0 ){ + throw IOException( __FILE__, __LINE__, + "BufferedInputStream::read() - failed reading bytes from stream"); + } + + // Increment the tail to the new end position. + tail += bytesRead; + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + Added: activemq/activemq-cpp/trunk/src/decaf/io/BufferedInputStream.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/io/BufferedInputStream.h?view=auto&rev=543121 ============================================================================== --- activemq/activemq-cpp/trunk/src/decaf/io/BufferedInputStream.h (added) +++ activemq/activemq-cpp/trunk/src/decaf/io/BufferedInputStream.h Thu May 31 04:55:59 2007 @@ -0,0 +1,184 @@ +/* + * 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_IO_BUFFEREDINPUTSTREAM_H_ +#define _DECAF_IO_BUFFEREDINPUTSTREAM_H_ + +#include +#include + +namespace decaf{ +namespace io{ + + /** + * A wrapper around another input stream that performs + * a buffered read, where it reads more data than it needs + * in order to reduce the number of io operations on the + * input stream. + */ + class BufferedInputStream : public FilterInputStream + { + private: + + /** + * The internal buffer. + */ + unsigned char* buffer; + + /** + * The buffer size. + */ + std::size_t bufferSize; + + /** + * The current head of the buffer. + */ + std::size_t head; + + /** + * The current tail of the buffer. + */ + std::size_t tail; + + public: + + /** + * Constructor + * @param stream The target input stream. + * @param own indicates if we own the stream object, defaults to false + */ + BufferedInputStream( InputStream* stream, bool own = false ); + + /** + * Constructor + * @param stream the target input stream + * @param bufferSize the size for the internal buffer. + * @param own indicates if we own the stream object, defaults to false. + */ + BufferedInputStream( InputStream* stream, + std::size_t bufferSize, + bool own = false); + + virtual ~BufferedInputStream(); + + /** + * Indcates the number of bytes avaialable. + * @return the sum of the amount of data avalable + * in the buffer and the data available on the target + * input stream. + */ + virtual std::size_t available() const throw ( IOException ) { + return ( tail - head ) + inputStream->available(); + } + + /** + * Reads a single byte from the buffer. Blocks until + * the data is available. + * @return The next byte. + * @throws IOException thrown if an error occurs. + */ + virtual unsigned char read() throw ( IOException ); + + /** + * Reads an array of bytes from the buffer. Blocks + * until the requested number of bytes are available. + * @param buffer (out) the target buffer. + * @param bufferSize the size of the output buffer. + * @return The number of bytes read. + * @throws IOException thrown if an error occurs. + */ + virtual std::size_t read( unsigned char* buffer, std::size_t bufferSize ) + throw ( IOException ); + + /** + * Skips over and discards n bytes of data from this input stream. The + * skip method may, for a variety of reasons, end up skipping over some + * smaller number of bytes, possibly 0. This may result from any of a + * number of conditions; reaching end of file before n bytes have been + * skipped is only one possibility. The actual number of bytes skipped + * is returned. If n is negative, no bytes are skipped. + *

+ * The skip method of InputStream creates a byte array and then + * repeatedly reads into it until n bytes have been read or the end + * of the stream has been reached. Subclasses are encouraged to + * provide a more efficient implementation of this method. + * @param num - the number of bytes to skip + * @returns total butes skipped + * @throws IOException if an error occurs + */ + virtual std::size_t skip( std::size_t num ) + throw ( io::IOException, lang::exceptions::UnsupportedOperationException ); + + private: + + /** + * Initializes the internal structures. + * @param size of buffer to allocate + */ + void init( std::size_t bufferSize ); + + /** + * Populates the buffer with as much data as possible + * from the target input stream. + * @throws CMSException + */ + void bufferData() throw ( IOException ); + + /** + * Returns the number of bytes that are currently unused + * in the buffer. + */ + std::size_t getUnusedBytes() const{ + return bufferSize - tail; + } + + /** + * Returns the current tail position of the buffer. + */ + unsigned char* getTail(){ + return buffer + tail; + } + + /** + * Initializes the head and tail indicies to the beginning + * of the buffer. + */ + void clear(){ + head = tail = 0; + } + + /** + * Inidicates whether or not the buffer is empty. + */ + bool isEmpty() const{ + return head == tail; + } + + /** + * Clears the buffer if there is no data remaining. + */ + void normalizeBuffer(){ + if( isEmpty() ){ + clear(); + } + } + + }; + +}} + +#endif /*_DECAF_IO_BUFFEREDINPUTSTREAM_H_*/ Added: activemq/activemq-cpp/trunk/src/decaf/io/BufferedOutputStream.cpp URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/io/BufferedOutputStream.cpp?view=auto&rev=543121 ============================================================================== --- activemq/activemq-cpp/trunk/src/decaf/io/BufferedOutputStream.cpp (added) +++ activemq/activemq-cpp/trunk/src/decaf/io/BufferedOutputStream.cpp Thu May 31 04:55:59 2007 @@ -0,0 +1,123 @@ +/* + * 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 "BufferedOutputStream.h" +#include + +using namespace std; +using namespace decaf; +using namespace decaf::io; + +//////////////////////////////////////////////////////////////////////////////// +BufferedOutputStream::BufferedOutputStream( OutputStream* stream, bool own ) +: FilterOutputStream( stream, own ) +{ + // Default to 1k buffer. + init( 1024 ); +} + +//////////////////////////////////////////////////////////////////////////////// +BufferedOutputStream::BufferedOutputStream( OutputStream* stream, + std::size_t bufSize, + bool own ) +: FilterOutputStream( stream, own ) +{ + init( bufSize ); +} + +//////////////////////////////////////////////////////////////////////////////// +BufferedOutputStream::~BufferedOutputStream() +{ + // Destroy the buffer. + if( buffer != NULL ){ + delete [] buffer; + buffer = NULL; + } +} + +//////////////////////////////////////////////////////////////////////////////// +void BufferedOutputStream::init( std::size_t bufSize ){ + + this->bufferSize = bufSize; + + buffer = new unsigned char[bufSize]; + head = tail = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +void BufferedOutputStream::close() throw( lang::Exception ){ + + // Flush this stream. + flush(); + + // Close the delegate stream. + outputStream->close(); +} + +//////////////////////////////////////////////////////////////////////////////// +void BufferedOutputStream::emptyBuffer() throw ( IOException ){ + + if( head != tail ){ + outputStream->write( buffer+head, tail-head ); + } + head = tail = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +void BufferedOutputStream::flush() throw ( IOException ){ + + // Empty the contents of the buffer to the output stream. + emptyBuffer(); + + // Flush the output stream. + outputStream->flush(); +} + +//////////////////////////////////////////////////////////////////////////////// +void BufferedOutputStream::write( const unsigned char c ) throw ( IOException ){ + + if( tail >= bufferSize ){ + emptyBuffer(); + } + + buffer[tail++] = c; +} + +//////////////////////////////////////////////////////////////////////////////// +void BufferedOutputStream::write( const unsigned char* buffer, std::size_t len ) + throw ( IOException ) +{ + // Iterate until all the data is written. + for( std::size_t pos=0; pos < len; ){ + + if( tail >= bufferSize ){ + emptyBuffer(); + } + + // Get the number of bytes left to write. + std::size_t bytesToWrite = min( (int)bufferSize-tail, len-pos ); + + // Copy the data. + memcpy( this->buffer+tail, buffer+pos, bytesToWrite ); + + // Increase the tail position. + tail += bytesToWrite; + + // Decrease the number of bytes to write. + pos += bytesToWrite; + } +} Added: activemq/activemq-cpp/trunk/src/decaf/io/BufferedOutputStream.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/io/BufferedOutputStream.h?view=auto&rev=543121 ============================================================================== --- activemq/activemq-cpp/trunk/src/decaf/io/BufferedOutputStream.h (added) +++ activemq/activemq-cpp/trunk/src/decaf/io/BufferedOutputStream.h Thu May 31 04:55:59 2007 @@ -0,0 +1,118 @@ +/* + * 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_IO_BUFFEREDOUTPUTSTREAM_H_ +#define _DECAF_IO_BUFFEREDOUTPUTSTREAM_H_ + +#include +#include + +namespace decaf{ +namespace io{ + + /** + * Wrapper around another output stream that buffers + * output before writing to the target output stream. + */ + class BufferedOutputStream : public FilterOutputStream + { + private: + + /** + * The internal buffer. + */ + unsigned char* buffer; + + /** + * The size of the internal buffer. + */ + std::size_t bufferSize; + + /** + * The current head of the buffer. + */ + std::size_t head; + + /** + * The current tail of the buffer. + */ + std::size_t tail; + + public: + + /** + * Constructor. + * @param stream the target output stream. + */ + BufferedOutputStream( OutputStream* stream, bool own = false ); + + /** + * Constructor + * @param stream the target output stream. + * @param bufSize the size for the internal buffer. + */ + BufferedOutputStream( OutputStream* stream, + std::size_t bufSize, + bool own = false); + + virtual ~BufferedOutputStream(); + + /** + * Writes a single byte to the output stream. + * @param c the byte. + * @throws IOException thrown if an error occurs. + */ + virtual void write( unsigned char c ) throw ( IOException ); + + /** + * Writes an array of bytes to the output stream. + * @param buffer The array of bytes to write. + * @param len The number of bytes from the buffer to be written. + * @throws IOException thrown if an error occurs. + */ + virtual void write( const unsigned char* buffer, std::size_t len ) + throw ( IOException ); + + /** + * Invokes flush on the target output stream. + * @throws IOException thrown if an error occurs. + */ + virtual void flush() throw ( IOException ); + + /** + * Invokes close on the target output stream. + * @throws CMSException thrown if an error occurs. + */ + void close() throw( lang::Exception ); + + private: + + /** + * Initializes the internal structures. + */ + void init( std::size_t bufSize ); + + /** + * Writes the contents of the buffer to the output stream. + */ + void emptyBuffer() throw ( IOException ); + + }; + +}} + +#endif /*_DECAF_IO_BUFFEREDOUTPUTSTREAM_H_*/ Added: activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayInputStream.cpp URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayInputStream.cpp?view=auto&rev=543121 ============================================================================== --- activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayInputStream.cpp (added) +++ activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayInputStream.cpp Thu May 31 04:55:59 2007 @@ -0,0 +1,127 @@ +/* + * 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 "ByteArrayInputStream.h" +#include + +using namespace std; +using namespace decaf; +using namespace decaf::io; +using namespace decaf::lang; +using namespace decaf::lang::exceptions; + +//////////////////////////////////////////////////////////////////////////////// +ByteArrayInputStream::ByteArrayInputStream(){ +} + +//////////////////////////////////////////////////////////////////////////////// +ByteArrayInputStream::ByteArrayInputStream( const vector& buffer ){ + setBuffer(buffer); +} + +//////////////////////////////////////////////////////////////////////////////// +ByteArrayInputStream::ByteArrayInputStream( const unsigned char* buffer, + std::size_t bufferSize ){ + setByteArray( buffer, bufferSize ); +} + +//////////////////////////////////////////////////////////////////////////////// +ByteArrayInputStream::~ByteArrayInputStream(){ +} + +//////////////////////////////////////////////////////////////////////////////// +void ByteArrayInputStream::setBuffer( const vector& buffer ){ + + // We're using the default buffer. + activeBuffer = &buffer; + + // Begin at the Beginning. + reset(); +} + +//////////////////////////////////////////////////////////////////////////////// +void ByteArrayInputStream::setByteArray( const unsigned char* lbuffer, + std::size_t lbufferSize ){ + // We're using the default buffer. + activeBuffer = &defaultBuffer; + + // Remove old data + defaultBuffer.clear(); + + // Copy data to internal buffer. + for( std::size_t ix = 0; ix < lbufferSize; ++ix ) + { + defaultBuffer.push_back(lbuffer[ix]); + } + + // Begin at the Beginning. + reset(); +} + +//////////////////////////////////////////////////////////////////////////////// +void ByteArrayInputStream::reset() throw ( lang::Exception){ + if( activeBuffer == NULL ){ + throw IOException( __FILE__, __LINE__, "Buffer has not been initialized" ); + } + + // Begin at the Beginning. + pos = activeBuffer->begin(); +} + +//////////////////////////////////////////////////////////////////////////////// +unsigned char ByteArrayInputStream::read() throw ( IOException ){ + if( pos == activeBuffer->end() ){ + throw IOException( __FILE__, __LINE__, "Buffer is empty" ); + } + + return *(pos++); +} + +//////////////////////////////////////////////////////////////////////////////// +std::size_t ByteArrayInputStream::read( unsigned char* buffer, + std::size_t bufferSize ) + throw ( IOException ){ + std::size_t ix = 0; + + for( ; ix < bufferSize; ++ix, ++pos) + { + if(pos == activeBuffer->end()) + { + // We don't have enough data to fulfill the request. + throw IOException( + __FILE__, __LINE__, + "Reached the end of the buffer" ); + } + + buffer[ix] = *(pos); + } + + return ix; +} + +//////////////////////////////////////////////////////////////////////////////// +std::size_t ByteArrayInputStream::skip( std::size_t num ) + throw ( IOException, lang::exceptions::UnsupportedOperationException ){ + + std::size_t ix = 0; + + // Increment the position until we've skipped the desired number + // or we've hit the end of the buffer. + for( ; ix < num && pos != activeBuffer->end(); ++ix, ++pos) {} + + return ix; +} Added: activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayInputStream.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayInputStream.h?view=auto&rev=543121 ============================================================================== --- activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayInputStream.h (added) +++ activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayInputStream.h Thu May 31 04:55:59 2007 @@ -0,0 +1,228 @@ +/* + * 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_IO_BYTEARRAYINPUTSTREAM_H_ +#define _DECAF_IO_BYTEARRAYINPUTSTREAM_H_ + +#include +#include +#include +#include + +namespace decaf{ +namespace io{ + + /** + * Simple implementation of InputStream that wraps around + * a std::vector. + */ + class ByteArrayInputStream : public InputStream + { + private: + + /** + * Default buffer to use, if none provided. + */ + std::vector defaultBuffer; + + /** + * Reference to the buffer being used by this stream. + */ + const std::vector* activeBuffer; + + /** + * iterator to current position in buffer. + */ + std::vector::const_iterator pos; + + /** + * Synchronization object. + */ + util::concurrent::Mutex mutex; + + public: + + /** + * Default Constructor + */ + ByteArrayInputStream(); + + /** + * Creates the input stream and calls setBuffer with the + * specified buffer object. + * @param buffer The buffer to be used. + */ + ByteArrayInputStream( const std::vector& buffer ); + + /** + * Constructor + * @param buffer initial byte array to use to read from + * @param bufferSize the size of the buffer + */ + ByteArrayInputStream( const unsigned char* buffer, + std::size_t bufferSize ); + + virtual ~ByteArrayInputStream(); + + /** + * Sets the internal buffer. The input stream will wrap around + * this buffer and will perform all read operations on it. The + * position will be reinitialized to the beginning of the specified + * buffer. This class will not own the given buffer - it is the + * caller's responsibility to free the memory of the given buffer + * as appropriate. + * @param buffer The buffer to be used. + */ + virtual void setBuffer( const std::vector& buffer ); + + /** + * Sets the data that this reader uses, replaces any existing + * data and resets to beginning of the buffer. + * @param buffer initial byte array to use to read from + * @param bufferSize the size of the buffer + */ + virtual void setByteArray( const unsigned char* buffer, + std::size_t bufferSize ); + + /** + * Waits on a signal from this object, which is generated + * by a call to Notify. Must have this object locked before + * calling. + * @throws Exception + */ + virtual void lock() throw( lang::Exception ){ + mutex.lock(); + } + + /** + * Unlocks the object. + * @throws Exception + */ + virtual void unlock() throw( lang::Exception ){ + mutex.unlock(); + } + + /** + * Waits on a signal from this object, which is generated + * by a call to Notify. Must have this object locked before + * calling. + * @throws Exception + */ + virtual void wait() throw( lang::Exception ){ + mutex.wait(); + } + + /** + * Waits on a signal from this object, which is generated + * by a call to Notify. Must have this object locked before + * calling. This wait will timeout after the specified time + * interval. + * @param millisecs the time in millisecsonds to wait, or + * WAIT_INIFINITE + * @throws Exception + */ + virtual void wait( unsigned long millisecs ) throw( lang::Exception ){ + mutex.wait(millisecs); + } + + /** + * Signals a waiter on this object that it can now wake + * up and continue. Must have this object locked before + * calling. + * @throws Exception + */ + virtual void notify() throw( lang::Exception ){ + mutex.notify(); + } + + /** + * Signals the waiters on this object that it can now wake + * up and continue. Must have this object locked before + * calling. + * @throws Exception + */ + virtual void notifyAll() throw( lang::Exception ){ + mutex.notifyAll(); + } + + /** + * Indcates the number of bytes avaialable. + * @return The number of bytes until the end of the internal buffer. + */ + virtual std::size_t available() const throw ( IOException ) { + if( activeBuffer == NULL ){ + throw IOException( + __FILE__, __LINE__, + "buffer has not been initialized"); + } + + return std::distance( pos, activeBuffer->end() ); + } + + /** + * Reads a single byte from the buffer. + * @return The next byte. + * @throws IOException thrown if an error occurs. + */ + virtual unsigned char read() throw ( IOException ); + + /** + * Reads an array of bytes from the buffer. + * @param buffer (out) the target buffer. + * @param bufferSize the size of the output buffer. + * @return The number of bytes read. + * @throws IOException thrown if an error occurs. + */ + virtual std::size_t read( unsigned char* buffer, std::size_t bufferSize ) + throw ( IOException ); + + /** + * Closes the target input stream. + * @throws IOException thrown if an error occurs. + */ + virtual void close() throw( lang::Exception ){ /* do nothing */ } + + /** + * Resets the read index to the beginning of the byte + * array. + */ + virtual void reset() throw ( lang::Exception); + + /** + * Skips over and discards n bytes of data from this input stream. The + * skip method may, for a variety of reasons, end up skipping over some + * smaller number of bytes, possibly 0. This may result from any of a + * number of conditions; reaching end of file before n bytes have been + * skipped is only one possibility. The actual number of bytes skipped + * is returned. If n is negative, no bytes are skipped. + *

+ * The skip method of InputStream creates a byte array and then + * repeatedly reads into it until n bytes have been read or the end + * of the stream has been reached. Subclasses are encouraged to + * provide a more efficient implementation of this method. + * @param num - the number of bytes to skip + * @returns total butes skipped + * @throws IOException if an error occurs + */ + virtual std::size_t skip( std::size_t num ) + throw ( io::IOException, lang::exceptions::UnsupportedOperationException ); + + }; + +}} + +#endif /*_DECAF_IO_BYTEARRAYINPUTSTREAM_H_*/ Added: activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayOutputStream.cpp URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayOutputStream.cpp?view=auto&rev=543121 ============================================================================== --- activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayOutputStream.cpp (added) +++ activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayOutputStream.cpp Thu May 31 04:55:59 2007 @@ -0,0 +1,69 @@ +/* + * 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 "ByteArrayOutputStream.h" +#include + +using namespace std; +using namespace decaf; +using namespace decaf::io; +using namespace decaf::lang; + +//////////////////////////////////////////////////////////////////////////////// +ByteArrayOutputStream::ByteArrayOutputStream() +{ + activeBuffer = &defaultBuffer; +} + +//////////////////////////////////////////////////////////////////////////////// +ByteArrayOutputStream::ByteArrayOutputStream( vector& buffer) +{ + setBuffer( buffer ); +} + +//////////////////////////////////////////////////////////////////////////////// +void ByteArrayOutputStream::setBuffer( vector& buffer) +{ + activeBuffer = &buffer; +} + +//////////////////////////////////////////////////////////////////////////////// +void ByteArrayOutputStream::clear() throw ( IOException ) +{ + // Empty the contents of the buffer to the output stream. + activeBuffer->clear(); +} + +//////////////////////////////////////////////////////////////////////////////// +void ByteArrayOutputStream::write( unsigned char c ) + throw ( IOException ) +{ + activeBuffer->push_back( c ); +} + +//////////////////////////////////////////////////////////////////////////////// +void ByteArrayOutputStream::write( const unsigned char* buffer, + std::size_t len ) + throw ( IOException ) +{ + // Iterate until all the data is written. + for( std::size_t ix = 0; ix < len; ++ix) + { + activeBuffer->push_back( buffer[ix] ); + } +} + Added: activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayOutputStream.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayOutputStream.h?view=auto&rev=543121 ============================================================================== --- activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayOutputStream.h (added) +++ activemq/activemq-cpp/trunk/src/decaf/io/ByteArrayOutputStream.h Thu May 31 04:55:59 2007 @@ -0,0 +1,193 @@ +/* + * 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_IO_BYTEARRAYOUTPUTSTREAM_H_ +#define _DECAF_IO_BYTEARRAYOUTPUTSTREAM_H_ + +#include +#include +#include + +namespace decaf{ +namespace io{ + + class ByteArrayOutputStream : public OutputStream + { + private: + + /** + * Default buffer to use, if none provided. + */ + std::vector defaultBuffer; + + /** + * Reference to the buffer being used by this stream. + */ + std::vector* activeBuffer; + + /** + * Synchronization object. + */ + util::concurrent::Mutex mutex; + + public: + + /** + * Default Constructor - uses a default internal buffer + */ + ByteArrayOutputStream(); + + /** + * Uses the given buffer as the target. Calls setBuffer. + * @param buffer the target buffer. + */ + ByteArrayOutputStream( std::vector& buffer ); + + /** + * Destructor + */ + virtual ~ByteArrayOutputStream() {}; + + /** + * Sets the internal buffer. This input stream will wrap around + * the given buffer and all writes will be performed directly on + * the buffer. This object does not retain control of the buffer's + * lifetime however - this is the job of the caller. + * @param buffer The target buffer. + */ + virtual void setBuffer( std::vector& buffer ); + + /** + * Get a snapshot of the data + * @return pointer to the data + */ + virtual const unsigned char* getByteArray() const { + if( activeBuffer->size() == 0 ){ + return NULL; + } + + return &(*activeBuffer)[0]; + } + + /** + * Get the Size of the Internal Buffer + * @return size of the internal buffer + */ + virtual std::size_t getByteArraySize() const { + return activeBuffer->size(); + } + + /** + * Waits on a signal from this object, which is generated + * by a call to Notify. Must have this object locked before + * calling. + * @throws Exception + */ + virtual void lock() throw( lang::Exception ){ + mutex.lock(); + } + + /** + * Unlocks the object. + * @throws Exception + */ + virtual void unlock() throw( lang::Exception ){ + mutex.unlock(); + } + + /** + * Waits on a signal from this object, which is generated + * by a call to Notify. Must have this object locked before + * calling. + * @throws Exception + */ + virtual void wait() throw( lang::Exception ){ + mutex.wait(); + } + + /** + * Waits on a signal from this object, which is generated + * by a call to Notify. Must have this object locked before + * calling. This wait will timeout after the specified time + * interval. + * @param time in millisecsonds to wait, or WAIT_INIFINITE + * @throws Exception + */ + virtual void wait( unsigned long millisecs ) throw( lang::Exception ){ + mutex.wait(millisecs); + } + + /** + * Signals a waiter on this object that it can now wake + * up and continue. Must have this object locked before + * calling. + * @throws Exception + */ + virtual void notify() throw( lang::Exception ){ + mutex.notify(); + } + + /** + * Signals the waiters on this object that it can now wake + * up and continue. Must have this object locked before + * calling. + * @throws Exception + */ + virtual void notifyAll() throw( lang::Exception ){ + mutex.notifyAll(); + } + + /** + * Writes a single byte to the output stream. + * @param c the byte. + * @throws IOException thrown if an error occurs. + */ + virtual void write( unsigned char c ) + throw ( IOException ); + + /** + * Writes an array of bytes to the output stream. + * @param buffer The array of bytes to write. + * @param len The number of bytes from the buffer to be written. + * @throws IOException thrown if an error occurs. + */ + virtual void write( const unsigned char* buffer, std::size_t len ) + throw ( IOException ); + + /** + * Invokes flush on the target output stream, has no affect. + * @throws IOException + */ + virtual void flush() throw ( IOException ){ /* do nothing */ } + + /** + * Clear current Stream contents + * @throws IOException + */ + virtual void clear() throw ( IOException ); + + /** + * Invokes close on the target output stream. + * @throws CMSException + */ + void close() throw( lang::Exception ){ /* do nothing */ } + + }; + +}} + +#endif /*_DECAF_IO_BYTEARRAYOUTPUTSTREAM_H_*/ Added: activemq/activemq-cpp/trunk/src/decaf/io/Closeable.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/io/Closeable.h?view=auto&rev=543121 ============================================================================== --- activemq/activemq-cpp/trunk/src/decaf/io/Closeable.h (added) +++ activemq/activemq-cpp/trunk/src/decaf/io/Closeable.h Thu May 31 04:55:59 2007 @@ -0,0 +1,46 @@ +/* + * 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_IO_CLOSEABLE_H_ +#define _DECAF_IO_CLOSEABLE_H_ + +#include + +namespace decaf{ +namespace io{ + + /** + * Interface for a class that implements the close method. + */ + class Closeable{ + + public: + + virtual ~Closeable(){} + + /** + * Closes this object and deallocates the appropriate resources. + * The object is generally no longer usable after calling close. + * @throws CMSException + */ + virtual void close() throw( lang::Exception ) = 0; + + }; + +}} + +#endif /*_DECAF_IO_CLOSEABLE_H_*/ Added: activemq/activemq-cpp/trunk/src/decaf/io/DataInputStream.cpp URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/io/DataInputStream.cpp?view=auto&rev=543121 ============================================================================== --- activemq/activemq-cpp/trunk/src/decaf/io/DataInputStream.cpp (added) +++ activemq/activemq-cpp/trunk/src/decaf/io/DataInputStream.cpp Thu May 31 04:55:59 2007 @@ -0,0 +1,333 @@ +/* + * 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 + +using namespace std; +using namespace decaf; +using namespace decaf::io; +using namespace decaf::util; +using namespace decaf::lang; +using namespace decaf::lang::exceptions; + +//////////////////////////////////////////////////////////////////////////////// +DataInputStream::DataInputStream( InputStream* inputStream, bool own ) + : FilterInputStream( inputStream, own ) {} + +//////////////////////////////////////////////////////////////////////////////// +DataInputStream::~DataInputStream() {} + +//////////////////////////////////////////////////////////////////////////////// +std::size_t DataInputStream::read( std::vector& buffer ) + throw ( io::IOException ) { + + try { + return this->read( &buffer[0], 0, buffer.size() ); + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +std::size_t DataInputStream::read( unsigned char* buffer, + std::size_t offset, + std::size_t length ) + throw ( IOException, IndexOutOfBoundsException, NullPointerException ) { + + try { + + if( buffer == NULL ) { + throw NullPointerException( + __FILE__, __LINE__, + "DataInputStream::read - Buffer is null" ); + } + + std::size_t read = 0; + + try { + read = inputStream->read( &buffer[offset], length ); + } catch( EOFException& ex ){ + if( read == 0 ) + return -1; + } + + if( read == 0 ){ + throw IOException( + __FILE__, __LINE__, + "DataInputStream::read - failed to extract data, not EOF." ); + } + + return read; + } + DECAF_CATCH_RETHROW( NullPointerException ) + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +bool DataInputStream::readBoolean() + throw( IOException, EOFException ) { + + try { + char value = 0; + this->readFully( ( unsigned char* )&value, 0, sizeof( char ) ); + return (char)( value != 0 ); + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +char DataInputStream::readByte() + throw ( IOException, EOFException ) { + + try { + char value = 0; + this->readFully( ( unsigned char* )&value, 0, sizeof( char ) ); + return (char)( value ); + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +unsigned char DataInputStream::readUnsignedByte() + throw ( IOException, EOFException ) { + + try { + unsigned char value = 0; + this->readFully( ( unsigned char* )&value, 0, sizeof( unsigned char ) ); + return (char)( value ); + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +char DataInputStream::readChar() throw ( IOException, EOFException ) { + try { + char value = 0; + this->readFully( ( unsigned char* )&value, 0, sizeof( char ) ); + return (char)( value ); + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +short DataInputStream::readShort() throw ( io::IOException, io::EOFException ) { + try { + unsigned short value = 0; + + unsigned char byte1 = this->readByte(); + unsigned char byte2 = this->readByte(); + + value |= (byte1 << 8 | byte2 << 0); + + return value; + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +unsigned short DataInputStream::readUnsignedShort() + throw ( io::IOException, io::EOFException ) { + try { + + unsigned short value = 0; + + unsigned char byte1 = this->readByte(); + unsigned char byte2 = this->readByte(); + + value |= (byte1 << 8 | byte2 << 0); + + return value; + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +int DataInputStream::readInt() throw ( io::IOException, io::EOFException ) { + try { + + unsigned int value = 0; + + unsigned char byte1 = this->readByte(); + unsigned char byte2 = this->readByte(); + unsigned char byte3 = this->readByte(); + unsigned char byte4 = this->readByte(); + + value |= (byte1 << 24 | byte2 << 16 | byte3 << 8 | byte4 << 0); + + return value; + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +double DataInputStream::readDouble() throw ( io::IOException, io::EOFException ) { + try { + + unsigned long long lvalue = this->readLong(); + double value = 0.0; + memcpy( &value, &lvalue, sizeof( unsigned long long ) ); + return value; + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +float DataInputStream::readFloat() throw ( io::IOException, io::EOFException ) { + try { + + unsigned int lvalue = this->readInt(); + float value = 0.0f; + memcpy( &value, &lvalue, sizeof( unsigned int ) ); + return value; + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +long long DataInputStream::readLong() + throw ( io::IOException, io::EOFException ) { + try { + + unsigned long long value = 0; + + unsigned long long byte1 = this->readByte() & 0x00000000000000FFULL; + unsigned long long byte2 = this->readByte() & 0x00000000000000FFULL; + unsigned long long byte3 = this->readByte() & 0x00000000000000FFULL; + unsigned long long byte4 = this->readByte() & 0x00000000000000FFULL; + unsigned long long byte5 = this->readByte() & 0x00000000000000FFULL; + unsigned long long byte6 = this->readByte() & 0x00000000000000FFULL; + unsigned long long byte7 = this->readByte() & 0x00000000000000FFULL; + unsigned long long byte8 = this->readByte() & 0x00000000000000FFULL; + + value = ( byte1 << 56 | byte2 << 48 | byte3 << 40 | byte4 << 32 | + byte5 << 24 | byte6 << 16 | byte7 << 8 | byte8 << 0 ); + + return value; + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +std::string DataInputStream::readString() + throw ( io::IOException, io::EOFException ) { + try { + std::string retVal; + char temp = 0; + + while( true ){ + temp = readChar(); + + // if null is found we are done. + if( temp == '\0' ){ + break; + } + + // Append no matter what + retVal += temp; + } + + return retVal; + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +std::string DataInputStream::readUTF() + throw ( io::IOException, io::EOFException ) { + try { + std::string buffer; + unsigned short len = readUnsignedShort(); + buffer.resize(len); + readFully( (unsigned char*)buffer.c_str(), 0, len ); + return buffer; + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataInputStream::readFully( std::vector< unsigned char >& buffer ) + throw ( io::IOException, io::EOFException ) { + try { + this->readFully( &buffer[0], 0, buffer.size() ); + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataInputStream::readFully( unsigned char* buffer, + std::size_t offset, + std::size_t length ) + throw ( io::IOException, + io::EOFException, + lang::exceptions::IndexOutOfBoundsException, + lang::exceptions::NullPointerException ) +{ + try { + + if( buffer == NULL ) { + throw NullPointerException( + __FILE__, __LINE__, + "DataInputStream::read - Buffer is null" ); + } + + std::size_t n = 0; + while( n < length ) { + std::size_t count = inputStream->read( &buffer[offset + n], (length - n) ); + if( count == (std::size_t)-1 ) { + throw EOFException( + __FILE__, __LINE__, + "DataInputStream::readFully - Reached EOF" ); + } + n += count; + } + } + DECAF_CATCH_RETHROW( NullPointerException ) + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +std::size_t DataInputStream::skip( std::size_t num ) +throw( io::IOException, lang::exceptions::UnsupportedOperationException ) { + try { + std::size_t total = 0; + std::size_t cur = 0; + + while( ( total < num ) && + ( ( cur = inputStream->skip( num-total ) ) > 0 ) ) { + total += cur; + } + + return total; + } + DECAF_CATCH_RETHROW( IOException ) + DECAF_CATCHALL_THROW( IOException ) +}