From d203e3f35dc4547bb46151637ba6054d13025d01 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Tue, 24 May 2022 15:42:04 +0200 Subject: use comphelper::ByteWriter in UNOMemoryStream to avoid a temporary buffer Change-Id: Ibe1d4ffb240993e14eaaddb219f9cde328f9afbe Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134919 Tested-by: Jenkins Reviewed-by: Noel Grandin --- comphelper/source/streaming/memorystream.cxx | 43 +++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'comphelper') diff --git a/comphelper/source/streaming/memorystream.cxx b/comphelper/source/streaming/memorystream.cxx index ea1a955cb4d4..199d6a9a6f77 100644 --- a/comphelper/source/streaming/memorystream.cxx +++ b/comphelper/source/streaming/memorystream.cxx @@ -21,11 +21,13 @@ #include #include +#include #include #include #include #include //#include +#include #include #include #include @@ -47,7 +49,9 @@ namespace comphelper namespace { -class UNOMemoryStream : public WeakImplHelper +class UNOMemoryStream : + public WeakImplHelper, + public comphelper::ByteWriter { public: UNOMemoryStream(); @@ -81,6 +85,12 @@ public: // XTruncate virtual void SAL_CALL truncate() override; + // XUnoTunnel + virtual sal_Int64 SAL_CALL getSomething( const css::uno::Sequence< sal_Int8 >& aIdentifier ) override; + + // comphelper::ByteWriter + virtual sal_Int32 writeSomeBytes(const sal_Int8* aData, sal_Int32 nBytesToWrite) override; + private: // prevents std::vector from wasting time doing memset on data we are going to overwrite anyway struct NoInitInt8 @@ -224,6 +234,30 @@ void SAL_CALL UNOMemoryStream::writeBytes( const Sequence< sal_Int8 >& aData ) mnCursor += nBytesToWrite; } +sal_Int32 UNOMemoryStream::writeSomeBytes( const sal_Int8* pInData, sal_Int32 nBytesToWrite ) +{ + if( !nBytesToWrite ) + return 0; + + sal_Int64 nNewSize = static_cast(mnCursor) + nBytesToWrite; + if( nNewSize > SAL_MAX_INT32 ) + { + OSL_ASSERT(false); + throw IOException("this implementation does not support more than 2GB!", static_cast(this) ); + } + + if( static_cast< sal_Int32 >( nNewSize ) > static_cast< sal_Int32 >( maData.size() ) ) + maData.resize( nNewSize ); + + NoInitInt8* pData = &(*maData.begin()); + NoInitInt8* pCursor = &(pData[mnCursor]); + // cast to avoid -Werror=class-memaccess + memcpy(static_cast(pCursor), pInData, nBytesToWrite); + + mnCursor += nBytesToWrite; + return nBytesToWrite; +} + void SAL_CALL UNOMemoryStream::flush() { } @@ -240,6 +274,13 @@ void SAL_CALL UNOMemoryStream::truncate() mnCursor = 0; } +sal_Int64 SAL_CALL UNOMemoryStream::getSomething( const css::uno::Sequence< sal_Int8 >& rIdentifier ) +{ + if (rIdentifier == comphelper::ByteWriter::getUnoTunnelId()) + return reinterpret_cast(static_cast(this)); + return 0; +} + } // namespace comphelper extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * -- cgit