diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2022-05-15 13:03:18 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2022-05-19 09:29:14 +0200 |
commit | d1dc27fef899ff6cd70873cd945d5312a53e1b3a (patch) | |
tree | 53e4975c8b7af4d493d797d798d42f26fb241c80 /unotools | |
parent | 2589f8a155d00b22078607ddd0229d155a394f3a (diff) |
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 <noel.grandin@collabora.co.uk>
Diffstat (limited to 'unotools')
-rw-r--r-- | unotools/source/streaming/streamhelper.cxx | 10 | ||||
-rw-r--r-- | unotools/source/streaming/streamwrap.cxx | 23 | ||||
-rw-r--r-- | unotools/source/ucbhelper/ucblockbytes.cxx | 20 |
3 files changed, 50 insertions, 3 deletions
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 <com/sun/star/io/BufferSizeExceededException.hpp> #include <com/sun/star/io/IOException.hpp> #include <com/sun/star/io/NotConnectedException.hpp> +#include <comphelper/servicehelper.hxx> #include <o3tl/safeint.hxx> #include <unotools/streamhelper.hxx> +#include <unotools/bytereader.hxx> namespace utl { @@ -118,6 +120,14 @@ void SAL_CALL OInputStreamHelper::acquire() SAL_NOEXCEPT cppu::WeakImplHelper<css::io::XInputStream, css::io::XSeekable>::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<css::uno::XWeak*>(this)); + + std::scoped_lock aGuard( m_aMutex ); + + sal_uInt32 nRead = m_pSvStream->ReadBytes(static_cast<void*>(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<css::uno::XWeak*>(static_cast<const css::uno::XWeak*>(this))); } +sal_Int64 SAL_CALL OInputStreamWrapper::getSomething( const css::uno::Sequence< sal_Int8 >& rIdentifier ) +{ + if (rIdentifier == utl::ByteReader::getUnoTunnelId()) + return reinterpret_cast<sal_Int64>(static_cast<utl::ByteReader*>(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 <tools/urlobj.hxx> #include <tools/solar.h> #include <ucbhelper/interactionrequest.hxx> +#include <com/sun/star/lang/XUnoTunnel.hpp> #include <com/sun/star/task/XInteractionAbort.hpp> #include <com/sun/star/ucb/InteractiveNetworkConnectException.hpp> #include <com/sun/star/ucb/CommandFailedException.hpp> @@ -57,6 +58,7 @@ #include <comphelper/storagehelper.hxx> #include <ucbhelper/content.hxx> +#include <unotools/bytereader.hxx> #include <mutex> using namespace ::com::sun::star::uno; @@ -1092,7 +1094,6 @@ ErrCode UcbLockBytes::ReadAt(sal_uInt64 const nPos, return ERRCODE_IO_CANTSEEK; } - Sequence<sal_Int8> 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<sal_Int8*>(pBuffer), sal_Int32(nCount) ); + } + else + { + Sequence<sal_Int8> 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<std::size_t>(nSize); |