1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 &)
32 , mbRemoveFile( true )
34 , mbOutClosed( false )
36 , mbHasCachedPos( false )
39 mpTempFile
.reset(new utl::TempFile());
40 mpTempFile
->EnableKillingFile();
43 OTempFileService::~OTempFileService ()
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();
60 sal_Bool SAL_CALL
OTempFileService::getRemoveFile()
62 ::osl::MutexGuard
aGuard( maMutex
);
66 // the stream is already disconnected
67 throw css::uno::RuntimeException();
72 void SAL_CALL
OTempFileService::setRemoveFile( sal_Bool _removefile
)
74 ::osl::MutexGuard
aGuard( maMutex
);
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
);
91 throw css::uno::RuntimeException();
94 return mpTempFile
->GetURL();
97 OUString SAL_CALL
OTempFileService::getResourceName()
99 ::osl::MutexGuard
aGuard( maMutex
);
103 throw css::uno::RuntimeException();
106 return mpTempFile
->GetFileName();
111 sal_Int32 SAL_CALL
OTempFileService::readBytes( css::uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
113 ::osl::MutexGuard
aGuard( maMutex
);
115 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak
* > (this ) );
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
);
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;
139 mpTempFile
->CloseStream();
144 sal_Int32 SAL_CALL
OTempFileService::readSomeBytes( css::uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
146 ::osl::MutexGuard
aGuard( maMutex
);
148 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak
* > (this ) );
153 if (nMaxBytesToRead
< 0)
154 throw css::io::BufferSizeExceededException( OUString(), static_cast < css::uno::XWeak
* >( this ) );
162 return readBytes(aData
, nMaxBytesToRead
);
164 void SAL_CALL
OTempFileService::skipBytes( sal_Int32 nBytesToSkip
)
166 ::osl::MutexGuard
aGuard( maMutex
);
168 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak
* > (this ) );
172 mpStream
->SeekRel(nBytesToSkip
);
175 sal_Int32 SAL_CALL
OTempFileService::available( )
177 ::osl::MutexGuard
aGuard( maMutex
);
179 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak
* > (this ) );
183 sal_Int64 nAvailable
= mpStream
->remainingSize();
186 return std::min
<sal_Int64
>(SAL_MAX_INT32
, nAvailable
);
188 void SAL_CALL
OTempFileService::closeInput( )
190 ::osl::MutexGuard
aGuard( maMutex
);
192 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak
* > (this ) );
198 // stream will be deleted by TempFile implementation
206 void SAL_CALL
OTempFileService::writeBytes( const css::uno::Sequence
< sal_Int8
>& aData
)
208 ::osl::MutexGuard
aGuard( maMutex
);
210 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak
* > (this ) );
213 sal_uInt32 nWritten
= mpStream
->WriteBytes(aData
.getConstArray(), aData
.getLength());
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
);
222 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak
* > (this ) );
228 void SAL_CALL
OTempFileService::closeOutput( )
230 ::osl::MutexGuard
aGuard( maMutex
);
232 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak
* > (this ) );
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? )
239 mnCachedPos
= mpStream
->Tell();
240 mbHasCachedPos
= true;
244 mpTempFile
->CloseStream();
249 // stream will be deleted by TempFile implementation
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;
276 mpTempFile
->CloseStream();
282 throw css::io::NotConnectedException ( OUString(), static_cast < css::uno::XWeak
* > (this ) );
287 void SAL_CALL
OTempFileService::seek( sal_Int64 nLocation
)
289 ::osl::MutexGuard
aGuard( maMutex
);
291 if ( nLocation
< 0 || nLocation
> getLength() )
292 throw css::lang::IllegalArgumentException();
294 mpStream
->Seek(static_cast<sal_uInt32
>(nLocation
) );
297 sal_Int64 SAL_CALL
OTempFileService::getPosition( )
299 ::osl::MutexGuard
aGuard( maMutex
);
302 sal_uInt32 nPos
= mpStream
->Tell();
304 return static_cast<sal_Int64
>(nPos
);
306 sal_Int64 SAL_CALL
OTempFileService::getLength( )
308 ::osl::MutexGuard
aGuard( maMutex
);
313 sal_Int64 nEndPos
= mpStream
->TellEnd();
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
);
332 void SAL_CALL
OTempFileService::truncate()
334 ::osl::MutexGuard
aGuard( maMutex
);
336 // SetStreamSize() call does not change the position
338 mpStream
->SetStreamSize( 0 );
342 #define PROPERTY_HANDLE_URI 1
343 #define PROPERTY_HANDLE_REMOVE_FILE 2
344 #define PROPERTY_HANDLE_RESOURCE_NAME 3
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(),
357 css::beans::Property( "ResourceName", PROPERTY_HANDLE_RESOURCE_NAME
, cppu::UnoType
<OUString
>::get(),
358 css::beans::PropertyAttribute::READONLY
)
361 static css::uno::Reference
< css::beans::XPropertySetInfo
> xInfo(
362 ::cppu::OPropertySetHelper::createPropertySetInfo( ourPropertyInfo
) );
365 void OTempFileService::setPropertyValue( const ::rtl::OUString
& aPropertyName
, const ::css::uno::Any
& aValue
)
367 if ( aPropertyName
== "RemoveFile" )
368 setRemoveFile( aValue
.get
<bool>() );
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());
386 throw css::beans::UnknownPropertyException(aPropertyName
);
389 void OTempFileService::addPropertyChangeListener( const ::rtl::OUString
& /*aPropertyName*/, const ::css::uno::Reference
< ::css::beans::XPropertyChangeListener
>& /*xListener*/ )
393 void OTempFileService::removePropertyChangeListener( const ::rtl::OUString
& /*aPropertyName*/, const ::css::uno::Reference
< ::css::beans::XPropertyChangeListener
>& /*xListener*/ )
397 void OTempFileService::addVetoableChangeListener( const ::rtl::OUString
& /*aPropertyName*/, const ::css::uno::Reference
< ::css::beans::XVetoableChangeListener
>& /*xListener*/ )
401 void OTempFileService::removeVetoableChangeListener( const ::rtl::OUString
& /*aPropertyName*/, const ::css::uno::Reference
< ::css::beans::XVetoableChangeListener
>& /*xListener*/ )
406 void OTempFileService::setFastPropertyValue( ::sal_Int32 nHandle
, const ::css::uno::Any
& aValue
)
410 case PROPERTY_HANDLE_REMOVE_FILE
: setRemoveFile( aValue
.get
<bool>() ); return;
413 throw css::beans::UnknownPropertyException(OUString::number(nHandle
));
415 ::css::uno::Any
OTempFileService::getFastPropertyValue( ::sal_Int32 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());
424 throw css::beans::UnknownPropertyException(OUString::number(nHandle
));
427 ::css::uno::Sequence
< ::css::beans::PropertyValue
> OTempFileService::getPropertyValues()
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: */