You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2007/04/24 16:33:47 UTC
svn commit: r531957 [1/2] -
/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/
Author: tabish
Date: Tue Apr 24 07:33:45 2007
New Revision: 531957
URL: http://svn.apache.org/viewvc?view=rev&rev=531957
Log:
http://issues.apache.org/activemq/browse/AMQCPP-103
Building Decaf lib
Added:
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BlockingByteArrayInputStream.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BlockingByteArrayInputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedInputStream.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedInputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedOutputStream.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedOutputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayInputStream.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayInputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayOutputStream.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayOutputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataInputStream.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataInputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataOutputStream.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataOutputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/EOFException.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterInputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterOutputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/IOException.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/InputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/OutputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/Reader.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/StandardErrorOutputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/Writer.h
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BlockingByteArrayInputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BlockingByteArrayInputStream.cpp?view=auto&rev=531957
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BlockingByteArrayInputStream.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BlockingByteArrayInputStream.cpp Tue Apr 24 07:33:45 2007
@@ -0,0 +1,151 @@
+/*
+ * 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 <algorithm>
+
+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 (cms::CMSException){
+
+ 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, 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/activemq-cpp/src/main/decaf/io/BlockingByteArrayInputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BlockingByteArrayInputStream.h?view=auto&rev=531957
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BlockingByteArrayInputStream.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BlockingByteArrayInputStream.h Tue Apr 24 07:33:45 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 <decaf/io/InputStream.h>
+#include <activemq/concurrent/Mutex.h>
+#include <vector>
+
+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<unsigned char> buffer;
+
+ /**
+ * iterator to current position in buffer.
+ */
+ std::vector<unsigned char>::const_iterator pos;
+
+ /**
+ * Synchronization object.
+ */
+ 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 ActiveMQException
+ */
+ virtual void lock() throw( exceptions::ActiveMQException ){
+ mutex.lock();
+ }
+
+ /**
+ * Unlocks the object.
+ * @throws ActiveMQException
+ */
+ virtual void unlock() throw( exceptions::ActiveMQException ){
+ 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 ActiveMQException
+ */
+ virtual void wait() throw( exceptions::ActiveMQException ){
+ 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 ActiveMQException
+ */
+ virtual void wait( unsigned long millisecs ) throw( exceptions::ActiveMQException ){
+ 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 ActiveMQException
+ */
+ virtual void notify() throw( exceptions::ActiveMQException ){
+ mutex.notify();
+ }
+
+ /**
+ * Signals the waiters on this object that it can now wake
+ * up and continue. Must have this object locked before
+ * calling.
+ * @throws ActiveMQException
+ */
+ virtual void notifyAll() throw( exceptions::ActiveMQException ){
+ 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 (cms::CMSException);
+
+ /**
+ * 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.
+ * <p>
+ * 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, exceptions::UnsupportedOperationException );
+
+ };
+
+}}
+
+#endif /*_DECAF_IO_BLOCKINGBYTEARRAYINPUTSTREAM_H_*/
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedInputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedInputStream.cpp?view=auto&rev=531957
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedInputStream.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedInputStream.cpp Tue Apr 24 07:33:45 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 <algorithm>
+
+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;
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_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;
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_CATCHALL_THROW( IOException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::size_t BufferedInputStream::skip( std::size_t num )
+ throw ( IOException, 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;
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_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;
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_CATCHALL_THROW( IOException )
+}
+
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedInputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedInputStream.h?view=auto&rev=531957
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedInputStream.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedInputStream.h Tue Apr 24 07:33:45 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 <decaf/io/FilterInputStream.h>
+#include <assert.h>
+
+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.
+ * <p>
+ * 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, 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/activemq-cpp/src/main/decaf/io/BufferedOutputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedOutputStream.cpp?view=auto&rev=531957
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedOutputStream.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedOutputStream.cpp Tue Apr 24 07:33:45 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 <algorithm>
+
+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( cms::CMSException ){
+
+ // 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/activemq-cpp/src/main/decaf/io/BufferedOutputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedOutputStream.h?view=auto&rev=531957
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedOutputStream.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/BufferedOutputStream.h Tue Apr 24 07:33:45 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 <decaf/io/FilterOutputStream.h>
+#include <assert.h>
+
+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( cms::CMSException );
+
+ 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/activemq-cpp/src/main/decaf/io/ByteArrayInputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayInputStream.cpp?view=auto&rev=531957
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayInputStream.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayInputStream.cpp Tue Apr 24 07:33:45 2007
@@ -0,0 +1,124 @@
+/*
+ * 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 <algorithm>
+
+using namespace std;
+using namespace decaf;
+using namespace decaf::io;
+using namespace decaf::exceptions;
+
+////////////////////////////////////////////////////////////////////////////////
+ByteArrayInputStream::ByteArrayInputStream(){
+}
+
+////////////////////////////////////////////////////////////////////////////////
+ByteArrayInputStream::ByteArrayInputStream( const vector<unsigned char>& buffer ){
+ setBuffer(buffer);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+ByteArrayInputStream::ByteArrayInputStream( const unsigned char* buffer,
+ std::size_t bufferSize ){
+ setByteArray( buffer, bufferSize );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+ByteArrayInputStream::~ByteArrayInputStream(){
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArrayInputStream::setBuffer( const vector<unsigned char>& 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 (cms::CMSException){
+ 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, 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/activemq-cpp/src/main/decaf/io/ByteArrayInputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayInputStream.h?view=auto&rev=531957
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayInputStream.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayInputStream.h Tue Apr 24 07:33:45 2007
@@ -0,0 +1,226 @@
+/*
+ * 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 <decaf/io/InputStream.h>
+#include <activemq/concurrent/Mutex.h>
+#include <vector>
+#include <algorithm>
+
+namespace decaf{
+namespace io{
+
+ /**
+ * Simple implementation of InputStream that wraps around
+ * a std::vector<unsigned char>.
+ */
+ class ByteArrayInputStream : public InputStream
+ {
+ private:
+
+ /**
+ * Default buffer to use, if none provided.
+ */
+ std::vector<unsigned char> defaultBuffer;
+
+ /**
+ * Reference to the buffer being used by this stream.
+ */
+ const std::vector<unsigned char>* activeBuffer;
+
+ /**
+ * iterator to current position in buffer.
+ */
+ std::vector<unsigned char>::const_iterator pos;
+
+ /**
+ * Synchronization object.
+ */
+ 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<unsigned char>& 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<unsigned char>& 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 ActiveMQException
+ */
+ virtual void lock() throw( exceptions::ActiveMQException ){
+ mutex.lock();
+ }
+
+ /**
+ * Unlocks the object.
+ * @throws ActiveMQException
+ */
+ virtual void unlock() throw( exceptions::ActiveMQException ){
+ 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 ActiveMQException
+ */
+ virtual void wait() throw( exceptions::ActiveMQException ){
+ 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 ActiveMQException
+ */
+ virtual void wait( unsigned long millisecs ) throw( exceptions::ActiveMQException ){
+ 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 ActiveMQException
+ */
+ virtual void notify() throw( exceptions::ActiveMQException ){
+ mutex.notify();
+ }
+
+ /**
+ * Signals the waiters on this object that it can now wake
+ * up and continue. Must have this object locked before
+ * calling.
+ * @throws ActiveMQException
+ */
+ virtual void notifyAll() throw( exceptions::ActiveMQException ){
+ 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(cms::CMSException){ /* do nothing */ }
+
+ /**
+ * Resets the read index to the beginning of the byte
+ * array.
+ */
+ virtual void reset() throw (cms::CMSException);
+
+ /**
+ * 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.
+ * <p>
+ * 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, exceptions::UnsupportedOperationException );
+
+ };
+
+}}
+
+#endif /*_DECAF_IO_BYTEARRAYINPUTSTREAM_H_*/
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayOutputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayOutputStream.cpp?view=auto&rev=531957
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayOutputStream.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayOutputStream.cpp Tue Apr 24 07:33:45 2007
@@ -0,0 +1,68 @@
+/*
+ * 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 <algorithm>
+
+using namespace std;
+using namespace decaf;
+using namespace decaf::io;
+
+////////////////////////////////////////////////////////////////////////////////
+ByteArrayOutputStream::ByteArrayOutputStream()
+{
+ activeBuffer = &defaultBuffer;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+ByteArrayOutputStream::ByteArrayOutputStream( vector<unsigned char>& buffer)
+{
+ setBuffer( buffer );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArrayOutputStream::setBuffer( vector<unsigned char>& 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/activemq-cpp/src/main/decaf/io/ByteArrayOutputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayOutputStream.h?view=auto&rev=531957
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayOutputStream.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/ByteArrayOutputStream.h Tue Apr 24 07:33:45 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 <decaf/io/OutputStream.h>
+#include <activemq/concurrent/Mutex.h>
+#include <vector>
+
+namespace decaf{
+namespace io{
+
+ class ByteArrayOutputStream : public OutputStream
+ {
+ private:
+
+ /**
+ * Default buffer to use, if none provided.
+ */
+ std::vector<unsigned char> defaultBuffer;
+
+ /**
+ * Reference to the buffer being used by this stream.
+ */
+ std::vector<unsigned char>* activeBuffer;
+
+ /**
+ * Synchronization object.
+ */
+ 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<unsigned char>& 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<unsigned char>& 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 ActiveMQException
+ */
+ virtual void lock() throw( exceptions::ActiveMQException ){
+ mutex.lock();
+ }
+
+ /**
+ * Unlocks the object.
+ * @throws ActiveMQException
+ */
+ virtual void unlock() throw( exceptions::ActiveMQException ){
+ 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 ActiveMQException
+ */
+ virtual void wait() throw( exceptions::ActiveMQException ){
+ 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 ActiveMQException
+ */
+ virtual void wait( unsigned long millisecs ) throw( exceptions::ActiveMQException ){
+ 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 ActiveMQException
+ */
+ virtual void notify() throw( exceptions::ActiveMQException ){
+ mutex.notify();
+ }
+
+ /**
+ * Signals the waiters on this object that it can now wake
+ * up and continue. Must have this object locked before
+ * calling.
+ * @throws ActiveMQException
+ */
+ virtual void notifyAll() throw( exceptions::ActiveMQException ){
+ 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( cms::CMSException ){ /* do nothing */ }
+
+ };
+
+}}
+
+#endif /*_DECAF_IO_BYTEARRAYOUTPUTSTREAM_H_*/
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataInputStream.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataInputStream.cpp?view=auto&rev=531957
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataInputStream.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataInputStream.cpp Tue Apr 24 07:33:45 2007
@@ -0,0 +1,334 @@
+/*
+ * 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 <activemq/io/DataInputStream.h>
+#include <decaf/util/Endian.h>
+
+using namespace std;
+using namespace decaf;
+using namespace decaf::io;
+using namespace decaf::util;
+using namespace decaf::lang;
+
+////////////////////////////////////////////////////////////////////////////////
+DataInputStream::DataInputStream( InputStream* inputStream, bool own )
+ : FilterInputStream( inputStream, own ) {}
+
+////////////////////////////////////////////////////////////////////////////////
+DataInputStream::~DataInputStream() {}
+
+////////////////////////////////////////////////////////////////////////////////
+std::size_t DataInputStream::read( std::vector<unsigned char>& buffer )
+ throw ( io::IOException ) {
+
+ try {
+ return this->read( &buffer[0], 0, buffer.size() );
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_CATCHALL_THROW( IOException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::size_t DataInputStream::read( unsigned char* buffer,
+ std::size_t offset,
+ std::size_t length )
+ throw ( io::IOException, exceptions::IndexOutOfBoundsException,
+ exceptions::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( io::EOFException& ex ){
+ if( read == 0 )
+ return -1;
+ }
+
+ if( read == 0 ){
+ throw IOException(
+ __FILE__, __LINE__,
+ "DataInputStream::read - failed to extract data, not EOF." );
+ }
+
+ return read;
+ }
+ AMQ_CATCH_RETHROW( NullPointerException )
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_CATCHALL_THROW( IOException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool DataInputStream::readBoolean()
+ throw( io::IOException, io::EOFException ) {
+
+ try {
+ char value = 0;
+ this->readFully( ( unsigned char* )&value, 0, sizeof( char ) );
+ return (char)( value != 0 );
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_CATCHALL_THROW( IOException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+char DataInputStream::readByte()
+ throw ( io::IOException, io::EOFException ) {
+
+ try {
+ char value = 0;
+ this->readFully( ( unsigned char* )&value, 0, sizeof( char ) );
+ return (char)( value );
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_CATCHALL_THROW( IOException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+unsigned char DataInputStream::readUnsignedByte()
+ throw ( io::IOException, io::EOFException ) {
+
+ try {
+ unsigned char value = 0;
+ this->readFully( ( unsigned char* )&value, 0, sizeof( unsigned char ) );
+ return (char)( value );
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_CATCHALL_THROW( IOException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+char DataInputStream::readChar() throw ( io::IOException, io::EOFException ) {
+ try {
+ char value = 0;
+ this->readFully( ( unsigned char* )&value, 0, sizeof( char ) );
+ return (char)( value );
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_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;
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_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;
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_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;
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_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;
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_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;
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_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;
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_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;
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_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;
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_CATCHALL_THROW( IOException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DataInputStream::readFully( std::vector< unsigned char >& buffer )
+ throw ( io::IOException, io::EOFException ) {
+ try {
+ this->readFully( &buffer[0], 0, buffer.size() );
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_CATCHALL_THROW( IOException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DataInputStream::readFully( unsigned char* buffer,
+ std::size_t offset,
+ std::size_t length )
+ throw ( io::IOException,
+ io::EOFException,
+ exceptions::IndexOutOfBoundsException,
+ 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;
+ }
+ }
+ AMQ_CATCH_RETHROW( NullPointerException )
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_CATCHALL_THROW( IOException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::size_t DataInputStream::skip( std::size_t num )
+throw( io::IOException, 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;
+ }
+ AMQ_CATCH_RETHROW( IOException )
+ AMQ_CATCHALL_THROW( IOException )
+}
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataInputStream.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataInputStream.h?view=auto&rev=531957
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataInputStream.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataInputStream.h Tue Apr 24 07:33:45 2007
@@ -0,0 +1,354 @@
+/*
+ * 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_DATAINPUTSTREAM_H_
+#define _DECAF_IO_DATAINPUTSTREAM_H_
+
+#include <decaf/io/FilterInputStream.h>
+#include <decaf/io/IOException.h>
+#include <decaf/io/EOFException.h>
+#include <activemq/exceptions/NullPointerException.h>
+#include <activemq/exceptions/IndexOutOfBoundsException.h>
+
+namespace decaf{
+namespace io{
+
+ /**
+ * A data input stream lets an application read primitive Java data
+ * types from an underlying input stream in a machine-independent way.
+ * An application uses a data output stream to write data that can
+ * later be read by a data input stream.
+ *
+ * Due to the lack of garbage collection in C++ a design decision was
+ * made to add a boolean parameter to the constructor indicating if the
+ * wrapped <code>InputStream</code> is owned by this object. That way
+ * creation of the underlying stream can occur in a Java like way. Ex:
+ *
+ * DataInputStream os = new DataInputStream( new InputStream(), true )
+ */
+ class DataInputStream : public FilterInputStream
+ {
+ public:
+
+ /**
+ * Creates a DataInputStream that uses the specified underlying
+ * InputStream.
+ * @param inputStream the InputStream instance to wrap.
+ * @param own, indicates if this class owns the wrapped string
+ * defaults to false.
+ */
+ DataInputStream( InputStream* inputStream, bool own = false );
+
+ virtual ~DataInputStream();
+
+ /**
+ * Reads some number of bytes from the contained input stream and
+ * stores them into the buffer array b. The number of bytes actually
+ * read is returned as an integer. This method blocks until input
+ * data is available, end of file is detected, or an exception is
+ * thrown.
+ * <p>
+ * If the length of buffer is zero, then no bytes are read and 0 is
+ * returned; otherwise, there is an attempt to read at least one
+ * byte. If no byte is available because the stream is at end of
+ * file, the value -1 is returned; otherwise, at least one byte is
+ * read and stored into buffer.
+ * <p>
+ * The first byte read is stored into element buffer[0], the next one
+ * into buffer[1], and so on. The number of bytes read is, at most,
+ * equal to the length of buffer. Let k be the number of bytes actually
+ * read; these bytes will be stored in elements b[0] through b[k-1],
+ * leaving elements buffer[k] through buffer[buffer.length-1]
+ * unaffected.
+ * <p>
+ * If the first byte cannot be read for any reason other than end
+ * of file, then an IOException is thrown. In particular, an
+ * IOException is thrown if the input stream has been closed.
+ * <p>
+ * The read( buffer ) method has the same effect as:
+ * read( buffer, 0, b.length )
+ * @param buffer - byte array to insert read data into
+ * @returns the total number of bytes read, or -1 if there is no
+ * more data because the stream is EOF.
+ * @throws IOException
+ */
+ virtual std::size_t read( std::vector< unsigned char >& buffer )
+ throw ( io::IOException );
+
+ /**
+ * Reads up to len bytes of data from the contained input stream
+ * into an array of bytes. An attempt is made to read as many as
+ * len bytes, but a smaller number may be read, possibly zero. The
+ * number of bytes actually read is returned as an integer.
+ * <p>
+ * This method blocks until input data is available, end of file is
+ * detected, or an exception is thrown.
+ * <p>
+ * If buffer is null, a NullPointerException is thrown.
+ * <p>
+ * If off is negative, or len is negative then an
+ * IndexOutOfBoundsException is thrown, if off + len is greater that
+ * the allocated length of the array, an IOException will result
+ * depending on the platform and compiler settings.
+ * <p>
+ * If len is zero, then no bytes are read and 0 is returned;
+ * otherwise, there is an attempt to read at least one byte. If no
+ * byte is available because the stream is at end of file, the
+ * value -1 is returned; otherwise, at least one byte is read and
+ * stored into buffer.
+ * <p>
+ * The first byte read is stored into element b[off], the next one
+ * into buffer[off+1], and so on. The number of bytes read is, at most,
+ * equal to len. Let k be the number of bytes actually read; these
+ * bytes will be stored in elements buffer[off] through buffer[off+k-1],
+ * leaving elements buffer[off+k] through buffer[off+len-1] unaffected.
+ * <p>
+ * In every case, elements buffer[0] through buffer[off] and elements
+ * buffer[off+len] through buffer[buffer.length-1] are unaffected.
+ * <p>
+ * If the first byte cannot be read for any reason other than end of
+ * file, then an IOException is thrown. In particular, an IOException
+ * is thrown if the input stream has been closed.
+ * @param buffer - byte array to insert read data into
+ * @param offset - location in buffer to start writing
+ * @param length - number of bytes to read
+ * @returns the total number of bytes read, or -1 if there is no
+ * more data because the stream is EOF.
+ * @throws IOException
+ */
+ virtual std::size_t read( unsigned char* buffer,
+ std::size_t offset,
+ std::size_t length )
+ throw ( io::IOException,
+ exceptions::IndexOutOfBoundsException,
+ exceptions::NullPointerException );
+
+ /**
+ * Reads one input byte and returns true if that byte is nonzero,
+ * false if that byte is zero.
+ * @returns the boolean value read.
+ * @throws IOException
+ * @throws EOFException
+ */
+ virtual bool readBoolean()
+ throw( io::IOException, io::EOFException );
+
+ /**
+ * Reads and returns one input byte. The byte is treated as a
+ * signed value in the range -128 through 127, inclusive.
+ * @returns the 8-bit value read.
+ * @throws IOException
+ * @throws EOFException
+ */
+ virtual char readByte()
+ throw ( io::IOException, io::EOFException );
+
+ /**
+ * Reads one input byte, zero-extends it to type int, and returns
+ * the result, which is therefore in the range 0 through 255.
+ * @returns the 8 bit unsigned value read
+ * @throws IOException
+ * @throws EOFException
+ */
+ virtual unsigned char readUnsignedByte()
+ throw ( io::IOException, io::EOFException );
+
+ /**
+ * Reads an input char and returns the char value. A ascii char
+ * is made up of one bytes. This returns the same result as
+ * <code>readByte</code>
+ * @returns the 8 bit char read
+ * @throws IOException
+ * @throws EOFException
+ */
+ virtual char readChar() throw ( io::IOException, io::EOFException );
+
+ /**
+ * Reads eight input bytes and returns a double value. It does this
+ * by first constructing a long long value in exactly the manner of
+ * the readlong method, then converting this long value to a double
+ * in exactly the manner of the method Double.longBitsToDouble.
+ * @returns the double value read
+ * @throws IOException
+ * @throws EOFException
+ */
+ virtual double readDouble()
+ throw ( io::IOException, io::EOFException );
+
+ /**
+ * Reads four input bytes and returns a float value. It does this
+ * by first constructing an int value in exactly the manner of the
+ * readInt method, then converting this int value to a float in
+ * exactly the manner of the method Float.intBitsToFloat.
+ * @returns the float value read
+ * @throws IOException
+ * @throws EOFException
+ */
+ virtual float readFloat() throw ( io::IOException, io::EOFException );
+
+ /**
+ * Reads four input bytes and returns an int value. Let a be the
+ * first byte read, b be the second byte, c be the third byte, and
+ * d be the fourth byte. The value returned is: <p>
+ * (((a & 0xff) << 24) | ((b & 0xff) << 16) |
+ * ((c & 0xff) << 8) | (d & 0xff))
+ * @returns the int value read
+ * @throws IOException
+ * @throws EOFException
+ */
+ virtual int readInt() throw ( io::IOException, io::EOFException );
+
+ /**
+ * Reads eight input bytes and returns a long value. Let a be the
+ * first byte read, b be the second byte, c be the third byte, d
+ * be the fourth byte, e be the fifth byte, f be the sixth byte,
+ * g be the seventh byte, and h be the eighth byte. The value
+ * returned is:
+ * (((long)(a & 0xff) << 56) |
+ * ((long)(b & 0xff) << 48) |
+ * ((long)(c & 0xff) << 40) |
+ * ((long)(d & 0xff) << 32) |
+ * ((long)(e & 0xff) << 24) |
+ * ((long)(f & 0xff) << 16) |
+ * ((long)(g & 0xff) << 8) |
+ * ((long)(h & 0xff)))
+ * @returns the 64 bit long long read
+ * @throws IOException
+ * @throws EOFException
+ */
+ virtual long long readLong()
+ throw ( io::IOException, io::EOFException );
+
+ /**
+ * Reads two input bytes and returns a short value. Let a be the
+ * first byte read and b be the second byte. The value returned is:
+ * (short)((a << 8) | (b & 0xff))
+ * @returns the 16 bit short value read
+ * @throws IOException
+ * @throws EOFException
+ */
+ virtual short readShort() throw ( io::IOException, io::EOFException );
+
+ /**
+ * Reads two input bytes and returns an int value in the range 0
+ * through 65535. Let a be the first byte read and b be the
+ * second byte. The value returned is:
+ * (((a & 0xff) << 8) | (b & 0xff))
+ * @returns the 16 bit unsigned short read
+ * @throws IOException
+ * @throws EOFException
+ */
+ virtual unsigned short readUnsignedShort()
+ throw ( io::IOException, io::EOFException );
+
+ /**
+ * Reads an null terminated ASCII string to the stream and returns the
+ * string to the caller.
+ * @returns string object containing the string read.
+ * @throws IOException
+ * @throws EOFException
+ */
+ virtual std::string readString()
+ throw ( io::IOException, io::EOFException );
+
+ /**
+ * Reads a UTF8 encoded string in ASCII format and returns it, this is
+ * only useful if you know for sure that the string that is to be read
+ * was a string that contained all ascii values, and not uncide chars.
+ * @returns string read from stream.
+ * @throws IOException
+ * @throws EOFException
+ */
+ virtual std::string readUTF()
+ throw ( io::IOException, io::EOFException );
+
+ /**
+ * Reads some bytes from an input stream and stores them into the
+ * buffer array buffer. The number of bytes read is equal to the length
+ * of buffer.<p>
+ * This method blocks until one of the following conditions occurs:
+ * * buffer.size() bytes of input data are available, in which case
+ * a normal return is made.
+ * * End of file is detected, in which case an EOFException is
+ * thrown.
+ * * An I/O error occurs, in which case an IOException other than
+ * EOFException is thrown.
+ * <p>
+ * If buffer.size() is zero, then no bytes are read. Otherwise, the
+ * first byte read is stored into element b[0], the next one into
+ * buffer[1], and so on. If an exception is thrown from this method,
+ * then it may be that some but not all bytes of buffer have been
+ * updated with data from the input stream.
+ * @param buffer - vector of char that is read to its size()
+ * @throws IOException
+ * @throws EOFException
+ */
+ virtual void readFully( std::vector< unsigned char >& buffer )
+ throw ( io::IOException, io::EOFException );
+
+ /**
+ * Reads length bytes from an input stream.
+ * <p>
+ * This method blocks until one of the following conditions occurs:
+ * * length bytes of input data are available, in which case a
+ * normal return is made.
+ * * End of file is detected, in which case an EOFException is
+ * thrown.
+ * * An I/O error occurs, in which case an IOException other
+ * than EOFException is thrown.
+ * <p>
+ * If buffer is null, a NullPointerException is thrown. If offset is
+ * negative, or len is negative, or offset+length is greater than the
+ * length of the array buffer, then an IndexOutOfBoundsException is
+ * thrown. If len is zero, then no bytes are read. Otherwise, the
+ * first byte read is stored into element buffer[off], the next one into
+ * buffer[offset+1], and so on. The number of bytes read is, at most,
+ * equal to len.
+ * @param buffer - byte array to insert read data into
+ * @param offset - location in buffer to start writing
+ * @param length - number of bytes to read
+ * @throws IOException
+ * @throws EOFException
+ */
+ virtual void readFully( unsigned char* buffer,
+ std::size_t offset,
+ std::size_t length )
+ throw ( io::IOException,
+ io::EOFException,
+ exceptions::IndexOutOfBoundsException,
+ exceptions::NullPointerException );
+
+ /**
+ * Makes an attempt to skip over n bytes of data from the input
+ * stream, discarding the skipped bytes. However, it may skip over
+ * some smaller number of bytes, possibly zero. This may result from
+ * any of a number of conditions; reaching end of file before n
+ * bytes have been skipped is only one possibility. This method
+ * never throws an EOFException. The actual number of bytes skipped
+ * is returned.
+ * @param num - number of bytes to skip
+ * @return the total number of bytes skipped
+ */
+ virtual std::size_t skip( std::size_t num )
+ throw( io::IOException,
+ exceptions::UnsupportedOperationException );
+
+ };
+
+}}
+
+#endif /*_DECAF_IO_DATAINPUTSTREAM_H_*/