#i107035# let the temporary file be removed after the stream has been closed

This commit is contained in:
Mikhail Voitenko 2009-11-19 17:30:33 +00:00
parent d95c37cdac
commit 792bdc656d
4 changed files with 240 additions and 9 deletions

View file

@ -52,6 +52,7 @@ SLOFILES = \
$(SLO)$/xstorage.obj\
$(SLO)$/xfactory.obj\
$(SLO)$/disposelistener.obj\
$(SLO)$/selfterminatefilestream.obj\
$(SLO)$/switchpersistencestream.obj\
$(SLO)$/register.obj

View file

@ -48,6 +48,7 @@
#include <comphelper/storagehelper.hxx>
#include <comphelper/ofopxmlhelper.hxx>
#include "selfterminatefilestream.hxx"
#include "owriteablestream.hxx"
#include "oseekinstream.hxx"
#include "mutexholder.hxx"
@ -866,18 +867,10 @@ void OWriteStream_Impl::Commit()
}
else if ( m_aTempURL.getLength() )
{
uno::Reference < ucb::XSimpleFileAccess > xTempAccess(
GetServiceFactory()->createInstance (
::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
uno::UNO_QUERY );
if ( !xTempAccess.is() )
throw uno::RuntimeException(); // TODO:
uno::Reference< io::XInputStream > xInStream;
try
{
xInStream = xTempAccess->openFileRead( m_aTempURL );
xInStream.set( static_cast< io::XInputStream* >( new OSelfTerminateFileStream( GetServiceFactory(), m_aTempURL ) ), uno::UNO_QUERY );
}
catch( uno::Exception& )
{
@ -2627,6 +2620,8 @@ void SAL_CALL OWriteStream::dispose()
m_xInStream = uno::Reference< io::XInputStream >();
}
m_xSeekable = uno::Reference< io::XSeekable >();
m_pImpl->m_pAntiImpl = NULL;
if ( !m_bInitOnDemand )

View file

@ -0,0 +1,156 @@
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2008 by Sun Microsystems, Inc.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: ocompinstream.cxx,v $
* $Revision: 1.12 $
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_package.hxx"
#include <com/sun/star/ucb/XSimpleFileAccess.hpp>
#include "selfterminatefilestream.hxx"
#include <comphelper/processfactory.hxx>
using namespace ::com::sun::star;
//-----------------------------------------------
OSelfTerminateFileStream::OSelfTerminateFileStream( const uno::Reference< lang::XMultiServiceFactory > xFactory, const ::rtl::OUString& aURL )
: m_aURL( aURL )
{
uno::Reference< lang::XMultiServiceFactory > xOwnFactory = xFactory;
if ( !xOwnFactory.is() )
xOwnFactory.set( ::comphelper::getProcessServiceFactory(), uno::UNO_SET_THROW );
// IMPORTANT: The implementation is based on idea that m_xFileAccess, m_xInputStream and m_xSeekable are always set
// otherwise an exception is thrown in constructor
m_xFileAccess.set( xOwnFactory->createInstance (
::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
uno::UNO_QUERY_THROW );
m_xInputStream.set( m_xFileAccess->openFileRead( aURL ), uno::UNO_SET_THROW );
m_xSeekable.set( m_xInputStream, uno::UNO_QUERY_THROW );
}
//-----------------------------------------------
OSelfTerminateFileStream::~OSelfTerminateFileStream()
{
CloseStreamDeleteFile();
}
//-----------------------------------------------
void OSelfTerminateFileStream::CloseStreamDeleteFile()
{
try
{
m_xInputStream->closeInput();
}
catch( uno::Exception& )
{}
try
{
m_xFileAccess->kill( m_aURL );
}
catch( uno::Exception& )
{}
}
//-----------------------------------------------
sal_Int32 SAL_CALL OSelfTerminateFileStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
throw ( io::NotConnectedException,
io::BufferSizeExceededException,
io::IOException,
uno::RuntimeException )
{
return m_xInputStream->readBytes( aData, nBytesToRead );
}
//-----------------------------------------------
sal_Int32 SAL_CALL OSelfTerminateFileStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
throw ( io::NotConnectedException,
io::BufferSizeExceededException,
io::IOException,
uno::RuntimeException )
{
return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
}
//-----------------------------------------------
void SAL_CALL OSelfTerminateFileStream::skipBytes( sal_Int32 nBytesToSkip )
throw ( io::NotConnectedException,
io::BufferSizeExceededException,
io::IOException,
uno::RuntimeException )
{
return m_xInputStream->skipBytes( nBytesToSkip );
}
//-----------------------------------------------
sal_Int32 SAL_CALL OSelfTerminateFileStream::available( )
throw ( io::NotConnectedException,
io::IOException,
uno::RuntimeException )
{
return m_xInputStream->available();
}
//-----------------------------------------------
void SAL_CALL OSelfTerminateFileStream::closeInput( )
throw ( io::NotConnectedException,
io::IOException,
uno::RuntimeException )
{
CloseStreamDeleteFile();
}
//-----------------------------------------------
void SAL_CALL OSelfTerminateFileStream::seek( sal_Int64 location )
throw ( lang::IllegalArgumentException,
io::IOException,
uno::RuntimeException )
{
m_xSeekable->seek( location );
}
//-----------------------------------------------
sal_Int64 SAL_CALL OSelfTerminateFileStream::getPosition()
throw ( io::IOException,
uno::RuntimeException)
{
return m_xSeekable->getPosition();
}
//-----------------------------------------------
sal_Int64 SAL_CALL OSelfTerminateFileStream::getLength()
throw ( io::IOException,
uno::RuntimeException )
{
return m_xSeekable->getLength();
}

View file

@ -0,0 +1,79 @@
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2008 by Sun Microsystems, Inc.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: ocompinstream.hxx,v $
* $Revision: 1.7 $
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#ifndef _SELFTERMINATEFILESTREAM_HXX_
#define _SELFTERMINATEFILESTREAM_HXX_
#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/io/XSeekable.hpp>
#include <com/sun/star/ucb/XSimpleFileAccess.hpp>
#include <cppuhelper/implbase2.hxx>
struct OWriteStream_Impl;
class OSelfTerminateFileStream : public cppu::WeakImplHelper2< ::com::sun::star::io::XInputStream,
::com::sun::star::io::XSeekable >
{
protected:
::com::sun::star::uno::Reference< ::com::sun::star::ucb::XSimpleFileAccess > m_xFileAccess;
::rtl::OUString m_aURL;
::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > m_xInputStream;
::com::sun::star::uno::Reference< ::com::sun::star::io::XSeekable > m_xSeekable;
public:
OSelfTerminateFileStream( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xFactory, const ::rtl::OUString& aURL );
virtual ~OSelfTerminateFileStream();
void CloseStreamDeleteFile();
// XInputStream
virtual sal_Int32 SAL_CALL readBytes( ::com::sun::star::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
virtual sal_Int32 SAL_CALL readSomeBytes( ::com::sun::star::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
virtual sal_Int32 SAL_CALL available()
throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
virtual void SAL_CALL closeInput()
throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
//XSeekable
virtual void SAL_CALL seek( sal_Int64 location ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
virtual sal_Int64 SAL_CALL getPosition() throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
virtual sal_Int64 SAL_CALL getLength() throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
};
#endif