diff options
author | Ocke Janssen <oj@openoffice.org> | 2001-02-02 13:56:32 +0000 |
---|---|---|
committer | Ocke Janssen <oj@openoffice.org> | 2001-02-02 13:56:32 +0000 |
commit | fd46c9937e1467bea77e89a9519f90ff31ad2cf6 (patch) | |
tree | f230946a61a355485329bee05189ec4237ff10bb /comphelper | |
parent | 38dfa186856ccab2085fb60eb7de4cc035f7484a (diff) |
new class for XOuputStream
Diffstat (limited to 'comphelper')
-rw-r--r-- | comphelper/inc/comphelper/seqstream.hxx | 89 | ||||
-rw-r--r-- | comphelper/source/streaming/seqstream.cxx | 128 |
2 files changed, 183 insertions, 34 deletions
diff --git a/comphelper/inc/comphelper/seqstream.hxx b/comphelper/inc/comphelper/seqstream.hxx index c389ca83c2b0..6016c275e1df 100644 --- a/comphelper/inc/comphelper/seqstream.hxx +++ b/comphelper/inc/comphelper/seqstream.hxx @@ -2,9 +2,9 @@ * * $RCSfile: seqstream.hxx,v $ * - * $Revision: 1.1.1.1 $ + * $Revision: 1.2 $ * - * last change: $Author: fs $ $Date: 2000-09-29 11:28:15 $ + * last change: $Author: oj $ $Date: 2001-02-02 14:56:08 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -70,7 +70,9 @@ #ifndef _COM_SUN_STAR_IO_XINPUTSTREAM_HPP_ #include <com/sun/star/io/XInputStream.hpp> #endif - +#ifndef _COM_SUN_STAR_IO_XOUTPUTSTREAM_HPP_ +#include <com/sun/star/io/XOutputStream.hpp> +#endif #ifndef _OSL_MUTEX_HXX_ #include <osl/mutex.hxx> #endif @@ -82,10 +84,7 @@ namespace comphelper { - namespace staruno = ::com::sun::star::uno; - namespace stario = ::com::sun::star::io; - - typedef staruno::Sequence<sal_Int8> ByteSequence; + typedef ::com::sun::star::uno::Sequence<sal_Int8> ByteSequence; //================================================================== // SequenceInputStream @@ -94,7 +93,7 @@ namespace comphelper class SequenceInputStream -: public ::cppu::WeakImplHelper1< stario::XInputStream > +: public ::cppu::WeakImplHelper1< ::com::sun::star::io::XInputStream > { ::osl::Mutex m_aMutex; ByteSequence m_aData; @@ -104,27 +103,81 @@ public: SequenceInputStream(const ByteSequence& rData); // com::sun::star::io::XInputStream - virtual sal_Int32 SAL_CALL readBytes( staruno::Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead ) - throw(stario::NotConnectedException, stario::BufferSizeExceededException, - stario::IOException, staruno::RuntimeException); + virtual sal_Int32 SAL_CALL readBytes( ::com::sun::star::uno::Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead ) + throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, + ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); - virtual sal_Int32 SAL_CALL readSomeBytes( staruno::Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead ) - throw(stario::NotConnectedException, stario::BufferSizeExceededException, - stario::IOException, staruno::RuntimeException); + virtual sal_Int32 SAL_CALL readSomeBytes( ::com::sun::star::uno::Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead ) + throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, + ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) - throw(stario::NotConnectedException, stario::BufferSizeExceededException, - stario::IOException, staruno::RuntimeException); + throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, + ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); virtual sal_Int32 SAL_CALL available( ) - throw(stario::NotConnectedException, stario::IOException, staruno::RuntimeException); + throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); virtual void SAL_CALL closeInput( ) - throw(stario::NotConnectedException, stario::IOException, staruno::RuntimeException); + throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); private: inline sal_Int32 avail(); }; +typedef ::cppu::WeakImplHelper1< ::com::sun::star::io::XOutputStream > OSequenceOutputStream_Base; + +class OSequenceOutputStream : public OSequenceOutputStream_Base +{ +protected: + ::com::sun::star::uno::Sequence< sal_Int8 >& m_rSequence; + double m_nResizeFactor; + sal_Int32 m_nMinimumResize; + sal_Int32 m_nMaximumResize; + sal_Int32 m_nSize; + // the size of the virtual stream. This is not the size of the sequence, but the number of bytes written + // into the stream at a given moment. + + sal_Bool m_bConnected; + // closeOutput has been called ? + + ::osl::Mutex m_aMutex; + +protected: + ~OSequenceOutputStream() { if (m_bConnected) closeOutput(); } + +public: + /** constructs the object. Everything written into the stream through the XOutputStream methods will be forwarded + to the sequence, reallocating it if neccessary. Writing will start at offset 0 within the sequence. + @param _rSeq a reference to the sequence which will be used for output. + The caller is responsible for taking care of the lifetime of the stream + object and the sequence. If you're in doubt about this, use <code>closeOutput</code> + before destroying the sequence + @param _nResizeFactor the factor which is used for resizing the sequence when neccessary. In every + resize step, the new sequence size will be calculated by multiplying the current + size with this factor, rounded off to the next multiple of 4. + @param _nMinimumResize the minmal number of bytes which is additionally allocated on resizing + @param _nMaximumResize as the growth of the stream size is exponential, you may want to specify a + maxmimum amount of memory which the sequence will grow by. If -1 is used, + no limit is applied + @see closeOutput + */ + OSequenceOutputStream( + ::com::sun::star::uno::Sequence< sal_Int8 >& _rSeq, + double _nResizeFactor = 1.3, + sal_Int32 _nMinimumResize = 128, + sal_Int32 _nMaximumResize = -1 + ); + + /// same as XOutputStream::writeBytes (as expected :) + virtual void SAL_CALL writeBytes( const ::com::sun::star::uno::Sequence< sal_Int8 >& aData ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); + /// this is a dummy in this implementation, no buffering is used + virtual void SAL_CALL flush( ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); + /** closes the output stream. In the case of this class, this means that the sequence used for writing is + resized to the really used size and not used any further, every subsequent call to one of the XOutputStream + methods will throw a <code>NotConnectedException</code>. + */ + virtual void SAL_CALL closeOutput( ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); +}; } // namespace comphelper diff --git a/comphelper/source/streaming/seqstream.cxx b/comphelper/source/streaming/seqstream.cxx index 469a9a963468..3dd0f42c9fa4 100644 --- a/comphelper/source/streaming/seqstream.cxx +++ b/comphelper/source/streaming/seqstream.cxx @@ -2,9 +2,9 @@ * * $RCSfile: seqstream.cxx,v $ * - * $Revision: 1.1.1.1 $ + * $Revision: 1.2 $ * - * last change: $Author: fs $ $Date: 2000-09-29 11:28:15 $ + * last change: $Author: oj $ $Date: 2001-02-02 14:56:32 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -67,6 +67,9 @@ namespace comphelper { +using namespace ::com::sun::star::io; +using namespace ::com::sun::star::uno; +using namespace ::osl; //--------------------------------------------------------------------------------------------- // class SequenceInputStream @@ -84,23 +87,23 @@ SequenceInputStream::SequenceInputStream(const ByteSequence& rData) inline sal_Int32 SequenceInputStream::avail() { if (m_nPos == -1) - throw stario::NotConnectedException(::rtl::OUString(), *this); + throw NotConnectedException(::rtl::OUString(), *this); return m_aData.getLength() - m_nPos; } // com::sun::star::io::XInputStream //------------------------------------------------------------------ -sal_Int32 SAL_CALL SequenceInputStream::readBytes( staruno::Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead ) - throw(stario::NotConnectedException, stario::BufferSizeExceededException, - stario::IOException, staruno::RuntimeException) +sal_Int32 SAL_CALL SequenceInputStream::readBytes( Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead ) + throw(NotConnectedException, BufferSizeExceededException, + IOException, RuntimeException) { ::osl::MutexGuard aGuard( m_aMutex ); sal_Int32 nAvail = avail(); if (nBytesToRead < 0) - throw stario::BufferSizeExceededException(::rtl::OUString(),*this); + throw BufferSizeExceededException(::rtl::OUString(),*this); if (nAvail < nBytesToRead) nBytesToRead = nAvail; @@ -113,9 +116,9 @@ sal_Int32 SAL_CALL SequenceInputStream::readBytes( staruno::Sequence<sal_Int8>& } //------------------------------------------------------------------ -sal_Int32 SAL_CALL SequenceInputStream::readSomeBytes( staruno::Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead ) - throw(stario::NotConnectedException, stario::BufferSizeExceededException, - stario::IOException, staruno::RuntimeException) +sal_Int32 SAL_CALL SequenceInputStream::readSomeBytes( Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead ) + throw(NotConnectedException, BufferSizeExceededException, + IOException, RuntimeException) { // all data is available at once return readBytes(aData, nMaxBytesToRead); @@ -123,15 +126,15 @@ sal_Int32 SAL_CALL SequenceInputStream::readSomeBytes( staruno::Sequence<sal_Int //------------------------------------------------------------------ void SAL_CALL SequenceInputStream::skipBytes( sal_Int32 nBytesToSkip ) - throw(stario::NotConnectedException, stario::BufferSizeExceededException, - stario::IOException, staruno::RuntimeException) + throw(NotConnectedException, BufferSizeExceededException, + IOException, RuntimeException) { ::osl::MutexGuard aGuard( m_aMutex ); sal_Int32 nAvail = avail(); if (nBytesToSkip < 0) - throw stario::BufferSizeExceededException(::rtl::OUString(),*this); + throw BufferSizeExceededException(::rtl::OUString(),*this); if (nAvail < nBytesToSkip) nBytesToSkip = nAvail; @@ -141,7 +144,7 @@ void SAL_CALL SequenceInputStream::skipBytes( sal_Int32 nBytesToSkip ) //------------------------------------------------------------------ sal_Int32 SAL_CALL SequenceInputStream::available( ) - throw(stario::NotConnectedException, stario::IOException, staruno::RuntimeException) + throw(NotConnectedException, IOException, RuntimeException) { ::osl::MutexGuard aGuard( m_aMutex ); @@ -150,11 +153,104 @@ sal_Int32 SAL_CALL SequenceInputStream::available( ) //------------------------------------------------------------------ void SAL_CALL SequenceInputStream::closeInput( ) - throw(stario::NotConnectedException, stario::IOException, staruno::RuntimeException) + throw(NotConnectedException, IOException, RuntimeException) { if (m_nPos == -1) - throw stario::NotConnectedException(::rtl::OUString(), *this); + throw NotConnectedException(::rtl::OUString(), *this); m_nPos = -1; } +//-------------------------------------------------------------------------- +OSequenceOutputStream::OSequenceOutputStream(Sequence< sal_Int8 >& _rSeq, double _nResizeFactor, sal_Int32 _nMinimumResize, sal_Int32 _nMaximumResize) + :m_rSequence(_rSeq) + ,m_nResizeFactor(_nResizeFactor) + ,m_nMinimumResize(_nMinimumResize) + ,m_nMaximumResize(_nMaximumResize) + ,m_nSize(0) // starting at position 0 + ,m_bConnected(sal_True) +{ + OSL_ENSHURE(m_nResizeFactor > 1, "OSequenceOutputStream::OSequenceOutputStream : invalid resize factor !"); + OSL_ENSHURE((m_nMaximumResize < 0) || (m_nMaximumResize > m_nMinimumResize), + "OSequenceOutputStream::OSequenceOutputStream : these limits don't make any sense !"); + + if (m_nResizeFactor <= 1) + m_nResizeFactor = 1.3; + if ((m_nMaximumResize >= 0) && (m_nMaximumResize <= m_nMinimumResize)) + m_nMaximumResize = m_nMinimumResize * 2; + // this heuristic is as good as any other ... supply better parameters if you don't like it :) +} + +//-------------------------------------------------------------------------- +void SAL_CALL OSequenceOutputStream::writeBytes( const Sequence< sal_Int8 >& _rData ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) +{ + MutexGuard aGuard(m_aMutex); + if (!m_bConnected) + throw NotConnectedException(); + + // ensure the sequence has enoungh space left + if (m_nSize + _rData.getLength() < m_rSequence.getLength()) + { + sal_Int32 nCurrentLength = m_rSequence.getLength(); + sal_Int32 nNewLength = nCurrentLength * m_nResizeFactor; + + if (m_nMinimumResize > nNewLength - nCurrentLength) + // we have a minimum so it's not too inefficient for small sequences and small write requests + nNewLength = nCurrentLength + m_nMinimumResize; + + if ((m_nMaximumResize > 0) && (nNewLength - nCurrentLength > m_nMaximumResize)) + // such a large step is not allowed + nNewLength = nCurrentLength + m_nMaximumResize; + + if (nNewLength < m_nSize + _rData.getLength()) + { // it's not enough .... the data would not fit + + // let's take the double amount of the length of the data to be written, as the next write + // request could be as large as this one + sal_Int32 nNewGrowth = _rData.getLength() * 2; + if ((m_nMaximumResize > 0) && (nNewGrowth > m_nMaximumResize)) + { // we came to the limit, again ... + nNewGrowth = m_nMaximumResize; + if (nNewGrowth + nCurrentLength < m_nSize + _rData.getLength()) + // but it would not fit if we respect the limit + nNewGrowth = m_nSize + _rData.getLength() - nCurrentLength; + } + nNewLength = nCurrentLength + nNewGrowth; + } + + // round it off to the next multiple of 4 ... + nNewLength = (nNewLength + 3) / 4 * 4; + + m_rSequence.realloc(nNewLength); + } + + OSL_ENSHURE(m_rSequence.getLength() >= m_nSize + _rData.getLength(), + "ooops ... the realloc algorithm seems to be wrong :( !"); + + memcpy(m_rSequence.getArray() + m_nSize, _rData.getConstArray(), _rData.getLength()); + m_nSize += _rData.getLength(); +} + +//-------------------------------------------------------------------------- +void SAL_CALL OSequenceOutputStream::flush( ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) +{ + MutexGuard aGuard(m_aMutex); + if (!m_bConnected) + throw NotConnectedException(); + + // nothing to do here +} + +//-------------------------------------------------------------------------- +void SAL_CALL OSequenceOutputStream::closeOutput( ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) +{ + MutexGuard aGuard(m_aMutex); + if (!m_bConnected) + throw NotConnectedException(); + + // cut the sequence to the real size + m_rSequence.realloc(m_nSize); + // and don't allow any further accesses + m_bConnected = sal_False; +} + } // namespace comphelper |