package/source/xstor/owriteablestream.cxx |   69 +++++-------------------------
 package/source/xstor/owriteablestream.hxx |    8 ++-
 2 files changed, 17 insertions(+), 60 deletions(-)

New commits:
commit 75fada58c2994d3b55a6c733dae9897f22eadf19
Author:     Mike Kaganski <[email protected]>
AuthorDate: Sat Jun 14 12:28:47 2025 +0500
Commit:     Mike Kaganski <[email protected]>
CommitDate: Sat Jun 14 14:16:50 2025 +0200

    Deduplicate initialization for write methods
    
    ByteWriter's writeBytes was copied from XOutputStream's one; they
    can share some code.
    
    Change-Id: Ife90fe785799144fde4641e8a2f170d507f601df
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/186487
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <[email protected]>

diff --git a/package/source/xstor/owriteablestream.cxx 
b/package/source/xstor/owriteablestream.cxx
index 27f30a8c453b..e3a12ee8db55 100644
--- a/package/source/xstor/owriteablestream.cxx
+++ b/package/source/xstor/owriteablestream.cxx
@@ -1977,12 +1977,9 @@ uno::Reference< io::XOutputStream > SAL_CALL 
OWriteStream::getOutputStream()
     return this;
 }
 
-void SAL_CALL OWriteStream::writeBytes( const uno::Sequence< sal_Int8 >& aData 
)
+void OWriteStream::CheckInitOnWriteDemand(sal_Int32 dataSize)
 {
-    osl::ClearableMutexGuard aGuard(m_xSharedMutex->GetMutex());
-
-    // the write method makes initialization itself, since it depends from the 
aData length
-    // NO CheckInitOnDemand()!
+    // write methods need a different initialization, since they depend on 
data length
 
     if ( !m_pImpl )
     {
@@ -1999,7 +1996,7 @@ void SAL_CALL OWriteStream::writeBytes( const 
uno::Sequence< sal_Int8 >& aData )
         {
             // check whether the cache should be turned off
             sal_Int64 nPos = m_xSeekable->getPosition();
-            if ( nPos + aData.getLength() > MAX_STORCACHE_SIZE )
+            if (nPos + dataSize > MAX_STORCACHE_SIZE)
             {
                 // disconnect the cache and copy the data to the temporary file
                 m_xSeekable->seek( 0 );
@@ -2018,7 +2015,7 @@ void SAL_CALL OWriteStream::writeBytes( const 
uno::Sequence< sal_Int8 >& aData )
 
     if ( m_bInitOnDemand )
     {
-        SAL_INFO( "package.xstor", "package (mv76033) 
OWriteStream::CheckInitOnDemand, initializing" );
+        SAL_INFO("package.xstor", "OWriteStream::CheckInitOnWriteDemand: 
initializing");
         uno::Reference< io::XStream > xStream = m_pImpl->GetTempFileAsStream();
         if ( xStream.is() )
         {
@@ -2031,6 +2028,13 @@ void SAL_CALL OWriteStream::writeBytes( const 
uno::Sequence< sal_Int8 >& aData )
             m_bInitOnDemand = false;
         }
     }
+}
+
+void SAL_CALL OWriteStream::writeBytes( const uno::Sequence< sal_Int8 >& aData 
)
+{
+    osl::ClearableMutexGuard aGuard(m_xSharedMutex->GetMutex());
+
+    CheckInitOnWriteDemand(aData.getLength());
 
     if ( !m_xOutStream.is() )
         throw io::NotConnectedException();
@@ -2047,56 +2051,7 @@ void OWriteStream::writeBytes( const sal_Int8* pData, 
sal_Int32 nBytesToWrite )
 
     osl::ClearableMutexGuard aGuard(m_xSharedMutex->GetMutex());
 
-    // the write method makes initialization itself, since it depends from the 
aData length
-    // NO CheckInitOnDemand()!
-
-    if ( !m_pImpl )
-    {
-        SAL_INFO("package.xstor", "Disposed!");
-        throw lang::DisposedException();
-    }
-
-    if ( !m_bInitOnDemand )
-    {
-        if ( !m_xOutStream.is() || !m_xSeekable.is())
-            throw io::NotConnectedException();
-
-        if ( m_pImpl->m_xCacheStream.is() )
-        {
-            // check whether the cache should be turned off
-            sal_Int64 nPos = m_xSeekable->getPosition();
-            if ( nPos + nBytesToWrite > MAX_STORCACHE_SIZE )
-            {
-                // disconnect the cache and copy the data to the temporary file
-                m_xSeekable->seek( 0 );
-
-                // it is enough to copy the cached stream, the cache should 
already contain everything
-                m_pImpl->GetFilledTempFileIfNo( m_xInStream );
-                if ( m_pImpl->m_oTempFile.has_value() )
-                {
-                    DeInit();
-                    // the last position is known and it is differs from the 
current stream position
-                    m_nInitPosition = nPos;
-                }
-            }
-        }
-    }
-
-    if ( m_bInitOnDemand )
-    {
-        SAL_INFO( "package.xstor", "package (mv76033) 
OWriteStream::CheckInitOnDemand, initializing" );
-        uno::Reference< io::XStream > xStream = m_pImpl->GetTempFileAsStream();
-        if ( xStream.is() )
-        {
-            m_xInStream.set( xStream->getInputStream(), uno::UNO_SET_THROW );
-            m_xOutStream.set( xStream->getOutputStream(), uno::UNO_SET_THROW );
-            m_xSeekable.set( xStream, uno::UNO_QUERY_THROW );
-            m_xSeekable->seek( m_nInitPosition );
-
-            m_nInitPosition = 0;
-            m_bInitOnDemand = false;
-        }
-    }
+    CheckInitOnWriteDemand(nBytesToWrite);
 
     if ( !m_xOutStream.is() )
         throw io::NotConnectedException();
diff --git a/package/source/xstor/owriteablestream.hxx 
b/package/source/xstor/owriteablestream.hxx
index 7152c30f5e85..442c48090d24 100644
--- a/package/source/xstor/owriteablestream.hxx
+++ b/package/source/xstor/owriteablestream.hxx
@@ -269,9 +269,6 @@ public:
 
     virtual ~OWriteStream() override;
 
-    void CheckInitOnDemand();
-    void DeInit();
-
     // XInterface
     virtual css::uno::Any SAL_CALL queryInterface( const css::uno::Type& rType 
) override;
     virtual void SAL_CALL acquire() noexcept override;
@@ -354,6 +351,11 @@ public:
 
     // comphelper::ByteWriter
     virtual void writeBytes(const sal_Int8* aData, sal_Int32 nBytesToWrite) 
override;
+
+private:
+    void CheckInitOnDemand();
+    void CheckInitOnWriteDemand(sal_Int32 dataSize);
+    void DeInit();
 };
 
 #endif

Reply via email to