LibreOffice Module unotools (master) 1
xtempfile.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 "XTempFile.hxx"
21#include <com/sun/star/io/BufferSizeExceededException.hpp>
22#include <com/sun/star/io/NotConnectedException.hpp>
23#include <com/sun/star/beans/PropertyAttribute.hpp>
25#include <o3tl/safeint.hxx>
26#include <unotools/tempfile.hxx>
29
30OTempFileService::OTempFileService(css::uno::Reference< css::uno::XComponentContext > const &)
31: mpStream( nullptr )
32, mbRemoveFile( true )
33, mbInClosed( false )
34, mbOutClosed( false )
35{
36 mpTempFile.emplace();
37 mpTempFile->EnableKillingFile();
38}
39
41{
42}
43
44// XTypeProvider
45
46css::uno::Sequence< css::uno::Type > SAL_CALL OTempFileService::getTypes( )
47{
48 static ::cppu::OTypeCollection ourTypeCollection(
50 ,OTempFileBase::getTypes() );
51
52 return ourTypeCollection.getTypes();
53};
54
55// XTempFile
56
58{
59 std::unique_lock aGuard( maMutex );
60
61 if ( !mpTempFile )
62 {
63 // the stream is already disconnected
64 throw css::uno::RuntimeException("Not connected to a file.");
65 }
66
67 return mbRemoveFile;
68};
69void SAL_CALL OTempFileService::setRemoveFile( sal_Bool _removefile )
70{
71 std::unique_lock aGuard( maMutex );
72
73 if ( !mpTempFile )
74 {
75 // the stream is already disconnected
76 throw css::uno::RuntimeException("Not connected to a file.");
77 }
78
79 mbRemoveFile = _removefile;
80 mpTempFile->EnableKillingFile( mbRemoveFile );
81};
82OUString SAL_CALL OTempFileService::getUri()
83{
84 std::unique_lock aGuard( maMutex );
85
86 if ( !mpTempFile )
87 {
88 throw css::uno::RuntimeException("Not connected to a file.");
89 }
90
91 return mpTempFile->GetURL();
92
93};
95{
96 std::unique_lock aGuard( maMutex );
97
98 if ( !mpTempFile )
99 {
100 throw css::uno::RuntimeException("Not connected to a file.");
101 }
102
103 return mpTempFile->GetFileName();
104};
105
106// XInputStream
107
108sal_Int32 SAL_CALL OTempFileService::readBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
109{
110 std::unique_lock aGuard( maMutex );
111 if ( mbInClosed )
112 throw css::io::NotConnectedException ( OUString(), getXWeak() );
113
115 if (nBytesToRead < 0)
116 throw css::io::BufferSizeExceededException( OUString(), getXWeak());
117
118 if (aData.getLength() < nBytesToRead)
119 aData.realloc(nBytesToRead);
120
121 sal_uInt32 nRead = mpStream->ReadBytes(static_cast<void*>(aData.getArray()), nBytesToRead);
122 checkError();
123
124 if (nRead < o3tl::make_unsigned(aData.getLength()))
125 aData.realloc( nRead );
126
127 return nRead;
128}
129sal_Int32 SAL_CALL OTempFileService::readSomeBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
130{
131 {
132 std::unique_lock aGuard( maMutex );
133 if ( mbInClosed )
134 throw css::io::NotConnectedException ( OUString(), getXWeak() );
135
137 checkError();
138
139 if (nMaxBytesToRead < 0)
140 throw css::io::BufferSizeExceededException( OUString(), getXWeak() );
141
142 if (mpStream->eof())
143 {
144 aData.realloc(0);
145 return 0;
146 }
147 }
148 return readBytes(aData, nMaxBytesToRead);
149}
150void SAL_CALL OTempFileService::skipBytes( sal_Int32 nBytesToSkip )
151{
152 std::unique_lock aGuard( maMutex );
153 if ( mbInClosed )
154 throw css::io::NotConnectedException ( OUString(), getXWeak() );
155
157 checkError();
158 mpStream->SeekRel(nBytesToSkip);
159 checkError();
160}
161sal_Int32 SAL_CALL OTempFileService::available( )
162{
163 std::unique_lock aGuard( maMutex );
164 if ( mbInClosed )
165 throw css::io::NotConnectedException ( OUString(), getXWeak() );
166
168
169 sal_Int64 nAvailable = mpStream->remainingSize();
170 checkError();
171
172 return std::min<sal_Int64>(SAL_MAX_INT32, nAvailable);
173}
175{
176 std::unique_lock aGuard( maMutex );
177 if ( mbInClosed )
178 throw css::io::NotConnectedException ( OUString(), getXWeak() );
179
180 mbInClosed = true;
181
182 if ( mbOutClosed )
183 {
184 // stream will be deleted by TempFile implementation
185 mpStream = nullptr;
186 mpTempFile.reset();
187 }
188}
189
190// XOutputStream
191
192void SAL_CALL OTempFileService::writeBytes( const css::uno::Sequence< sal_Int8 >& aData )
193{
194 std::unique_lock aGuard( maMutex );
195 if ( mbOutClosed )
196 throw css::io::NotConnectedException ( OUString(), getXWeak() );
197
199 sal_uInt32 nWritten = mpStream->WriteBytes(aData.getConstArray(), aData.getLength());
200 checkError();
201 if ( nWritten != static_cast<sal_uInt32>(aData.getLength()))
202 throw css::io::BufferSizeExceededException( OUString(), getXWeak() );
203}
205{
206 std::unique_lock aGuard( maMutex );
207 if ( mbOutClosed )
208 throw css::io::NotConnectedException ( OUString(), getXWeak() );
209
211 mpStream->Flush();
212 checkError();
213}
215{
216 std::unique_lock aGuard( maMutex );
217 if ( mbOutClosed )
218 throw css::io::NotConnectedException ( OUString(), getXWeak() );
219
220 mbOutClosed = true;
221 if (mpStream)
222 {
223 // so that if you then open the InputStream, you can read the content
225 mpStream->Seek(0);
226 }
227
228 if ( mbInClosed )
229 {
230 // stream will be deleted by TempFile implementation
231 mpStream = nullptr;
232 mpTempFile.reset();
233 }
234}
235
237{
238 if (!mpStream || mpStream->SvStream::GetError () != ERRCODE_NONE )
239 throw css::io::NotConnectedException ( OUString(), const_cast < OTempFileService * > (this)->getXWeak() );
240}
242{
243 if (!mpStream && mpTempFile)
244 {
245 // Ideally we should open this SHARE_DENYALL, but the JunitTest_unotools_complex test wants to open
246 // this file directly and read from it.
247 mpStream = mpTempFile->GetStream(StreamMode::READ | StreamMode::WRITE
248 | StreamMode::SHARE_DENYWRITE);
249 }
250
251 if (!mpStream)
252 throw css::io::NotConnectedException ( OUString(), getXWeak() );
253}
254
255// XSeekable
256
257void SAL_CALL OTempFileService::seek( sal_Int64 nLocation )
258{
259 std::unique_lock aGuard( maMutex );
261 checkError();
262 sal_Int64 nEndPos = mpStream->TellEnd();
263 if ( nLocation < 0 || nLocation > nEndPos )
264 throw css::lang::IllegalArgumentException();
265
266 mpStream->Seek(static_cast<sal_uInt32>(nLocation) );
267 checkError();
268}
269sal_Int64 SAL_CALL OTempFileService::getPosition( )
270{
271 std::unique_lock aGuard( maMutex );
273
274 sal_uInt32 nPos = mpStream->Tell();
275 checkError();
276 return static_cast<sal_Int64>(nPos);
277}
278sal_Int64 SAL_CALL OTempFileService::getLength( )
279{
280 std::unique_lock aGuard( maMutex );
282
283 checkError();
284
285 sal_Int64 nEndPos = mpStream->TellEnd();
286
287 return nEndPos;
288}
289
290// XStream
291
292css::uno::Reference< css::io::XInputStream > SAL_CALL OTempFileService::getInputStream()
293{
294 return this;
295}
296
297css::uno::Reference< css::io::XOutputStream > SAL_CALL OTempFileService::getOutputStream()
298{
299 return this;
300}
301
302// XTruncate
303
305{
306 std::unique_lock aGuard( maMutex );
308 // SetStreamSize() call does not change the position
309 mpStream->Seek( 0 );
311 checkError();
312}
313
314#define PROPERTY_HANDLE_URI 1
315#define PROPERTY_HANDLE_REMOVE_FILE 2
316#define PROPERTY_HANDLE_RESOURCE_NAME 3
317
318// XPropertySet
319::css::uno::Reference< ::css::beans::XPropertySetInfo > OTempFileService::getPropertySetInfo()
320{
321 // Create a table that map names to index values.
322 // attention: properties need to be sorted by name!
323 static cppu::OPropertyArrayHelper ourPropertyInfo(
324 {
325 css::beans::Property( "Uri", PROPERTY_HANDLE_URI, cppu::UnoType<OUString>::get(),
326 css::beans::PropertyAttribute::READONLY ),
327 css::beans::Property( "RemoveFile", PROPERTY_HANDLE_REMOVE_FILE, cppu::UnoType<bool>::get(),
328 0 ),
329 css::beans::Property( "ResourceName", PROPERTY_HANDLE_RESOURCE_NAME, cppu::UnoType<OUString>::get(),
330 css::beans::PropertyAttribute::READONLY )
331 },
332 true );
333 static css::uno::Reference< css::beans::XPropertySetInfo > xInfo(
335 return xInfo;
336}
337void OTempFileService::setPropertyValue( const ::rtl::OUString& aPropertyName, const ::css::uno::Any& aValue )
338{
339 if ( aPropertyName == "RemoveFile" )
340 setRemoveFile( aValue.get<bool>() );
341 else
342 {
343 assert(false);
344 throw css::beans::UnknownPropertyException(aPropertyName);
345 }
346}
347::css::uno::Any OTempFileService::getPropertyValue( const ::rtl::OUString& aPropertyName )
348{
349 if ( aPropertyName == "RemoveFile" )
350 return css::uno::Any(getRemoveFile());
351 else if ( aPropertyName == "ResourceName" )
352 return css::uno::Any(getResourceName());
353 else if ( aPropertyName == "Uri" )
354 return css::uno::Any(getUri());
355 else
356 {
357 assert(false);
358 throw css::beans::UnknownPropertyException(aPropertyName);
359 }
360}
361void OTempFileService::addPropertyChangeListener( const ::rtl::OUString& /*aPropertyName*/, const ::css::uno::Reference< ::css::beans::XPropertyChangeListener >& /*xListener*/ )
362{
363 assert(false);
364}
365void OTempFileService::removePropertyChangeListener( const ::rtl::OUString& /*aPropertyName*/, const ::css::uno::Reference< ::css::beans::XPropertyChangeListener >& /*xListener*/ )
366{
367 assert(false);
368}
369void OTempFileService::addVetoableChangeListener( const ::rtl::OUString& /*aPropertyName*/, const ::css::uno::Reference< ::css::beans::XVetoableChangeListener >& /*xListener*/ )
370{
371 assert(false);
372}
373void OTempFileService::removeVetoableChangeListener( const ::rtl::OUString& /*aPropertyName*/, const ::css::uno::Reference< ::css::beans::XVetoableChangeListener >& /*xListener*/ )
374{
375 assert(false);
376}
377// XFastPropertySet
378void OTempFileService::setFastPropertyValue( ::sal_Int32 nHandle, const ::css::uno::Any& aValue )
379{
380 switch (nHandle)
381 {
382 case PROPERTY_HANDLE_REMOVE_FILE: setRemoveFile( aValue.get<bool>() ); return;
383 }
384 assert(false);
385 throw css::beans::UnknownPropertyException(OUString::number(nHandle));
386}
387::css::uno::Any OTempFileService::getFastPropertyValue( ::sal_Int32 nHandle )
388{
389 switch (nHandle)
390 {
391 case PROPERTY_HANDLE_REMOVE_FILE: return css::uno::Any(getRemoveFile());
392 case PROPERTY_HANDLE_RESOURCE_NAME: return css::uno::Any(getResourceName());
393 case PROPERTY_HANDLE_URI: return css::uno::Any(getUri());
394 }
395 assert(false);
396 throw css::beans::UnknownPropertyException(OUString::number(nHandle));
397}
398// XPropertyAccess
399::css::uno::Sequence< ::css::beans::PropertyValue > OTempFileService::getPropertyValues()
400{
401 return {
402 css::beans::PropertyValue("Uri", PROPERTY_HANDLE_URI, css::uno::Any(getUri()), css::beans::PropertyState_DEFAULT_VALUE),
403 css::beans::PropertyValue("RemoveFile", PROPERTY_HANDLE_REMOVE_FILE, css::uno::Any(getRemoveFile()), css::beans::PropertyState_DEFAULT_VALUE),
404 css::beans::PropertyValue("ResourceName", PROPERTY_HANDLE_RESOURCE_NAME, css::uno::Any(getResourceName()), css::beans::PropertyState_DEFAULT_VALUE)
405 };
406}
407void OTempFileService::setPropertyValues( const ::css::uno::Sequence< ::css::beans::PropertyValue >& aProps )
408{
409 for ( auto const & rPropVal : aProps )
410 setPropertyValue( rPropVal.Name, rPropVal.Value );
411}
412
413// XServiceInfo
414sal_Bool OTempFileService::supportsService(const OUString& sServiceName)
415{
417}
419{
420 return "com.sun.star.io.comp.TempFile";
421}
422css::uno::Sequence< OUString > OTempFileService::getSupportedServiceNames()
423{
424 return { "com.sun.star.io.TempFile" };
425}
426
427extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
429 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
430{
431 return cppu::acquire(new OTempFileService(context));
432}
433
434
435/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral sServiceName
virtual void SAL_CALL skipBytes(::sal_Int32 nBytesToSkip) override
Definition: xtempfile.cxx:150
virtual void SAL_CALL addVetoableChangeListener(const ::rtl::OUString &PropertyName, const ::css::uno::Reference< ::css::beans::XVetoableChangeListener > &aListener) override
Definition: xtempfile.cxx:369
virtual void SAL_CALL setRemoveFile(sal_Bool _removefile) override
Definition: xtempfile.cxx:69
virtual sal_Int64 SAL_CALL getPosition() override
Definition: xtempfile.cxx:269
virtual ::css::uno::Any SAL_CALL getFastPropertyValue(::sal_Int32 nHandle) override
Definition: xtempfile.cxx:387
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
Definition: xtempfile.cxx:46
SvStream * mpStream
Definition: XTempFile.hxx:53
virtual void SAL_CALL closeInput() override
Definition: xtempfile.cxx:174
virtual OUString SAL_CALL getUri() override
Definition: xtempfile.cxx:82
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: xtempfile.cxx:422
virtual ::css::uno::Any SAL_CALL getPropertyValue(const ::rtl::OUString &PropertyName) override
Definition: xtempfile.cxx:347
virtual ::sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< ::sal_Int8 > &aData, ::sal_Int32 nMaxBytesToRead) override
Definition: xtempfile.cxx:129
virtual void SAL_CALL setFastPropertyValue(::sal_Int32 nHandle, const ::css::uno::Any &aValue) override
Definition: xtempfile.cxx:378
virtual void SAL_CALL closeOutput() override
Definition: xtempfile.cxx:214
virtual ::css::uno::Reference< ::css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
Definition: xtempfile.cxx:319
std::mutex maMutex
Definition: XTempFile.hxx:52
virtual void SAL_CALL addPropertyChangeListener(const ::rtl::OUString &aPropertyName, const ::css::uno::Reference< ::css::beans::XPropertyChangeListener > &xListener) override
Definition: xtempfile.cxx:361
virtual ::css::uno::Sequence< ::css::beans::PropertyValue > SAL_CALL getPropertyValues() override
Definition: xtempfile.cxx:399
virtual void SAL_CALL seek(sal_Int64 location) override
Definition: xtempfile.cxx:257
virtual ::sal_Int32 SAL_CALL available() override
Definition: xtempfile.cxx:161
void checkConnected()
Definition: xtempfile.cxx:241
virtual css::uno::Reference< css::io::XOutputStream > SAL_CALL getOutputStream() override
Definition: xtempfile.cxx:297
virtual OUString SAL_CALL getImplementationName() override
Definition: xtempfile.cxx:418
std::optional< utl::TempFileNamed > mpTempFile
Definition: XTempFile.hxx:51
virtual void SAL_CALL removeVetoableChangeListener(const ::rtl::OUString &PropertyName, const ::css::uno::Reference< ::css::beans::XVetoableChangeListener > &aListener) override
Definition: xtempfile.cxx:373
virtual void SAL_CALL flush() override
Definition: xtempfile.cxx:204
OTempFileService(css::uno::Reference< css::uno::XComponentContext > const &context)
Definition: xtempfile.cxx:30
virtual OUString SAL_CALL getResourceName() override
Definition: xtempfile.cxx:94
virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getInputStream() override
Definition: xtempfile.cxx:292
virtual sal_Bool SAL_CALL supportsService(const OUString &sServiceName) override
Definition: xtempfile.cxx:414
virtual void SAL_CALL writeBytes(const css::uno::Sequence< ::sal_Int8 > &aData) override
Definition: xtempfile.cxx:192
void checkError() const
Definition: xtempfile.cxx:236
virtual sal_Int64 SAL_CALL getLength() override
Definition: xtempfile.cxx:278
virtual ::sal_Int32 SAL_CALL readBytes(css::uno::Sequence< ::sal_Int8 > &aData, ::sal_Int32 nBytesToRead) override
Definition: xtempfile.cxx:108
virtual void SAL_CALL removePropertyChangeListener(const ::rtl::OUString &aPropertyName, const ::css::uno::Reference< ::css::beans::XPropertyChangeListener > &aListener) override
Definition: xtempfile.cxx:365
virtual void SAL_CALL setPropertyValues(const ::css::uno::Sequence< ::css::beans::PropertyValue > &aProps) override
Definition: xtempfile.cxx:407
virtual sal_Bool SAL_CALL getRemoveFile() override
Definition: xtempfile.cxx:57
virtual void SAL_CALL truncate() override
Definition: xtempfile.cxx:304
virtual void SAL_CALL setPropertyValue(const ::rtl::OUString &aPropertyName, const ::css::uno::Any &aValue) override
Definition: xtempfile.cxx:337
virtual ~OTempFileService() override
Definition: xtempfile.cxx:40
sal_uInt64 Tell() const
virtual sal_uInt64 TellEnd()
std::size_t WriteBytes(const void *pData, std::size_t nSize)
bool eof() const
bool SetStreamSize(sal_uInt64 nSize)
sal_uInt64 Seek(sal_uInt64 nPos)
void Flush()
std::size_t ReadBytes(void *pData, std::size_t nSize)
sal_uInt64 SeekRel(sal_Int64 nPos)
void FlushBuffer()
sal_uInt64 remainingSize()
static css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL createPropertySetInfo(IPropertyArrayHelper &rProperties)
#define ERRCODE_NONE
sal_uInt16 nPos
constexpr OUStringLiteral aData
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)
sal_Int32 nHandle
#define SAL_MAX_INT32
unsigned char sal_Bool
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * unotools_OTempFileService_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: xtempfile.cxx:428
#define PROPERTY_HANDLE_REMOVE_FILE
Definition: xtempfile.cxx:315
#define PROPERTY_HANDLE_RESOURCE_NAME
Definition: xtempfile.cxx:316
#define PROPERTY_HANDLE_URI
Definition: xtempfile.cxx:314