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 <com/sun/star/ucb/SimpleFileAccess.hpp>
22 #include "selfterminatefilestream.hxx"
23 #include <comphelper/processfactory.hxx>
25 using namespace ::com::sun::star
;
27 OSelfTerminateFileStream::OSelfTerminateFileStream( const uno::Reference
< uno::XComponentContext
>& xContext
, const OUString
& aURL
)
30 uno::Reference
< uno::XComponentContext
> xOwnContext
= xContext
;
31 if ( !xOwnContext
.is() )
32 xOwnContext
.set( ::comphelper::getProcessComponentContext(), uno::UNO_SET_THROW
);
34 // IMPORTANT: The implementation is based on idea that m_xFileAccess, m_xInputStream and m_xSeekable are always set
35 // otherwise an exception is thrown in constructor
37 m_xFileAccess
.set( ucb::SimpleFileAccess::create(xOwnContext
) );
39 m_xInputStream
.set( m_xFileAccess
->openFileRead( aURL
), uno::UNO_SET_THROW
);
40 m_xSeekable
.set( m_xInputStream
, uno::UNO_QUERY_THROW
);
43 OSelfTerminateFileStream::~OSelfTerminateFileStream()
45 CloseStreamDeleteFile();
48 void OSelfTerminateFileStream::CloseStreamDeleteFile()
52 m_xInputStream
->closeInput();
54 catch( uno::Exception
& )
59 m_xFileAccess
->kill( m_aURL
);
61 catch( uno::Exception
& )
65 sal_Int32 SAL_CALL
OSelfTerminateFileStream::readBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
67 return m_xInputStream
->readBytes( aData
, nBytesToRead
);
70 sal_Int32 SAL_CALL
OSelfTerminateFileStream::readSomeBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
72 return m_xInputStream
->readSomeBytes( aData
, nMaxBytesToRead
);
75 void SAL_CALL
OSelfTerminateFileStream::skipBytes( sal_Int32 nBytesToSkip
)
77 return m_xInputStream
->skipBytes( nBytesToSkip
);
80 sal_Int32 SAL_CALL
OSelfTerminateFileStream::available( )
82 return m_xInputStream
->available();
85 void SAL_CALL
OSelfTerminateFileStream::closeInput( )
87 CloseStreamDeleteFile();
90 void SAL_CALL
OSelfTerminateFileStream::seek( sal_Int64 location
)
92 m_xSeekable
->seek( location
);
95 sal_Int64 SAL_CALL
OSelfTerminateFileStream::getPosition()
97 return m_xSeekable
->getPosition();
100 sal_Int64 SAL_CALL
OSelfTerminateFileStream::getLength()
102 return m_xSeekable
->getLength();
105 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */