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, XUnoTunnel>,
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 // XUnoTunnel
94 virtual sal_Int64 SAL_CALL getSomething( const css::uno::Sequence< sal_Int8 >& aIdentifier ) override;
95
96 // comphelper::ByteWriter
97 virtual void writeBytes(const sal_Int8* aData, sal_Int32 nBytesToWrite) override;
98
99private:
100 std::vector< sal_Int8, boost::noinit_adaptor<std::allocator<sal_Int8>> > maData;
101 sal_Int32 mnCursor;
102};
103
104}
105
106UNOMemoryStream::UNOMemoryStream()
107: mnCursor(0)
108{
109 maData.reserve(1 * 1024 * 1024);
110}
111
112// XServiceInfo
113OUString SAL_CALL UNOMemoryStream::getImplementationName()
114{
115 return "com.sun.star.comp.MemoryStream";
116}
117
118sal_Bool SAL_CALL UNOMemoryStream::supportsService(const OUString& ServiceName)
119{
120 return cppu::supportsService(this, ServiceName);
121}
122
123css::uno::Sequence<OUString> SAL_CALL UNOMemoryStream::getSupportedServiceNames()
124{
125 return { "com.sun.star.comp.MemoryStream" };
126}
127
128// XStream
129Reference< XInputStream > SAL_CALL UNOMemoryStream::getInputStream( )
130{
131 return this;
132}
133
134Reference< XOutputStream > SAL_CALL UNOMemoryStream::getOutputStream( )
135{
136 return this;
137}
138
139// XInputStream
140sal_Int32 SAL_CALL UNOMemoryStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
141{
142 if( nBytesToRead < 0 )
143 throw IOException("nBytesToRead < 0");
144
145 nBytesToRead = std::min( nBytesToRead, available() );
146 aData.realloc( nBytesToRead );
147
148 if( nBytesToRead )
149 {
150 sal_Int8* pData = &(*maData.begin());
151 sal_Int8* pCursor = &(pData[mnCursor]);
152 memcpy( aData.getArray(), pCursor, nBytesToRead );
153
154 mnCursor += nBytesToRead;
155 }
156
157 return nBytesToRead;
158}
159
160sal_Int32 SAL_CALL UNOMemoryStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
161{
162 return readBytes( aData, nMaxBytesToRead );
163}
164
165void SAL_CALL UNOMemoryStream::skipBytes( sal_Int32 nBytesToSkip )
166{
167 if( nBytesToSkip < 0 )
168 throw IOException("nBytesToSkip < 0");
169
170 mnCursor += std::min( nBytesToSkip, available() );
171}
172
173sal_Int32 SAL_CALL UNOMemoryStream::available()
174{
175 return std::min<sal_Int64>( SAL_MAX_INT32, maData.size() - mnCursor);
176}
177
178void SAL_CALL UNOMemoryStream::closeInput()
179{
180 mnCursor = 0;
181}
182
183// XSeekable
184void SAL_CALL UNOMemoryStream::seek( sal_Int64 location )
185{
186 if( (location < 0) || (location > SAL_MAX_INT32) )
187 throw IllegalArgumentException("this implementation does not support more than 2GB!", static_cast<OWeakObject*>(this), 0 );
188
189 // seek operation should be able to resize the stream
190 if ( o3tl::make_unsigned(location) > maData.size() )
191 maData.resize( static_cast< sal_Int32 >( location ) );
192
193 mnCursor = static_cast< sal_Int32 >( location );
194}
195
196sal_Int64 SAL_CALL UNOMemoryStream::getPosition()
197{
198 return static_cast< sal_Int64 >( mnCursor );
199}
200
201sal_Int64 SAL_CALL UNOMemoryStream::getLength()
202{
203 return static_cast< sal_Int64 >( maData.size() );
204}
205
206// XOutputStream
207void SAL_CALL UNOMemoryStream::writeBytes( const Sequence< sal_Int8 >& aData )
208{
209 writeBytes(aData.getConstArray(), aData.getLength());
210}
211
212void UNOMemoryStream::writeBytes( const sal_Int8* pInData, sal_Int32 nBytesToWrite )
213{
214 assert(nBytesToWrite >= 0);
215 if( !nBytesToWrite )
216 return;
217
218 sal_Int64 nNewSize = static_cast<sal_Int64>(mnCursor) + nBytesToWrite;
219 if( nNewSize > SAL_MAX_INT32 )
220 {
221 OSL_ASSERT(false);
222 throw IOException("this implementation does not support more than 2GB!", static_cast<OWeakObject*>(this) );
223 }
224
225 if( o3tl::make_unsigned( nNewSize ) > maData.size() )
226 maData.resize( nNewSize );
227
228 sal_Int8* pData = &(*maData.begin());
229 sal_Int8* pCursor = &(pData[mnCursor]);
230 memcpy(pCursor, pInData, nBytesToWrite);
231
232 mnCursor += nBytesToWrite;
233}
234
235void SAL_CALL UNOMemoryStream::flush()
236{
237}
238
239void SAL_CALL UNOMemoryStream::closeOutput()
240{
241 mnCursor = 0;
242}
243
244//XTruncate
245void SAL_CALL UNOMemoryStream::truncate()
246{
247 maData.clear();
248 mnCursor = 0;
249}
250
251sal_Int64 SAL_CALL UNOMemoryStream::getSomething( const css::uno::Sequence< sal_Int8 >& rIdentifier )
252{
253 if (rIdentifier == comphelper::ByteWriter::getUnoTunnelId())
254 return reinterpret_cast<sal_Int64>(static_cast<comphelper::ByteWriter*>(this));
255 return 0;
256}
257
258} // namespace comphelper
259
260extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
262 css::uno::XComponentContext *,
263 css::uno::Sequence<css::uno::Any> const &)
264{
265 return cppu::acquire(new ::comphelper::UNOMemoryStream());
266}
267
268/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Interface that we can cast to, to bypass the inefficiency of using Sequence<sal_Int8> when writing vi...
Definition: bytereader.hxx:34
static const css::uno::Sequence< sal_Int8 > & getUnoTunnelId()
Definition: basicio.cxx:172
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