From d1dc27fef899ff6cd70873cd945d5312a53e1b3a Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Sun, 15 May 2022 13:03:18 +0200 Subject: add utl::ByteReader pure class which lets us skip the inefficiency of needing an extra buffer when reading via XInputStream Change-Id: Ic5334b7d11ea6a57bc1800f508fc69611a053af1 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134348 Tested-by: Jenkins Reviewed-by: Noel Grandin --- unotools/source/streaming/streamhelper.cxx | 10 ++++++++++ unotools/source/streaming/streamwrap.cxx | 23 +++++++++++++++++++++++ unotools/source/ucbhelper/ucblockbytes.cxx | 20 +++++++++++++++++--- 3 files changed, 50 insertions(+), 3 deletions(-) (limited to 'unotools') diff --git a/unotools/source/streaming/streamhelper.cxx b/unotools/source/streaming/streamhelper.cxx index b5f07e6a6a7f..39585ec5f369 100644 --- a/unotools/source/streaming/streamhelper.cxx +++ b/unotools/source/streaming/streamhelper.cxx @@ -22,8 +22,10 @@ #include #include #include +#include #include #include +#include namespace utl { @@ -118,6 +120,14 @@ void SAL_CALL OInputStreamHelper::acquire() SAL_NOEXCEPT cppu::WeakImplHelper::acquire(); } +ByteReader::~ByteReader() {} + +const css::uno::Sequence< sal_Int8 > & ByteReader::getUnoTunnelId() +{ + static const comphelper::UnoIdInit implId; + return implId.getSeq(); +} + } // namespace utl /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/streaming/streamwrap.cxx b/unotools/source/streaming/streamwrap.cxx index ccd9b5033d2a..0ed933b530e3 100644 --- a/unotools/source/streaming/streamwrap.cxx +++ b/unotools/source/streaming/streamwrap.cxx @@ -78,6 +78,21 @@ sal_Int32 SAL_CALL OInputStreamWrapper::readBytes(css::uno::Sequence< sal_Int8 > return nRead; } +sal_Int32 OInputStreamWrapper::readSomeBytes(sal_Int8* pData, sal_Int32 nBytesToRead) +{ + checkConnected(); + + if (nBytesToRead < 0) + throw css::io::BufferSizeExceededException(OUString(),static_cast(this)); + + std::scoped_lock aGuard( m_aMutex ); + + sal_uInt32 nRead = m_pSvStream->ReadBytes(static_cast(pData), nBytesToRead); + checkError(); + + return nRead; +} + sal_Int32 SAL_CALL OInputStreamWrapper::readSomeBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) { checkError(); @@ -141,6 +156,14 @@ void OInputStreamWrapper::checkError() const throw css::io::NotConnectedException("utl::OInputStreamWrapper error " + e.toHexString(), const_cast(static_cast(this))); } +sal_Int64 SAL_CALL OInputStreamWrapper::getSomething( const css::uno::Sequence< sal_Int8 >& rIdentifier ) +{ + if (rIdentifier == utl::ByteReader::getUnoTunnelId()) + return reinterpret_cast(static_cast(this)); + return 0; +} + + //= OSeekableInputStreamWrapper OSeekableInputStreamWrapper::~OSeekableInputStreamWrapper() = default; diff --git a/unotools/source/ucbhelper/ucblockbytes.cxx b/unotools/source/ucbhelper/ucblockbytes.cxx index 2d716f571586..b40be28f2225 100644 --- a/unotools/source/ucbhelper/ucblockbytes.cxx +++ b/unotools/source/ucbhelper/ucblockbytes.cxx @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -57,6 +58,7 @@ #include #include +#include #include using namespace ::com::sun::star::uno; @@ -1092,7 +1094,6 @@ ErrCode UcbLockBytes::ReadAt(sal_uInt64 const nPos, return ERRCODE_IO_CANTSEEK; } - Sequence aData; sal_Int32 nSize; if(nCount > 0x7FFFFFFF) @@ -1108,14 +1109,27 @@ ErrCode UcbLockBytes::ReadAt(sal_uInt64 const nPos, return ERRCODE_IO_PENDING; } - nSize = xStream->readBytes( aData, sal_Int32(nCount) ); + Reference< css::lang::XUnoTunnel > xTunnel( xStream, UNO_QUERY ); + utl::ByteReader* pByteReader = nullptr; + if (xTunnel) + pByteReader = reinterpret_cast< utl::ByteReader* >( xTunnel->getSomething( utl::ByteReader::getUnoTunnelId() ) ); + + if (pByteReader) + { + nSize = pByteReader->readSomeBytes( static_cast(pBuffer), sal_Int32(nCount) ); + } + else + { + Sequence aData; + nSize = xStream->readBytes( aData, sal_Int32(nCount) ); + memcpy (pBuffer, aData.getConstArray(), nSize); + } } catch (const IOException&) { return ERRCODE_IO_CANTREAD; } - memcpy (pBuffer, aData.getConstArray(), nSize); if (pRead) *pRead = static_cast(nSize); -- cgit