summaryrefslogtreecommitdiffstats
path: root/comphelper/source/streaming/memorystream.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'comphelper/source/streaming/memorystream.cxx')
-rw-r--r--comphelper/source/streaming/memorystream.cxx33
1 files changed, 25 insertions, 8 deletions
diff --git a/comphelper/source/streaming/memorystream.cxx b/comphelper/source/streaming/memorystream.cxx
index b8ff86e03b17..20d0fbd5766a 100644
--- a/comphelper/source/streaming/memorystream.cxx
+++ b/comphelper/source/streaming/memorystream.cxx
@@ -18,16 +18,23 @@
*/
#include <algorithm>
+#include <cassert>
+#include <memory>
+
+#include <boost/core/noinit_adaptor.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
+#include <com/sun/star/lang/XUnoTunnel.hpp>
#include <com/sun/star/io/IOException.hpp>
#include <com/sun/star/io/XStream.hpp>
#include <com/sun/star/io/XSeekableInputStream.hpp>
#include <com/sun/star/io/XTruncate.hpp>
//#include <com/sun/star/uno/XComponentContext.hpp>
+#include <comphelper/bytereader.hxx>
#include <cppuhelper/implbase.hxx>
#include <cppuhelper/supportsservice.hxx>
+#include <o3tl/safeint.hxx>
#include <osl/diagnose.h>
#include <string.h>
@@ -40,14 +47,15 @@ using ::cppu::WeakImplHelper;
using namespace ::com::sun::star::io;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
-using namespace ::osl;
namespace comphelper
{
namespace {
-class UNOMemoryStream : public WeakImplHelper<XServiceInfo, XStream, XSeekableInputStream, XOutputStream, XTruncate>
+class UNOMemoryStream :
+ public WeakImplHelper<XServiceInfo, XStream, XSeekableInputStream, XOutputStream, XTruncate>,
+ public comphelper::ByteWriter
{
public:
UNOMemoryStream();
@@ -81,8 +89,11 @@ public:
// XTruncate
virtual void SAL_CALL truncate() override;
+ // comphelper::ByteWriter
+ virtual void writeBytes(const sal_Int8* aData, sal_Int32 nBytesToWrite) override;
+
private:
- std::vector< sal_Int8 > maData;
+ std::vector< sal_Int8, boost::noinit_adaptor<std::allocator<sal_Int8>> > maData;
sal_Int32 mnCursor;
};
@@ -91,6 +102,7 @@ private:
UNOMemoryStream::UNOMemoryStream()
: mnCursor(0)
{
+ maData.reserve(1 * 1024 * 1024);
}
// XServiceInfo
@@ -133,7 +145,7 @@ sal_Int32 SAL_CALL UNOMemoryStream::readBytes( Sequence< sal_Int8 >& aData, sal_
{
sal_Int8* pData = &(*maData.begin());
sal_Int8* pCursor = &(pData[mnCursor]);
- memcpy( static_cast<void*>(aData.getArray()), static_cast<void*>(pCursor), nBytesToRead );
+ memcpy( aData.getArray(), pCursor, nBytesToRead );
mnCursor += nBytesToRead;
}
@@ -171,7 +183,7 @@ void SAL_CALL UNOMemoryStream::seek( sal_Int64 location )
throw IllegalArgumentException("this implementation does not support more than 2GB!", static_cast<OWeakObject*>(this), 0 );
// seek operation should be able to resize the stream
- if ( location > static_cast< sal_Int64 >( maData.size() ) )
+ if ( o3tl::make_unsigned(location) > maData.size() )
maData.resize( static_cast< sal_Int32 >( location ) );
mnCursor = static_cast< sal_Int32 >( location );
@@ -190,7 +202,12 @@ sal_Int64 SAL_CALL UNOMemoryStream::getLength()
// XOutputStream
void SAL_CALL UNOMemoryStream::writeBytes( const Sequence< sal_Int8 >& aData )
{
- const sal_Int32 nBytesToWrite( aData.getLength() );
+ writeBytes(aData.getConstArray(), aData.getLength());
+}
+
+void UNOMemoryStream::writeBytes( const sal_Int8* pInData, sal_Int32 nBytesToWrite )
+{
+ assert(nBytesToWrite >= 0);
if( !nBytesToWrite )
return;
@@ -201,12 +218,12 @@ void SAL_CALL UNOMemoryStream::writeBytes( const Sequence< sal_Int8 >& aData )
throw IOException("this implementation does not support more than 2GB!", static_cast<OWeakObject*>(this) );
}
- if( static_cast< sal_Int32 >( nNewSize ) > static_cast< sal_Int32 >( maData.size() ) )
+ if( o3tl::make_unsigned( nNewSize ) > maData.size() )
maData.resize( nNewSize );
sal_Int8* pData = &(*maData.begin());
sal_Int8* pCursor = &(pData[mnCursor]);
- memcpy( pCursor, aData.getConstArray(), nBytesToWrite );
+ memcpy(pCursor, pInData, nBytesToWrite);
mnCursor += nBytesToWrite;
}