1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_package.hxx"
26 
27 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
28 
29 #include "selfterminatefilestream.hxx"
30 #include <comphelper/processfactory.hxx>
31 
32 using namespace ::com::sun::star;
33 
34 //-----------------------------------------------
OSelfTerminateFileStream(const uno::Reference<lang::XMultiServiceFactory> xFactory,const::rtl::OUString & aURL)35 OSelfTerminateFileStream::OSelfTerminateFileStream( const uno::Reference< lang::XMultiServiceFactory > xFactory, const ::rtl::OUString& aURL )
36 : m_aURL( aURL )
37 {
38     uno::Reference< lang::XMultiServiceFactory > xOwnFactory = xFactory;
39     if ( !xOwnFactory.is() )
40         xOwnFactory.set( ::comphelper::getProcessServiceFactory(), uno::UNO_SET_THROW );
41 
42     // IMPORTANT: The implementation is based on idea that m_xFileAccess, m_xInputStream and m_xSeekable are always set
43     // otherwise an exception is thrown in constructor
44 
45     m_xFileAccess.set( xOwnFactory->createInstance (
46                             ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
47                        uno::UNO_QUERY_THROW );
48 
49     m_xInputStream.set( m_xFileAccess->openFileRead( aURL ), uno::UNO_SET_THROW );
50     m_xSeekable.set( m_xInputStream, uno::UNO_QUERY_THROW );
51 }
52 
53 //-----------------------------------------------
~OSelfTerminateFileStream()54 OSelfTerminateFileStream::~OSelfTerminateFileStream()
55 {
56     CloseStreamDeleteFile();
57 }
58 
59 //-----------------------------------------------
CloseStreamDeleteFile()60 void OSelfTerminateFileStream::CloseStreamDeleteFile()
61 {
62     try
63     {
64         m_xInputStream->closeInput();
65     }
66     catch( uno::Exception& )
67     {}
68 
69     try
70     {
71         m_xFileAccess->kill( m_aURL );
72     }
73     catch( uno::Exception& )
74     {}
75 }
76 
77 //-----------------------------------------------
readBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)78 sal_Int32 SAL_CALL OSelfTerminateFileStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
79         throw ( io::NotConnectedException,
80                 io::BufferSizeExceededException,
81                 io::IOException,
82                 uno::RuntimeException )
83 {
84     return m_xInputStream->readBytes( aData, nBytesToRead );
85 }
86 
87 //-----------------------------------------------
readSomeBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)88 sal_Int32 SAL_CALL OSelfTerminateFileStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
89         throw ( io::NotConnectedException,
90                 io::BufferSizeExceededException,
91                 io::IOException,
92                 uno::RuntimeException )
93 {
94     return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
95 }
96 
97 //-----------------------------------------------
skipBytes(sal_Int32 nBytesToSkip)98 void SAL_CALL OSelfTerminateFileStream::skipBytes( sal_Int32 nBytesToSkip )
99         throw ( io::NotConnectedException,
100                 io::BufferSizeExceededException,
101                 io::IOException,
102                 uno::RuntimeException )
103 {
104     return m_xInputStream->skipBytes( nBytesToSkip );
105 }
106 
107 //-----------------------------------------------
available()108 sal_Int32 SAL_CALL OSelfTerminateFileStream::available(  )
109         throw ( io::NotConnectedException,
110                 io::IOException,
111                 uno::RuntimeException )
112 {
113     return m_xInputStream->available();
114 }
115 
116 //-----------------------------------------------
closeInput()117 void SAL_CALL OSelfTerminateFileStream::closeInput(  )
118         throw ( io::NotConnectedException,
119                 io::IOException,
120                 uno::RuntimeException )
121 {
122     CloseStreamDeleteFile();
123 }
124 
125 //-----------------------------------------------
seek(sal_Int64 location)126 void SAL_CALL OSelfTerminateFileStream::seek( sal_Int64 location )
127         throw ( lang::IllegalArgumentException,
128                 io::IOException,
129                 uno::RuntimeException )
130 {
131     m_xSeekable->seek( location );
132 }
133 
134 //-----------------------------------------------
getPosition()135 sal_Int64 SAL_CALL OSelfTerminateFileStream::getPosition()
136         throw ( io::IOException,
137                 uno::RuntimeException)
138 {
139     return m_xSeekable->getPosition();
140 }
141 
142 //-----------------------------------------------
getLength()143 sal_Int64 SAL_CALL OSelfTerminateFileStream::getLength()
144         throw ( io::IOException,
145                 uno::RuntimeException )
146 {
147     return m_xSeekable->getLength();
148 }
149 
150