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