Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataOutputStream.cpp URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataOutputStream.cpp?view=auto&rev=531957 ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataOutputStream.cpp (added) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataOutputStream.cpp Tue Apr 24 07:33:45 2007 @@ -0,0 +1,221 @@ +/* + * 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 "DataOutputStream.h" +#include <decaf/util/Endian.h> +#include <decaf/util/Config.h> + +using namespace decaf; +using namespace decaf::io; +using namespace decaf::util; + +//////////////////////////////////////////////////////////////////////////////// +DataOutputStream::DataOutputStream( OutputStream* outputStream, bool own ) + : FilterOutputStream( outputStream, own ) +{ + // Init the written count + written = 0; +} + +//////////////////////////////////////////////////////////////////////////////// +DataOutputStream::~DataOutputStream() +{ +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::write( const unsigned char c ) throw ( IOException ) { + try { + outputStream->write( c ); + written++; + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::write( const std::vector<unsigned char>& buffer ) + throw ( IOException ) { + + try { + + if( buffer.size() == 0 ){ + // nothing to write. + return; + } + + outputStream->write( &buffer[0], buffer.size() ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::write( const unsigned char* buffer, std::size_t len ) + throw ( IOException ) { + + try { + outputStream->write( buffer, len ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::write( const unsigned char* buffer, + std::size_t offset, + std::size_t len ) throw ( IOException ) +{ + + try { + outputStream->write( buffer+offset, len ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::writeBoolean( bool value ) throw ( IOException ) { + try { + unsigned char ivalue = 0; + value == true ? ivalue = 1 : ivalue = 0; + + this->write( ivalue ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::writeByte( unsigned char value ) throw ( IOException ) { + try { + this->write( value ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::writeShort( short value ) throw ( IOException ) { + try { + this->writeUnsignedShort( (unsigned short)value ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::writeUnsignedShort( unsigned short value ) + throw ( IOException ) +{ + try { + write( (unsigned char)( (value & 0xFF00) >> 8 ) ); + write( (unsigned char)( (value & 0x00FF) >> 0 ) ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::writeChar( char value ) throw ( IOException ) { + try { + write( value ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::writeInt( int value ) throw ( IOException ) { + try { + this->write( (unsigned char)( (value & 0xFF000000) >> 24 ) ); + this->write( (unsigned char)( (value & 0x00FF0000) >> 16 ) ); + this->write( (unsigned char)( (value & 0x0000FF00) >> 8 ) ); + this->write( (unsigned char)( (value & 0x000000FF) >> 0 ) ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::writeLong( long long value ) throw ( IOException ) { + try { + this->write( (unsigned char)( (value & 0xFF00000000000000ULL) >> 56 ) ); + this->write( (unsigned char)( (value & 0x00FF000000000000ULL) >> 48 ) ); + this->write( (unsigned char)( (value & 0x0000FF0000000000ULL) >> 40 ) ); + this->write( (unsigned char)( (value & 0x000000FF00000000ULL) >> 32 ) ); + this->write( (unsigned char)( (value & 0x00000000FF000000ULL) >> 24 ) ); + this->write( (unsigned char)( (value & 0x0000000000FF0000ULL) >> 16 ) ); + this->write( (unsigned char)( (value & 0x000000000000FF00ULL) >> 8 ) ); + this->write( (unsigned char)( (value & 0x00000000000000FFULL) >> 0 ) ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::writeFloat( float value ) throw ( IOException ) { + try { + unsigned int lvalue = 0; + memcpy( &lvalue, &value, sizeof( float ) ); + this->writeInt( lvalue ); + memcpy( &value, &lvalue, sizeof( unsigned int ) ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::writeDouble( double value ) throw ( IOException ) { + try { + unsigned long long lvalue = 0; + memcpy( &lvalue, &value, sizeof( double ) ); + this->writeLong( lvalue ); + memcpy( &value, &lvalue, sizeof( unsigned long long ) ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::writeBytes( const std::string& value ) throw ( IOException ) { + try { + // do not add one so that we don't write the NULL + this->write( (const unsigned char*)value.c_str(), value.length() ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::writeChars( const std::string& value ) throw ( IOException ) { + try { + // add one so that we write the NULL + this->write( (const unsigned char*)value.c_str(), value.length() + 1 ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +} + +//////////////////////////////////////////////////////////////////////////////// +void DataOutputStream::writeUTF( const std::string& value ) throw ( IOException ) { + try { + this->writeUnsignedShort( (unsigned short)value.length() ); + this->write( (const unsigned char*)value.c_str(), value.length() ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) +}
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataOutputStream.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataOutputStream.h?view=auto&rev=531957 ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataOutputStream.h (added) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/DataOutputStream.h Tue Apr 24 07:33:45 2007 @@ -0,0 +1,218 @@ +/* + * 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_DATAOUTPUTSTREAM_H_ +#define _DECAF_IO_DATAOUTPUTSTREAM_H_ + +#include <decaf/io/FilterOutputStream.h> + +namespace decaf{ +namespace io{ + + /** + * A data output stream lets an application write primitive Java data + * types to an output stream in a portable way. An application can then + * use a data input stream to read the data back in. + */ + class DataOutputStream : public FilterOutputStream + { + protected: + + // The number of bytes written to the data output stream so far. + std::size_t written; + + public: + + /** + * Creates a new data output stream to write data to the specified + * underlying output stream. + * @param outputStream a stream to wrap with this one. + * @param own true if this objects owns the stream that it wraps. + */ + DataOutputStream( OutputStream* outputStream, bool own = false ); + + virtual ~DataOutputStream(); + + /** + * Returns the current value of the counter written, the number of + * bytes written to this data output stream so far. If the counter + * overflows, it will be wrapped to Integer.MAX_VALUE. + * @return the value of the written field. + */ + virtual std::size_t size() const { + return written; + } + + /** + * Writes a single byte to the output stream. If no exception is + * thrown, the counter written is incremented by 1. + * @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. the counter + * written is incremented by len. + * @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 ); + + /** + * Writes an array of bytes to the output stream. the counter + * written is incremented by len. + * @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 offset, + std::size_t len ) throw ( IOException ); + + /** + * Writes an array of bytes to the output stream. + * @param buffer The bytes to write. + * @throws IOException thrown if an error occurs. + */ + virtual void write( const std::vector<unsigned char>& buffer ) + throw ( IOException ); + + /** + * Writes a boolean to the underlying output stream as a 1-byte value. The + * value true is written out as the value (byte)1; the value false + * is written out as the value (byte)0. If no exception is thrown, + * the counter written is incremented by 1. + * @param value the boolean to write. + * @throws IOException + * + */ + virtual void writeBoolean( bool value ) throw ( IOException ); + + /** + * Writes out a byte to the underlying output stream as a 1-byte + * value. If no exception is thrown, the counter written is + * incremented by 1. + * @param value the unsigned char value to write. + * @throws IOException + */ + virtual void writeByte( unsigned char value ) throw ( IOException ); + + /** + * Writes a short to the underlying output stream as two bytes, high + * byte first. If no exception is thrown, the counter written is + * incremented by 2. + * @param value the value to write. + * @throws IOException + */ + virtual void writeShort( short value ) throw ( IOException ); + + /** + * Writes a unsigned short to the bytes message stream as a 2 byte value + * @param value - unsigned short to write to the stream + * @throws IOException + */ + virtual void writeUnsignedShort( unsigned short value ) throw ( IOException ); + + /** + * Writes out a char to the underlying output stream as a one byte + * value If no exception is thrown, the counter written is + * incremented by 1. + * @param value the value to write. + * @throws IOException + */ + virtual void writeChar( char value ) throw ( IOException ); + + /** + * Writes an int to the underlying output stream as four bytes, high + * byte first. If no exception is thrown, the counter written is + * incremented by 4. + * @param value the value to write. + * @throws IOException + */ + virtual void writeInt( int value ) throw ( IOException ); + + /** + * Writes an 64 bit long to the underlying output stream as eight + * bytes, high byte first. If no exception is thrown, the counter + * written is incremented by 8. + * @param value the value to write. + * @throws IOException + */ + virtual void writeLong( long long value ) throw ( IOException ); + + /** + * Converts the float argument to an int using the floatToIntBits + * method in class Float, and then writes that int value to the + * underlying output stream as a 4-byte quantity, high byte first. + * If no exception is thrown, the counter written is incremented + * by 4. + * @param value the value to write. + * @throws IOException + */ + virtual void writeFloat( float value ) throw ( IOException ); + + /** + * Converts the double argument to a long using the doubleToLongBits + * method in class Double, and then writes that long value to the + * underlying output stream as an 8-byte quantity, high byte first. + * If no exception is thrown, the counter written is incremented + * by 8. + * @param value the value to write. + * @throws IOException + */ + virtual void writeDouble( double value ) throw ( IOException ); + + /** + * Writes out the string to the underlying output stream as a + * sequence of bytes. Each character in the string is written out, + * in sequence, by discarding its high eight bits. If no exception + * is thrown, the counter written is incremented by the length of + * value. The value written does not include a trailing null as that + * is not part of the sequence of bytes, if the null is needed, then use + * the writeChars method. + * @param value the value to write. + * @throws IOException + */ + virtual void writeBytes( const std::string& value ) throw ( IOException ); + + /** + * Writes a string to the underlying output stream as a sequence of + * characters. Each character is written to the data output stream + * as if by the writeChar method. If no exception is thrown, the + * counter written is incremented by the length of value. The trailing + * NULL charactor is written by this method. + * @param value the value to write. + * @throws IOException + */ + virtual void writeChars( const std::string& value ) throw ( IOException ); + + /** + * Writes out the string to the underlying output stream as a + * unsigned short indicating its length followed by the rest of + * the string. + * @param value the value to write. + * @throws IOException + */ + virtual void writeUTF( const std::string& value ) throw ( io::IOException ); + }; + +}} + +#endif /*_DECAF_IO_DATAOUTPUTSTREAM_H_*/ Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/EOFException.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/EOFException.h?view=auto&rev=531957 ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/EOFException.h (added) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/EOFException.h Tue Apr 24 07:33:45 2007 @@ -0,0 +1,91 @@ +/* + * 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_EOFEXCEPTION_H +#define _DECAF_IO_EOFEXCEPTION_H + +#include <decaf/io/IOException.h> + +namespace decaf{ +namespace io{ + + /* + * Signals that an End of File exception has occurred. + */ + class EOFException : public io::IOException + { + public: + + /** + * Default Constructor + */ + EOFException() throw(){} + + /** + * Copy Constructor + * @param ex the exception to copy + */ + EOFException( const exceptions::Exception& ex ) throw() + : IOException() + { + *(exceptions::ActiveMQException*)this = ex; + } + + /** + * Copy Constructor + * @param ex the exception to copy, which is an instance of this type + */ + EOFException( const EOFException& ex ) throw() + : IOException() + { + *(exceptions::Exception*)this = ex; + } + + /** + * Consturctor + * @param file name of the file were the exception occured. + * @param lineNumber line where the exception occured + * @param msg the message that was generated + */ + EOFException( const char* file, const int lineNumber, + const char* msg, ... ) throw() + : IOException() + { + va_list vargs; + va_start( vargs, msg ); + buildMessage( msg, vargs ); + + // Set the first mark for this exception. + setMark( file, lineNumber ); + } + + /** + * Clones this exception. This is useful for cases where you need + * to preserve the type of the original exception as well as the message. + * All subclasses should override. + */ + virtual exceptions::Exception* clone() const{ + return new EOFException( *this ); + } + + virtual ~EOFException() throw(){} + + }; + +}} + +#endif /*_DECAF*/ Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterInputStream.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterInputStream.h?view=auto&rev=531957 ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterInputStream.h (added) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterInputStream.h Tue Apr 24 07:33:45 2007 @@ -0,0 +1,225 @@ +/* + * 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_FILTERINPUTSTREAM_H_ +#define _DECAF_IO_FILTERINPUTSTREAM_H_ + +#include <decaf/io/InputStream.h> +#include <decaf/io/IOException.h> +#include <activemq/concurrent/Mutex.h> + +namespace decaf{ +namespace io{ + + /** + * A FilterInputStream contains some other input stream, which it uses + * as its basic source of data, possibly transforming the data along the + * way or providing additional functionality. The class FilterInputStream + * itself simply overrides all methods of InputStream with versions + * that pass all requests to the contained input stream. Subclasses of + * FilterInputStream may further override some of these methods and may + * also provide additional methods and fields. + */ + class FilterInputStream : public InputStream + { + protected: + + // The input stream to wrap + InputStream* inputStream; + + // Synchronization object. + concurrent::Mutex mutex; + + // Indicates if we own the wrapped stream + bool own; + + public: + + /** + * Constructor to create a wrapped InputStream + * @param inputStream the stream to wrap and filter + * @param own indicates if we own the stream object, defaults to false + */ + FilterInputStream( InputStream* inputStream, bool own = false ) { + this->inputStream = inputStream; + this->own = own; + } + + virtual ~FilterInputStream() { + try { + if( own == true ) delete inputStream; + } + AMQ_CATCH_NOTHROW( IOException ) + AMQ_CATCHALL_NOTHROW( ) + } + + /** + * Returns the number of bytes that can be read from this input stream + * without blocking. This method simply performs in.available() and + * returns the result. + * @return the number of bytes available without blocking. + */ + virtual std::size_t available() const throw ( IOException ) { + try { + return inputStream->available(); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) + } + + /** + * Reads the next byte of data from this input stream. The value byte + * is returned as an unsigned char in the range 0 to 255. If no byte is + * available because the end of the stream has been reached, the value + * -1 is returned. This method blocks until input data is available, + * the end of the stream is detected, or an exception is thrown. + * This method simply performs in.read() and returns the result. + * @return The next byte. + * @throws IOException thrown if an error occurs. + */ + virtual unsigned char read() throw ( IOException ) { + try { + return inputStream->read(); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) + } + + /** + * Reads up to len bytes of data from this input stream into an array + * of bytes. This method blocks until some input is available. + * This method simply performs in.read(b, len) and returns the result. + * @param buffer (out) the target buffer. + * @param bufferSize the size of the output buffer. + * @return The number of bytes read or -1 if EOS is detected + * @throws IOException thrown if an error occurs. + */ + virtual std::size_t read( unsigned char* buffer, std::size_t bufferSize ) + throw ( IOException ) + { + try { + return inputStream->read( buffer, bufferSize ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) + } + + /** + * Close the Stream, the FilterOutputStream simply calls the close + * method of the underlying stream + * @throws CMSException + */ + virtual void close() throw ( cms::CMSException ) { + try { + inputStream->close(); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_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 ) { + try { + return inputStream->skip(num); + } + AMQ_CATCH_RETHROW( exceptions::UnsupportedOperationException ) + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) + } + + public: // Synchronizable + + /** + * 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 Exception + */ + virtual void notifyAll() throw( exceptions::Exception ){ + mutex.notifyAll(); + } + + }; + +}} + +#endif /*_DECAF_IO_FILTERINPUTSTREAM_H_*/ Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterOutputStream.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterOutputStream.h?view=auto&rev=531957 ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterOutputStream.h (added) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/FilterOutputStream.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_FILTEROUTPUTSTREAM_H_ +#define _DECAF_IO_FILTEROUTPUTSTREAM_H_ + +#include <decaf/io/OutputStream.h> +#include <decaf/io/IOException.h> +#include <activemq/concurrent/Mutex.h> + +namespace decaf{ +namespace io{ + + /** + * This class is the superclass of all classes that filter output + * streams. These streams sit on top of an already existing output + * stream (the underlying output stream) which it uses as its basic + * sink of data, but possibly transforming the data along the way or + * providing additional functionality. + * + * The class FilterOutputStream itself simply overrides all methods of + * OutputStream with versions that pass all requests to the underlying + * output stream. Subclasses of FilterOutputStream may further override + * some of these methods as well as provide additional methods and + * fields. + * + * 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: + * + * DataOutputStream os = new DataOutputStream( new OutputStream(), true ) + */ + class FilterOutputStream : public OutputStream + { + protected: + + // The output Stream to wrap + OutputStream* outputStream; + + // Synchronization object. + concurrent::Mutex mutex; + + // Indicates if we own the wrapped stream + bool own; + + public: + + /** + * Constructor, creates a wrapped output stream + * @param outputStream the OutputStream to wrap + * @param own If true, this object will control the lifetime of the + * output stream that it encapsulates. + */ + FilterOutputStream( OutputStream* outputStream, bool own = false ){ + this->outputStream = outputStream; + this->own = own; + } + + virtual ~FilterOutputStream() { + try { + if( own == true ) delete outputStream; + } + AMQ_CATCH_NOTHROW( IOException ) + AMQ_CATCHALL_NOTHROW( ) + } + + /** + * Writes a single byte to the output stream. The write method of + * FilterOutputStream calls the write method of its underlying output + * stream, that is, it performs out.write(b). + * @param c the byte. + * @throws IOException thrown if an error occurs. + */ + virtual void write( unsigned char c ) throw ( IOException ) { + try { + outputStream->write( c ); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) + } + + /** + * Writes an array of bytes to the output stream. The write method of + * FilterOutputStream calls the write method of one argument on each + * byte to output. + * @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 ) { + try { + for( std::size_t ix = 0; ix < len; ++ix ) + { + outputStream->write( buffer[ix] ); + } + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) + } + + /** + * Flushes any pending writes in this output stream. + * The flush method of FilterOutputStream calls the flush method + * of its underlying output stream + * @throws IOException + */ + virtual void flush() throw ( IOException ) { + try { + outputStream->flush(); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) + } + + /** + * Close the Stream, the FilterOutputStream simply calls the close + * method of the underlying stream + * @throws CMSException + */ + virtual void close() throw ( cms::CMSException ) { + try { + outputStream->close(); + } + AMQ_CATCH_RETHROW( IOException ) + AMQ_CATCHALL_THROW( IOException ) + } + + public: // Synchronizable + + /** + * 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(); + } + + }; + +}} + +#endif /*_DECAF_IO_FILTEROUTPUTSTREAM_H_*/ Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/IOException.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/IOException.h?view=auto&rev=531957 ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/IOException.h (added) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/IOException.h Tue Apr 24 07:33:45 2007 @@ -0,0 +1,90 @@ +/* + * 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_IOEXCEPTION_H +#define _DECAF_IO_IOEXCEPTION_H + +#include <decaf/lang/Exception.h> + +namespace decaf{ +namespace io{ + + /* + * Signals that an I/O exception of some sort has occurred. + */ + class IOException : public lang::Exception + { + public: + + /** + * Default Constructor + */ + IOException() throw() {} + + /** + * Copy Constructor + * @param ex the exception to copy + */ + IOException( const exceptions::Exception& ex ) throw() + : lang::Exception() + { + *(lang::Exception*)this = ex; + } + + /** + * Copy Constructor + * @param ex the exception to copy, which is an instance of this type + */ + IOException( const IOException& ex ) throw() + : lang::Exception() + { + *(lang::Exception*)this = ex; + } + + /** + * Consturctor + * @param file name of the file were the exception occured. + * @param lineNumber line where the exception occured + * @param msg the message that was generated + */ + IOException( const char* file, const int lineNumber, + const char* msg, ... ) throw() + : lang::Exception() + { + va_list vargs; + va_start( vargs, msg ); + buildMessage( msg, vargs ); + + // Set the first mark for this exception. + setMark( file, lineNumber ); + } + + /** + * Clones this exception. This is useful for cases where you need + * to preserve the type of the original exception as well as the message. + * All subclasses should override. + */ + virtual lang::IOException* clone() const{ + return new IOException( *this ); + } + + virtual ~IOException() throw() {} + + }; + +}} + +#endif /*_DECAF_IO_IOEXCEPTION_H*/ Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/InputStream.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/InputStream.h?view=auto&rev=531957 ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/InputStream.h (added) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/InputStream.h Tue Apr 24 07:33:45 2007 @@ -0,0 +1,90 @@ +/* + * 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_INPUTSTREAM_H_ +#define _DECAF_IO_INPUTSTREAM_H_ + +#include <decaf/io/IOException.h> +#include <cms/Closeable.h> +#include <activemq/concurrent/Synchronizable.h> +#include <activemq/exceptions/UnsupportedOperationException.h> + +namespace decaf{ +namespace io{ + + /** + * Base interface for an input stream. + */ + class InputStream + : + public cms::Closeable, + public concurrent::Synchronizable + { + + public: + + virtual ~InputStream(){} + + /** + * Indcates the number of bytes avaialable. + * @return the number of bytes available on this input stream. + * @throws IOException if an error occurs. + */ + virtual std::size_t available() const throw ( IOException ) = 0; + + /** + * Reads a single byte from the buffer. Blocks until + * data is available. + * @return The next byte. + * @throws IOException thrown if an error occurs. + */ + virtual unsigned char read() throw ( IOException ) = 0; + + /** + * 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 or -1 if EOF is detected + * @throws IOException thrown if an error occurs. + */ + virtual std::size_t read( unsigned char* buffer, std::size_t bufferSize ) + throw ( IOException ) = 0; + + /** + * 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 ) = 0; + + }; + +}} + +#endif /*_DECAF_IO_INPUTSTREAM_H_*/ Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/OutputStream.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/OutputStream.h?view=auto&rev=531957 ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/OutputStream.h (added) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/OutputStream.h Tue Apr 24 07:33:45 2007 @@ -0,0 +1,65 @@ +/* + * 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_OUTPUTSTREAM_H +#define _DECAF_IO_OUTPUTSTREAM_H + +#include <cms/Closeable.h> +#include <decaf/io/IOException.h> +#include <activemq/concurrent/Synchronizable.h> + +namespace decaf{ +namespace io{ + + /** + * Base interface for an output stream. + */ + class OutputStream + : + public cms::Closeable, + public concurrent::Synchronizable + { + public: + + virtual ~OutputStream(){} + + /** + * 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 ) = 0; + + /** + * 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 ) = 0; + + /** + * Flushes any pending writes in this output stream. + * @throws IOException + */ + virtual void flush() throw ( IOException ) = 0; + }; + +}} + +#endif /*_DECAF_IO_OUTPUTSTREAM_H*/ Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/Reader.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/Reader.h?view=auto&rev=531957 ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/Reader.h (added) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/Reader.h 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. + */ +#ifndef _DECAF_IO_READER_H +#define _DECAF_IO_READER_H + +#include <string> +#include <decaf/io/IOException.h> +#include <decaf/io/InputStream.h> + +namespace decaf{ +namespace io{ + + /* + * Reader interface that wraps around an input stream and provides + * an interface for extracting the data from the input stream. + */ + class Reader + { + public: + + virtual ~Reader(){}; + + /** + * Sets the target input stream. + */ + virtual void setInputStream( InputStream* is ) = 0; + + /** + * Gets the target input stream. + */ + virtual InputStream* getInputStream() = 0; + + /** + * Attempts to read an array of bytes from the stream. + * @param buffer The target byte buffer. + * @param count The number of bytes to read. + * @return The number of bytes read. + * @throws IOException thrown if an error occurs. + */ + virtual std::size_t read( unsigned char* buffer, std::size_t count ) + throw( IOException ) = 0; + + /** + * Attempts to read a byte from the input stream + * @return The byte. + * @throws IOException thrown if an error occurs. + */ + virtual unsigned char readByte() throw( IOException ) = 0; + + }; + +}} + +#endif /*_DECAF_IO_READER_H*/ Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/StandardErrorOutputStream.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/StandardErrorOutputStream.h?view=auto&rev=531957 ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/StandardErrorOutputStream.h (added) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/StandardErrorOutputStream.h Tue Apr 24 07:33:45 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. + */ +#ifndef _DECAF_IO_STANDARDERROROUTPUTSTREAM_H_ +#define _DECAF_IO_STANDARDERROROUTPUTSTREAM_H_ + +#include <decaf/io/OutputStream.h> +#include <activemq/concurrent/Mutex.h> + +#include <iostream> + +namespace decaf{ +namespace io{ + + class StandardErrorOutputStream : public OutputStream + { + private: + + /** + * Synchronization object. + */ + concurrent::Mutex mutex; + + public: + + /** + * Default Constructor + */ + StandardErrorOutputStream(void) {} + + virtual ~StandardErrorOutputStream(void) {} + + /** + * 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 ) + { + std::cerr << c; + } + + /** + * 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, int len ) + throw ( IOException ) + { + for(int i = 0; i < len; ++i) + { + std::cerr << buffer[i]; + } + } + + /** + * Invokes flush on the target output stream. + * throws IOException if an error occurs + */ + virtual void flush() throw ( IOException ){ + std::cerr.flush(); + } + + /** + * Invokes close on the target output stream. + * throws CMSException if an error occurs + */ + void close() throw( cms::CMSException ){ + std::cerr.flush(); + } + + }; + +} + +#endif /*_DECAF_IO_STANDARDERROROUTPUTSTREAM_H_*/ Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/Writer.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/Writer.h?view=auto&rev=531957 ============================================================================== --- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/Writer.h (added) +++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/io/Writer.h 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. + */ +#ifndef _DECAF_IO_WRITER_H +#define _DECAF_IO_WRITER_H + +#include <string> +#include <decaf/io/IOException.h> +#include <decaf/io/OutputStream.h> + +namespace decaf{ +namespace io{ + + /* + * Writer interface for an object that wraps around an output + * stream + */ + class Writer + { + public: + + virtual ~Writer(){}; + + /** + * Sets the target output stream. + * @param Outputstream to use + */ + virtual void setOutputStream( OutputStream* os ) = 0; + + /** + * Gets the target output stream. + * @returns the output stream currently being used + */ + virtual OutputStream* getOutputStream() = 0; + + /** + * Writes a byte array to the output stream. + * @param buffer a byte array + * @param count the number of bytes in the array to write. + * @throws IOException thrown if an error occurs. + */ + virtual void write( const unsigned char* buffer, + std::size_t count ) throw( IOException ) = 0; + + /** + * Writes a byte to the output stream. + * @param v The value to be written. + * @throws IOException thrown if an error occurs. + */ + virtual void writeByte( unsigned char v ) throw( IOException ) = 0; + }; + +}} + +#endif /*_DECAF_IO_WRITER_H*/
