24#include <boost/core/noinit_adaptor.hpp>
26#include <com/sun/star/lang/IllegalArgumentException.hpp>
27#include <com/sun/star/lang/XServiceInfo.hpp>
28#include <com/sun/star/lang/XUnoTunnel.hpp>
29#include <com/sun/star/io/IOException.hpp>
30#include <com/sun/star/io/XStream.hpp>
31#include <com/sun/star/io/XSeekableInputStream.hpp>
32#include <com/sun/star/io/XTruncate.hpp>
38#include <osl/diagnose.h>
45using ::cppu::OWeakObject;
46using ::cppu::WeakImplHelper;
57class UNOMemoryStream :
58 public WeakImplHelper<XServiceInfo, XStream, XSeekableInputStream, XOutputStream, XTruncate>,
59 public comphelper::ByteWriter
70 virtual Reference< XInputStream > SAL_CALL getInputStream( )
override;
71 virtual Reference< XOutputStream > SAL_CALL
getOutputStream( )
override;
74 virtual sal_Int32 SAL_CALL readBytes(
Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
override;
75 virtual sal_Int32 SAL_CALL readSomeBytes(
Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
override;
76 virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
override;
77 virtual sal_Int32 SAL_CALL available()
override;
78 virtual void SAL_CALL closeInput()
override;
81 virtual void SAL_CALL seek( sal_Int64 location )
override;
82 virtual sal_Int64 SAL_CALL getPosition()
override;
83 virtual sal_Int64 SAL_CALL
getLength()
override;
87 virtual void SAL_CALL flush()
override;
88 virtual void SAL_CALL closeOutput()
override;
91 virtual void SAL_CALL truncate()
override;
94 virtual void writeBytes(
const sal_Int8* aData, sal_Int32 nBytesToWrite)
override;
97 std::vector< sal_Int8, boost::noinit_adaptor<std::allocator<sal_Int8>> >
maData;
103UNOMemoryStream::UNOMemoryStream()
106 maData.reserve(1 * 1024 * 1024);
110OUString SAL_CALL UNOMemoryStream::getImplementationName()
112 return "com.sun.star.comp.MemoryStream";
115sal_Bool SAL_CALL UNOMemoryStream::supportsService(
const OUString& ServiceName)
120css::uno::Sequence<OUString> SAL_CALL UNOMemoryStream::getSupportedServiceNames()
122 return {
"com.sun.star.comp.MemoryStream" };
126Reference< XInputStream > SAL_CALL UNOMemoryStream::getInputStream( )
131Reference< XOutputStream > SAL_CALL UNOMemoryStream::getOutputStream( )
137sal_Int32 SAL_CALL UNOMemoryStream::readBytes(
Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
139 if( nBytesToRead < 0 )
142 nBytesToRead = std::min( nBytesToRead, available() );
143 aData.realloc( nBytesToRead );
149 memcpy(
aData.getArray(), pCursor, nBytesToRead );
157sal_Int32 SAL_CALL UNOMemoryStream::readSomeBytes(
Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
159 return readBytes( aData, nMaxBytesToRead );
162void SAL_CALL UNOMemoryStream::skipBytes( sal_Int32 nBytesToSkip )
164 if( nBytesToSkip < 0 )
167 mnCursor += std::min( nBytesToSkip, available() );
170sal_Int32 SAL_CALL UNOMemoryStream::available()
172 return std::min<sal_Int64>( SAL_MAX_INT32,
maData.size() -
mnCursor);
175void SAL_CALL UNOMemoryStream::closeInput()
181void SAL_CALL UNOMemoryStream::seek( sal_Int64 location )
183 if( (location < 0) || (location > SAL_MAX_INT32) )
184 throw IllegalArgumentException(
"this implementation does not support more than 2GB!",
static_cast<OWeakObject*
>(
this), 0 );
188 maData.resize(
static_cast< sal_Int32
>( location ) );
190 mnCursor =
static_cast< sal_Int32
>( location );
193sal_Int64 SAL_CALL UNOMemoryStream::getPosition()
195 return static_cast< sal_Int64
>(
mnCursor );
198sal_Int64 SAL_CALL UNOMemoryStream::getLength()
200 return static_cast< sal_Int64
>(
maData.size() );
206 writeBytes(
aData.getConstArray(),
aData.getLength());
209void UNOMemoryStream::writeBytes(
const sal_Int8* pInData, sal_Int32 nBytesToWrite )
211 assert(nBytesToWrite >= 0);
215 sal_Int64 nNewSize =
static_cast<sal_Int64
>(
mnCursor) + nBytesToWrite;
216 if( nNewSize > SAL_MAX_INT32 )
219 throw IOException(
"this implementation does not support more than 2GB!",
static_cast<OWeakObject*
>(
this) );
223 maData.resize( nNewSize );
227 memcpy(pCursor, pInData, nBytesToWrite);
232void SAL_CALL UNOMemoryStream::flush()
236void SAL_CALL UNOMemoryStream::closeOutput()
242void SAL_CALL UNOMemoryStream::truncate()
250extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
252 css::uno::XComponentContext *,
253 css::uno::Sequence<css::uno::Any>
const &)
255 return cppu::acquire(new ::comphelper::UNOMemoryStream());
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_MemoryStream(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
std::vector< sal_Int8, boost::noinit_adaptor< std::allocator< sal_Int8 > > > maData
std::unique_ptr< sal_Int32[]> pData
constexpr OUStringLiteral aData
double getLength(const B2DPolygon &rCandidate)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
bool getOutputStream(ProgramOptions const &options, OString const &extension, std::ostream **ppOutputStream, OString &targetSourceFileName, OString &tmpSourceFileName)