LibreOffice Module comphelper (master) 1
memorystream.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <algorithm>
21#include <cassert>
22#include <memory>
23
24#include <boost/core/noinit_adaptor.hpp>
25
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>
33//#include <com/sun/star/uno/XComponentContext.hpp>
37#include <o3tl/safeint.hxx>
38#include <osl/diagnose.h>
39
40#include <string.h>
41#include <vector>
42
43namespace com::sun::star::uno { class XComponentContext; }
44
45using ::cppu::OWeakObject;
46using ::cppu::WeakImplHelper;
47using namespace ::com::sun::star::io;
48using namespace ::com::sun::star::uno;
49using namespace ::com::sun::star::lang;
50using namespace ::osl;
51
52namespace comphelper
53{
54
55namespace {
56
57class UNOMemoryStream :
58 public WeakImplHelper<XServiceInfo, XStream, XSeekableInputStream, XOutputStream, XTruncate>,
59 public comphelper::ByteWriter
60{
61public:
62 UNOMemoryStream();
63
64 // XServiceInfo
65 virtual OUString SAL_CALL getImplementationName() override;
66 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
67 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
68
69 // XStream
70 virtual Reference< XInputStream > SAL_CALL getInputStream( ) override;
71 virtual Reference< XOutputStream > SAL_CALL getOutputStream( ) override;
72
73 // XInputStream
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;
79
80 // XSeekable
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;
84
85 // XOutputStream
86 virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData ) override;
87 virtual void SAL_CALL flush() override;
88 virtual void SAL_CALL closeOutput() override;
89
90 // XTruncate
91 virtual void SAL_CALL truncate() override;
92
93 // comphelper::ByteWriter
94 virtual void writeBytes(const sal_Int8* aData, sal_Int32 nBytesToWrite) override;
95
96private:
97 std::vector< sal_Int8, boost::noinit_adaptor<std::allocator<sal_Int8>> > maData;
98 sal_Int32 mnCursor;
99};
100
101}
102
103UNOMemoryStream::UNOMemoryStream()
104: mnCursor(0)
105{
106 maData.reserve(1 * 1024 * 1024);
107}
108
109// XServiceInfo
110OUString SAL_CALL UNOMemoryStream::getImplementationName()
111{
112 return "com.sun.star.comp.MemoryStream";
113}
114
115sal_Bool SAL_CALL UNOMemoryStream::supportsService(const OUString& ServiceName)
116{
117 return cppu::supportsService(this, ServiceName);
118}
119
120css::uno::Sequence<OUString> SAL_CALL UNOMemoryStream::getSupportedServiceNames()
121{
122 return { "com.sun.star.comp.MemoryStream" };
123}
124
125// XStream
126Reference< XInputStream > SAL_CALL UNOMemoryStream::getInputStream( )
127{
128 return this;
129}
130
131Reference< XOutputStream > SAL_CALL UNOMemoryStream::getOutputStream( )
132{
133 return this;
134}
135
136// XInputStream
137sal_Int32 SAL_CALL UNOMemoryStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
138{
139 if( nBytesToRead < 0 )
140 throw IOException("nBytesToRead < 0");
141
142 nBytesToRead = std::min( nBytesToRead, available() );
143 aData.realloc( nBytesToRead );
144
145 if( nBytesToRead )
146 {
147 sal_Int8* pData = &(*maData.begin());
148 sal_Int8* pCursor = &(pData[mnCursor]);
149 memcpy( aData.getArray(), pCursor, nBytesToRead );
150
151 mnCursor += nBytesToRead;
152 }
153
154 return nBytesToRead;
155}
156
157sal_Int32 SAL_CALL UNOMemoryStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
158{
159 return readBytes( aData, nMaxBytesToRead );
160}
161
162void SAL_CALL UNOMemoryStream::skipBytes( sal_Int32 nBytesToSkip )
163{
164 if( nBytesToSkip < 0 )
165 throw IOException("nBytesToSkip < 0");
166
167 mnCursor += std::min( nBytesToSkip, available() );
168}
169
170sal_Int32 SAL_CALL UNOMemoryStream::available()
171{
172 return std::min<sal_Int64>( SAL_MAX_INT32, maData.size() - mnCursor);
173}
174
175void SAL_CALL UNOMemoryStream::closeInput()
176{
177 mnCursor = 0;
178}
179
180// XSeekable
181void SAL_CALL UNOMemoryStream::seek( sal_Int64 location )
182{
183 if( (location < 0) || (location > SAL_MAX_INT32) )
184 throw IllegalArgumentException("this implementation does not support more than 2GB!", static_cast<OWeakObject*>(this), 0 );
185
186 // seek operation should be able to resize the stream
187 if ( o3tl::make_unsigned(location) > maData.size() )
188 maData.resize( static_cast< sal_Int32 >( location ) );
189
190 mnCursor = static_cast< sal_Int32 >( location );
191}
192
193sal_Int64 SAL_CALL UNOMemoryStream::getPosition()
194{
195 return static_cast< sal_Int64 >( mnCursor );
196}
197
198sal_Int64 SAL_CALL UNOMemoryStream::getLength()
199{
200 return static_cast< sal_Int64 >( maData.size() );
201}
202
203// XOutputStream
204void SAL_CALL UNOMemoryStream::writeBytes( const Sequence< sal_Int8 >& aData )
205{
206 writeBytes(aData.getConstArray(), aData.getLength());
207}
208
209void UNOMemoryStream::writeBytes( const sal_Int8* pInData, sal_Int32 nBytesToWrite )
210{
211 assert(nBytesToWrite >= 0);
212 if( !nBytesToWrite )
213 return;
214
215 sal_Int64 nNewSize = static_cast<sal_Int64>(mnCursor) + nBytesToWrite;
216 if( nNewSize > SAL_MAX_INT32 )
217 {
218 OSL_ASSERT(false);
219 throw IOException("this implementation does not support more than 2GB!", static_cast<OWeakObject*>(this) );
220 }
221
222 if( o3tl::make_unsigned( nNewSize ) > maData.size() )
223 maData.resize( nNewSize );
224
225 sal_Int8* pData = &(*maData.begin());
226 sal_Int8* pCursor = &(pData[mnCursor]);
227 memcpy(pCursor, pInData, nBytesToWrite);
228
229 mnCursor += nBytesToWrite;
230}
231
232void SAL_CALL UNOMemoryStream::flush()
233{
234}
235
236void SAL_CALL UNOMemoryStream::closeOutput()
237{
238 mnCursor = 0;
239}
240
241//XTruncate
242void SAL_CALL UNOMemoryStream::truncate()
243{
244 maData.clear();
245 mnCursor = 0;
246}
247
248} // namespace comphelper
249
250extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
252 css::uno::XComponentContext *,
253 css::uno::Sequence<css::uno::Any> const &)
254{
255 return cppu::acquire(new ::comphelper::UNOMemoryStream());
256}
257
258/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_MemoryStream(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
sal_Int32 mnCursor
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)
unsigned char sal_Bool
signed char sal_Int8