Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / unotools / source / ucbhelper / xtempfile.cxx
blob24213db368a8027dcfd803a4b5fc1d1772d123d2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "XTempFile.hxx"
21 #include <unotoolsservices.hxx>
22 #include <com/sun/star/io/BufferSizeExceededException.hpp>
23 #include <com/sun/star/io/NotConnectedException.hpp>
24 #include <com/sun/star/beans/PropertyAttribute.hpp>
25 #include <cppuhelper/typeprovider.hxx>
26 #include <comphelper/servicedecl.hxx>
27 #include <unotools/tempfile.hxx>
28 #include <cppuhelper/propshlp.hxx>
30 OTempFileService::OTempFileService(css::uno::Reference< css::uno::XComponentContext > const &)
31 : mpStream( nullptr )
32 , mbRemoveFile( true )
33 , mbInClosed( false )
34 , mbOutClosed( false )
35 , mnCachedPos( 0 )
36 , mbHasCachedPos( false )
39 mpTempFile.reset(new utl::TempFile());
40 mpTempFile->EnableKillingFile();
43 OTempFileService::~OTempFileService ()
47 // XTypeProvider
49 css::uno::Sequence< css::uno::Type > SAL_CALL OTempFileService::getTypes( )
51 static ::cppu::OTypeCollection ourTypeCollection(
52 cppu::UnoType<css::beans::XPropertySet>::get()
53 ,OTempFileBase::getTypes() );
55 return ourTypeCollection.getTypes();
58 // XTempFile
60 sal_Bool SAL_CALL OTempFileService::getRemoveFile()
62 ::osl::MutexGuard aGuard( maMutex );
64 if ( !mpTempFile )
66 // the stream is already disconnected
67 throw css::uno::RuntimeException();
70 return mbRemoveFile;
72 void SAL_CALL OTempFileService::setRemoveFile( sal_Bool _removefile )
74 ::osl::MutexGuard aGuard( maMutex );
76 if ( !mpTempFile )
78 // the stream is already disconnected
79 throw css::uno::RuntimeException();
82 mbRemoveFile = _removefile;
83 mpTempFile->EnableKillingFile( mbRemoveFile );
85 OUString SAL_CALL OTempFileService::getUri()
87 ::osl::MutexGuard aGuard( maMutex );
89 if ( !mpTempFile )
91 throw css::uno::RuntimeException();
94 return mpTempFile->GetURL();
97 OUString SAL_CALL OTempFileService::getResourceName()
99 ::osl::MutexGuard aGuard( maMutex );
101 if ( !mpTempFile )
103 throw css::uno::RuntimeException();
106 return mpTempFile->GetFileName();
109 // XInputStream
111 sal_Int32 SAL_CALL OTempFileService::readBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
113 ::osl::MutexGuard aGuard( maMutex );
114 if ( mbInClosed )
115 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) );
117 checkConnected();
118 if (nBytesToRead < 0)
119 throw css::io::BufferSizeExceededException( OUString(), static_cast< css::uno::XWeak * >(this));
121 if (aData.getLength() < nBytesToRead)
122 aData.realloc(nBytesToRead);
124 sal_uInt32 nRead = mpStream->ReadBytes(static_cast<void*>(aData.getArray()), nBytesToRead);
125 checkError();
127 if (nRead < static_cast<std::size_t>(aData.getLength()))
128 aData.realloc( nRead );
130 if ( sal::static_int_cast<sal_uInt32>(nBytesToRead) > nRead )
132 // usually that means that the stream was read till the end
133 // TODO/LATER: it is better to get rid of this optimization by avoiding using of multiple temporary files ( there should be only one temporary file? )
134 mnCachedPos = mpStream->Tell();
135 mbHasCachedPos = true;
137 mpStream = nullptr;
138 if ( mpTempFile )
139 mpTempFile->CloseStream();
142 return nRead;
144 sal_Int32 SAL_CALL OTempFileService::readSomeBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
146 ::osl::MutexGuard aGuard( maMutex );
147 if ( mbInClosed )
148 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) );
150 checkConnected();
151 checkError();
153 if (nMaxBytesToRead < 0)
154 throw css::io::BufferSizeExceededException( OUString(), static_cast < css::uno::XWeak * >( this ) );
156 if (mpStream->eof())
158 aData.realloc(0);
159 return 0;
161 else
162 return readBytes(aData, nMaxBytesToRead);
164 void SAL_CALL OTempFileService::skipBytes( sal_Int32 nBytesToSkip )
166 ::osl::MutexGuard aGuard( maMutex );
167 if ( mbInClosed )
168 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) );
170 checkConnected();
171 checkError();
172 mpStream->SeekRel(nBytesToSkip);
173 checkError();
175 sal_Int32 SAL_CALL OTempFileService::available( )
177 ::osl::MutexGuard aGuard( maMutex );
178 if ( mbInClosed )
179 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) );
181 checkConnected();
183 sal_Int64 nAvailable = mpStream->remainingSize();
184 checkError();
186 return std::min<sal_Int64>(SAL_MAX_INT32, nAvailable);
188 void SAL_CALL OTempFileService::closeInput( )
190 ::osl::MutexGuard aGuard( maMutex );
191 if ( mbInClosed )
192 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) );
194 mbInClosed = true;
196 if ( mbOutClosed )
198 // stream will be deleted by TempFile implementation
199 mpStream = nullptr;
200 mpTempFile.reset();
204 // XOutputStream
206 void SAL_CALL OTempFileService::writeBytes( const css::uno::Sequence< sal_Int8 >& aData )
208 ::osl::MutexGuard aGuard( maMutex );
209 if ( mbOutClosed )
210 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) );
212 checkConnected();
213 sal_uInt32 nWritten = mpStream->WriteBytes(aData.getConstArray(), aData.getLength());
214 checkError();
215 if ( nWritten != static_cast<sal_uInt32>(aData.getLength()))
216 throw css::io::BufferSizeExceededException( OUString(),static_cast < css::uno::XWeak * > ( this ) );
218 void SAL_CALL OTempFileService::flush( )
220 ::osl::MutexGuard aGuard( maMutex );
221 if ( mbOutClosed )
222 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) );
224 checkConnected();
225 mpStream->Flush();
226 checkError();
228 void SAL_CALL OTempFileService::closeOutput( )
230 ::osl::MutexGuard aGuard( maMutex );
231 if ( mbOutClosed )
232 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) );
234 mbOutClosed = true;
236 // TODO/LATER: it is better to get rid of this optimization by avoiding using of multiple temporary files ( there should be only one temporary file? )
237 if ( mpStream )
239 mnCachedPos = mpStream->Tell();
240 mbHasCachedPos = true;
242 mpStream = nullptr;
243 if ( mpTempFile )
244 mpTempFile->CloseStream();
247 if ( mbInClosed )
249 // stream will be deleted by TempFile implementation
250 mpStream = nullptr;
251 mpTempFile.reset();
255 void OTempFileService::checkError () const
257 if (!mpStream || mpStream->SvStream::GetError () != ERRCODE_NONE )
258 throw css::io::NotConnectedException ( OUString(), const_cast < css::uno::XWeak * > ( static_cast < const css::uno::XWeak * > (this ) ) );
260 void OTempFileService::checkConnected ()
262 if (!mpStream && mpTempFile)
264 mpStream = mpTempFile->GetStream( StreamMode::STD_READWRITE );
265 if ( mpStream && mbHasCachedPos )
267 mpStream->Seek( sal::static_int_cast<std::size_t>(mnCachedPos) );
268 if ( mpStream->SvStream::GetError () == ERRCODE_NONE )
270 mbHasCachedPos = false;
271 mnCachedPos = 0;
273 else
275 mpStream = nullptr;
276 mpTempFile->CloseStream();
281 if (!mpStream)
282 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak * > (this ) );
285 // XSeekable
287 void SAL_CALL OTempFileService::seek( sal_Int64 nLocation )
289 ::osl::MutexGuard aGuard( maMutex );
290 checkConnected();
291 if ( nLocation < 0 || nLocation > getLength() )
292 throw css::lang::IllegalArgumentException();
294 mpStream->Seek(static_cast<sal_uInt32>(nLocation) );
295 checkError();
297 sal_Int64 SAL_CALL OTempFileService::getPosition( )
299 ::osl::MutexGuard aGuard( maMutex );
300 checkConnected();
302 sal_uInt32 nPos = mpStream->Tell();
303 checkError();
304 return static_cast<sal_Int64>(nPos);
306 sal_Int64 SAL_CALL OTempFileService::getLength( )
308 ::osl::MutexGuard aGuard( maMutex );
309 checkConnected();
311 checkError();
313 sal_Int64 nEndPos = mpStream->TellEnd();
315 return nEndPos;
318 // XStream
320 css::uno::Reference< css::io::XInputStream > SAL_CALL OTempFileService::getInputStream()
322 return css::uno::Reference< css::io::XInputStream >( *this, css::uno::UNO_QUERY );
325 css::uno::Reference< css::io::XOutputStream > SAL_CALL OTempFileService::getOutputStream()
327 return css::uno::Reference< css::io::XOutputStream >( *this, css::uno::UNO_QUERY );
330 // XTruncate
332 void SAL_CALL OTempFileService::truncate()
334 ::osl::MutexGuard aGuard( maMutex );
335 checkConnected();
336 // SetStreamSize() call does not change the position
337 mpStream->Seek( 0 );
338 mpStream->SetStreamSize( 0 );
339 checkError();
342 #define PROPERTY_HANDLE_URI 1
343 #define PROPERTY_HANDLE_REMOVE_FILE 2
344 #define PROPERTY_HANDLE_RESOURCE_NAME 3
346 // XPropertySet
347 ::css::uno::Reference< ::css::beans::XPropertySetInfo > OTempFileService::getPropertySetInfo()
349 // Create a table that map names to index values.
350 // attention: properties need to be sorted by name!
351 static cppu::OPropertyArrayHelper ourPropertyInfo(
353 css::beans::Property( "Uri", PROPERTY_HANDLE_URI, cppu::UnoType<OUString>::get(),
354 css::beans::PropertyAttribute::READONLY ),
355 css::beans::Property( "RemoveFile", PROPERTY_HANDLE_REMOVE_FILE, cppu::UnoType<bool>::get(),
356 0 ),
357 css::beans::Property( "ResourceName", PROPERTY_HANDLE_RESOURCE_NAME, cppu::UnoType<OUString>::get(),
358 css::beans::PropertyAttribute::READONLY )
360 true );
361 static css::uno::Reference< css::beans::XPropertySetInfo > xInfo(
362 ::cppu::OPropertySetHelper::createPropertySetInfo( ourPropertyInfo ) );
363 return xInfo;
365 void OTempFileService::setPropertyValue( const ::rtl::OUString& aPropertyName, const ::css::uno::Any& aValue )
367 if ( aPropertyName == "RemoveFile" )
368 setRemoveFile( aValue.get<bool>() );
369 else
371 assert(false);
372 throw css::beans::UnknownPropertyException(aPropertyName);
375 ::css::uno::Any OTempFileService::getPropertyValue( const ::rtl::OUString& aPropertyName )
377 if ( aPropertyName == "RemoveFile" )
378 return css::uno::Any(getRemoveFile());
379 else if ( aPropertyName == "ResourceName" )
380 return css::uno::Any(getResourceName());
381 else if ( aPropertyName == "Uri" )
382 return css::uno::Any(getUri());
383 else
385 assert(false);
386 throw css::beans::UnknownPropertyException(aPropertyName);
389 void OTempFileService::addPropertyChangeListener( const ::rtl::OUString& /*aPropertyName*/, const ::css::uno::Reference< ::css::beans::XPropertyChangeListener >& /*xListener*/ )
391 assert(false);
393 void OTempFileService::removePropertyChangeListener( const ::rtl::OUString& /*aPropertyName*/, const ::css::uno::Reference< ::css::beans::XPropertyChangeListener >& /*xListener*/ )
395 assert(false);
397 void OTempFileService::addVetoableChangeListener( const ::rtl::OUString& /*aPropertyName*/, const ::css::uno::Reference< ::css::beans::XVetoableChangeListener >& /*xListener*/ )
399 assert(false);
401 void OTempFileService::removeVetoableChangeListener( const ::rtl::OUString& /*aPropertyName*/, const ::css::uno::Reference< ::css::beans::XVetoableChangeListener >& /*xListener*/ )
403 assert(false);
405 // XFastPropertySet
406 void OTempFileService::setFastPropertyValue( ::sal_Int32 nHandle, const ::css::uno::Any& aValue )
408 switch (nHandle)
410 case PROPERTY_HANDLE_REMOVE_FILE: setRemoveFile( aValue.get<bool>() ); return;
412 assert(false);
413 throw css::beans::UnknownPropertyException(OUString::number(nHandle));
415 ::css::uno::Any OTempFileService::getFastPropertyValue( ::sal_Int32 nHandle )
417 switch (nHandle)
419 case PROPERTY_HANDLE_REMOVE_FILE: return css::uno::Any(getRemoveFile());
420 case PROPERTY_HANDLE_RESOURCE_NAME: return css::uno::Any(getResourceName());
421 case PROPERTY_HANDLE_URI: return css::uno::Any(getUri());
423 assert(false);
424 throw css::beans::UnknownPropertyException(OUString::number(nHandle));
426 // XPropertyAccess
427 ::css::uno::Sequence< ::css::beans::PropertyValue > OTempFileService::getPropertyValues()
429 return {
430 css::beans::PropertyValue("Uri", PROPERTY_HANDLE_URI, css::uno::Any(getUri()), css::beans::PropertyState_DEFAULT_VALUE),
431 css::beans::PropertyValue("RemoveFile", PROPERTY_HANDLE_REMOVE_FILE, css::uno::Any(getRemoveFile()), css::beans::PropertyState_DEFAULT_VALUE),
432 css::beans::PropertyValue("ResourceName", PROPERTY_HANDLE_RESOURCE_NAME, css::uno::Any(getResourceName()), css::beans::PropertyState_DEFAULT_VALUE)
435 void OTempFileService::setPropertyValues( const ::css::uno::Sequence< ::css::beans::PropertyValue >& aProps )
437 for ( auto const & rPropVal : aProps )
438 setPropertyValue( rPropVal.Name, rPropVal.Value );
441 namespace sdecl = ::comphelper::service_decl;
442 sdecl::class_< OTempFileService> const OTempFileServiceImpl;
443 const sdecl::ServiceDecl OTempFileServiceDecl(
444 OTempFileServiceImpl,
445 "com.sun.star.io.comp.TempFile",
446 "com.sun.star.io.TempFile");
448 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */