1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_package.hxx"
30 
31 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
32 
33 #include "selfterminatefilestream.hxx"
34 #include <comphelper/processfactory.hxx>
35 
36 using namespace ::com::sun::star;
37 
38 //-----------------------------------------------
39 OSelfTerminateFileStream::OSelfTerminateFileStream( const uno::Reference< lang::XMultiServiceFactory > xFactory, const ::rtl::OUString& aURL )
40 : m_aURL( aURL )
41 {
42     uno::Reference< lang::XMultiServiceFactory > xOwnFactory = xFactory;
43     if ( !xOwnFactory.is() )
44         xOwnFactory.set( ::comphelper::getProcessServiceFactory(), uno::UNO_SET_THROW );
45 
46     // IMPORTANT: The implementation is based on idea that m_xFileAccess, m_xInputStream and m_xSeekable are always set
47     // otherwise an exception is thrown in constructor
48 
49     m_xFileAccess.set( xOwnFactory->createInstance (
50                             ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
51                        uno::UNO_QUERY_THROW );
52 
53     m_xInputStream.set( m_xFileAccess->openFileRead( aURL ), uno::UNO_SET_THROW );
54     m_xSeekable.set( m_xInputStream, uno::UNO_QUERY_THROW );
55 }
56 
57 //-----------------------------------------------
58 OSelfTerminateFileStream::~OSelfTerminateFileStream()
59 {
60     CloseStreamDeleteFile();
61 }
62 
63 //-----------------------------------------------
64 void OSelfTerminateFileStream::CloseStreamDeleteFile()
65 {
66     try
67     {
68         m_xInputStream->closeInput();
69     }
70     catch( uno::Exception& )
71     {}
72 
73     try
74     {
75         m_xFileAccess->kill( m_aURL );
76     }
77     catch( uno::Exception& )
78     {}
79 }
80 
81 //-----------------------------------------------
82 sal_Int32 SAL_CALL OSelfTerminateFileStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
83         throw ( io::NotConnectedException,
84                 io::BufferSizeExceededException,
85                 io::IOException,
86                 uno::RuntimeException )
87 {
88     return m_xInputStream->readBytes( aData, nBytesToRead );
89 }
90 
91 //-----------------------------------------------
92 sal_Int32 SAL_CALL OSelfTerminateFileStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
93         throw ( io::NotConnectedException,
94                 io::BufferSizeExceededException,
95                 io::IOException,
96                 uno::RuntimeException )
97 {
98     return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
99 }
100 
101 //-----------------------------------------------
102 void SAL_CALL OSelfTerminateFileStream::skipBytes( sal_Int32 nBytesToSkip )
103         throw ( io::NotConnectedException,
104                 io::BufferSizeExceededException,
105                 io::IOException,
106                 uno::RuntimeException )
107 {
108     return m_xInputStream->skipBytes( nBytesToSkip );
109 }
110 
111 //-----------------------------------------------
112 sal_Int32 SAL_CALL OSelfTerminateFileStream::available(  )
113         throw ( io::NotConnectedException,
114                 io::IOException,
115                 uno::RuntimeException )
116 {
117     return m_xInputStream->available();
118 }
119 
120 //-----------------------------------------------
121 void SAL_CALL OSelfTerminateFileStream::closeInput(  )
122         throw ( io::NotConnectedException,
123                 io::IOException,
124                 uno::RuntimeException )
125 {
126     CloseStreamDeleteFile();
127 }
128 
129 //-----------------------------------------------
130 void SAL_CALL OSelfTerminateFileStream::seek( sal_Int64 location )
131         throw ( lang::IllegalArgumentException,
132                 io::IOException,
133                 uno::RuntimeException )
134 {
135     m_xSeekable->seek( location );
136 }
137 
138 //-----------------------------------------------
139 sal_Int64 SAL_CALL OSelfTerminateFileStream::getPosition()
140         throw ( io::IOException,
141                 uno::RuntimeException)
142 {
143     return m_xSeekable->getPosition();
144 }
145 
146 //-----------------------------------------------
147 sal_Int64 SAL_CALL OSelfTerminateFileStream::getLength()
148         throw ( io::IOException,
149                 uno::RuntimeException )
150 {
151     return m_xSeekable->getLength();
152 }
153 
154