diff options
-rw-r--r-- | comphelper/source/streaming/seqstream.cxx | 34 | ||||
-rw-r--r-- | include/comphelper/seqstream.hxx | 32 |
2 files changed, 32 insertions, 34 deletions
diff --git a/comphelper/source/streaming/seqstream.cxx b/comphelper/source/streaming/seqstream.cxx index 91fdc7d73378..5bc44a52e877 100644 --- a/comphelper/source/streaming/seqstream.cxx +++ b/comphelper/source/streaming/seqstream.cxx @@ -156,19 +156,15 @@ OSequenceOutputStream::OSequenceOutputStream(Sequence< sal_Int8 >& _rSeq, double // 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, std::exception) -{ - writeBytes(_rData.getConstArray(), _rData.getLength()); -} -void SAL_CALL OSequenceOutputStream::writeBytes( const sal_Int8* pStr, sal_Int32 nLen ) - throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) +void SAL_CALL OSequenceOutputStream::writeBytes( const Sequence< sal_Int8 >& _rData ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) { + MutexGuard aGuard(m_aMutex); if (!m_bConnected) throw NotConnectedException(); // ensure the sequence has enough space left - if (m_nSize + nLen > m_rSequence.getLength()) + if (m_nSize + _rData.getLength() > m_rSequence.getLength()) { sal_Int32 nCurrentLength = m_rSequence.getLength(); sal_Int32 nNewLength = static_cast< sal_Int32 >( @@ -182,18 +178,18 @@ void SAL_CALL OSequenceOutputStream::writeBytes( const sal_Int8* pStr, sal_Int32 // such a large step is not allowed nNewLength = nCurrentLength + m_nMaximumResize; - if (nNewLength < m_nSize + nLen) + 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 = nLen * 2; + 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 + nLen) + if (nNewGrowth + nCurrentLength < m_nSize + _rData.getLength()) // but it would not fit if we respect the limit - nNewGrowth = m_nSize + nLen - nCurrentLength; + nNewGrowth = m_nSize + _rData.getLength() - nCurrentLength; } nNewLength = nCurrentLength + nNewGrowth; } @@ -204,29 +200,33 @@ void SAL_CALL OSequenceOutputStream::writeBytes( const sal_Int8* pStr, sal_Int32 m_rSequence.realloc(nNewLength); } - OSL_ENSURE(m_rSequence.getLength() >= m_nSize + nLen, + OSL_ENSURE(m_rSequence.getLength() >= m_nSize + _rData.getLength(), "ooops ... the realloc algorithm seems to be wrong :( !"); - memcpy(m_rSequence.getArray() + m_nSize, pStr, nLen); - m_nSize += nLen; + memcpy(m_rSequence.getArray() + m_nSize, _rData.getConstArray(), _rData.getLength()); + m_nSize += _rData.getLength(); } void SAL_CALL OSequenceOutputStream::flush( ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) { + MutexGuard aGuard(m_aMutex); if (!m_bConnected) throw NotConnectedException(); // cut the sequence to the real size m_rSequence.realloc(m_nSize); - // and next time write to the beginning - m_nSize = 0; } void SAL_CALL OSequenceOutputStream::closeOutput( ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) { - flush(); + 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 = false; } diff --git a/include/comphelper/seqstream.hxx b/include/comphelper/seqstream.hxx index 1288a9baea73..d5729955000c 100644 --- a/include/comphelper/seqstream.hxx +++ b/include/comphelper/seqstream.hxx @@ -74,21 +74,26 @@ public: private: inline sal_Int32 avail(); }; +typedef ::cppu::WeakImplHelper1< ::com::sun::star::io::XOutputStream > OSequenceOutputStream_Base; -class COMPHELPER_DLLPUBLIC OSequenceOutputStream : - public ::cppu::WeakImplHelper1< ::com::sun::star::io::XOutputStream > +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; - /** 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_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. - bool m_bConnected; ///< closeOutput has been called ? + bool m_bConnected; + // closeOutput has been called ? + + ::osl::Mutex m_aMutex; + +protected: + virtual ~OSequenceOutputStream() { if (m_bConnected) closeOutput(); } public: /** constructs the object. Everything written into the stream through the XOutputStream methods will be forwarded @@ -113,20 +118,13 @@ public: sal_Int32 _nMaximumResize = -1 ); - virtual ~OSequenceOutputStream() { if (m_bConnected) closeOutput(); } - - sal_Int32 getSize() const { return m_nSize; } - /// 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, std::exception) SAL_OVERRIDE; - void SAL_CALL writeBytes( const sal_Int8* pStr, sal_Int32 nLen ) - throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception); - /** Resizes the sequence used for writing to the really used size. - * Next time, writeBytes will write to the beginning of the sequence. - */ + /// 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, std::exception) SAL_OVERRIDE; - /** Calls flush() and closes the output stream to prevent further manipulation with the sequence. - Every subsequent call to one of the XOutputStream methods will throw a <code>NotConnectedException</code>. + /** 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, std::exception) SAL_OVERRIDE; }; |