summaryrefslogtreecommitdiff
path: root/comphelper
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2022-05-19 13:46:40 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2022-05-19 21:04:48 +0200
commit62531ec1091c7b3f6a3577889a18234790ec716d (patch)
tree72766dd4a58bbf61e2a56adc870d1a73307a9043 /comphelper
parent5eb25f6a7ecb215f7bc81116cd930c1dec645e8d (diff)
add ByteWriter to reduce memory copying when writing data
similarly to ByteReader move both of them down to comphelper, since we want to use it from comphelper, and comphelper is "below" unotools in the module dependency graph Change-Id: Ic98fa2268e125fd8e4378fb899ad5f97de721713 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134645 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'comphelper')
-rw-r--r--comphelper/source/misc/storagehelper.cxx48
-rw-r--r--comphelper/source/streaming/basicio.cxx18
-rw-r--r--comphelper/source/streaming/seqstream.cxx24
3 files changed, 80 insertions, 10 deletions
diff --git a/comphelper/source/misc/storagehelper.cxx b/comphelper/source/misc/storagehelper.cxx
index 8e08612b0699..5f8527bcf273 100644
--- a/comphelper/source/misc/storagehelper.cxx
+++ b/comphelper/source/misc/storagehelper.cxx
@@ -27,6 +27,7 @@
#include <com/sun/star/embed/FileSystemStorageFactory.hpp>
#include <com/sun/star/io/IOException.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
+#include <com/sun/star/lang/XUnoTunnel.hpp>
#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
#include <com/sun/star/ucb/SimpleFileAccess.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
@@ -48,6 +49,7 @@
#include <ucbhelper/content.hxx>
+#include <comphelper/bytereader.hxx>
#include <comphelper/fileformat.h>
#include <comphelper/hash.hxx>
#include <comphelper/processfactory.hxx>
@@ -173,21 +175,47 @@ void OStorageHelper::CopyInputToOutput(
{
static const sal_Int32 nConstBufferSize = 32000;
- sal_Int32 nRead;
- uno::Sequence < sal_Int8 > aSequence ( nConstBufferSize );
+ uno::Reference< css::lang::XUnoTunnel > xInputTunnel( xInput, uno::UNO_QUERY );
+ comphelper::ByteReader* pByteReader = nullptr;
+ comphelper::ByteWriter* pByteWriter = nullptr;
+ if (xInputTunnel)
+ pByteReader = reinterpret_cast< comphelper::ByteReader* >( xInputTunnel->getSomething( comphelper::ByteReader::getUnoTunnelId() ) );
+ if (pByteReader)
+ {
+ uno::Reference< css::lang::XUnoTunnel > xOutputTunnel( xOutput, uno::UNO_QUERY );
+ if (xOutputTunnel)
+ pByteWriter = reinterpret_cast< comphelper::ByteWriter* >( xOutputTunnel->getSomething( comphelper::ByteWriter::getUnoTunnelId() ) );
+ }
- do
+ if (pByteWriter)
{
- nRead = xInput->readBytes ( aSequence, nConstBufferSize );
- if ( nRead < nConstBufferSize )
+ sal_Int32 nRead;
+ sal_Int8 aTempBuf[ nConstBufferSize ];
+ do
{
- uno::Sequence < sal_Int8 > aTempBuf ( aSequence.getConstArray(), nRead );
- xOutput->writeBytes ( aTempBuf );
+ nRead = pByteReader->readSomeBytes ( aTempBuf, nConstBufferSize );
+ pByteWriter->writeSomeBytes ( aTempBuf, nRead );
}
- else
- xOutput->writeBytes ( aSequence );
+ while ( nRead == nConstBufferSize );
+ }
+ else
+ {
+ sal_Int32 nRead;
+ uno::Sequence < sal_Int8 > aSequence ( nConstBufferSize );
+
+ do
+ {
+ nRead = xInput->readBytes ( aSequence, nConstBufferSize );
+ if ( nRead < nConstBufferSize )
+ {
+ uno::Sequence < sal_Int8 > aTempBuf ( aSequence.getConstArray(), nRead );
+ xOutput->writeBytes ( aTempBuf );
+ }
+ else
+ xOutput->writeBytes ( aSequence );
+ }
+ while ( nRead == nConstBufferSize );
}
- while ( nRead == nConstBufferSize );
}
diff --git a/comphelper/source/streaming/basicio.cxx b/comphelper/source/streaming/basicio.cxx
index d86427b7de81..b8c0c96e2cfa 100644
--- a/comphelper/source/streaming/basicio.cxx
+++ b/comphelper/source/streaming/basicio.cxx
@@ -18,6 +18,8 @@
*/
#include <comphelper/basicio.hxx>
+#include <comphelper/bytereader.hxx>
+#include <comphelper/servicehelper.hxx>
#include <com/sun/star/awt/FontDescriptor.hpp>
namespace comphelper
@@ -157,6 +159,22 @@ const css::uno::Reference<css::io::XObjectOutputStream>& operator << (const css:
return _rxOutStream;
}
+ByteReader::~ByteReader() {}
+
+const css::uno::Sequence< sal_Int8 > & ByteReader::getUnoTunnelId()
+{
+ static const comphelper::UnoIdInit implId;
+ return implId.getSeq();
+}
+
+ByteWriter::~ByteWriter() {}
+
+const css::uno::Sequence< sal_Int8 > & ByteWriter::getUnoTunnelId()
+{
+ static const comphelper::UnoIdInit implId;
+ return implId.getSeq();
+}
+
} // namespace comphelper
diff --git a/comphelper/source/streaming/seqstream.cxx b/comphelper/source/streaming/seqstream.cxx
index db39a1dc30e8..1f37a7967b04 100644
--- a/comphelper/source/streaming/seqstream.cxx
+++ b/comphelper/source/streaming/seqstream.cxx
@@ -74,6 +74,30 @@ sal_Int32 SAL_CALL SequenceInputStream::readBytes( Sequence<sal_Int8>& aData, sa
return nBytesToRead;
}
+sal_Int32 SequenceInputStream::readSomeBytes( sal_Int8* pData, sal_Int32 nBytesToRead )
+{
+ if (nBytesToRead < 0)
+ throw BufferSizeExceededException(OUString(),*this);
+
+ std::scoped_lock aGuard( m_aMutex );
+
+ sal_Int32 nAvail = avail();
+
+ if (nAvail < nBytesToRead)
+ nBytesToRead = nAvail;
+
+ memcpy(pData, m_aData.getConstArray() + m_nPos, nBytesToRead);
+ m_nPos += nBytesToRead;
+
+ return nBytesToRead;
+}
+
+sal_Int64 SAL_CALL SequenceInputStream::getSomething( const css::uno::Sequence< sal_Int8 >& rIdentifier )
+{
+ if (rIdentifier == comphelper::ByteReader::getUnoTunnelId())
+ return reinterpret_cast<sal_Int64>(static_cast<comphelper::ByteReader*>(this));
+ return 0;
+}
sal_Int32 SAL_CALL SequenceInputStream::readSomeBytes( Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead )
{