xref: /trunk/main/ucb/source/ucp/file/filinpstr.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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_ucb.hxx"
30 #include "filinpstr.hxx"
31 #ifndef _FILERROR_HXX_
32 #include "filerror.hxx"
33 #endif
34 #include "shell.hxx"
35 #include "prov.hxx"
36 
37 
38 using namespace fileaccess;
39 using namespace com::sun::star;
40 using namespace com::sun::star::ucb;
41 
42 
43 
44 XInputStream_impl::XInputStream_impl( shell* pMyShell,const rtl::OUString& aUncPath, sal_Bool bLock )
45     : m_pMyShell( pMyShell ),
46       m_xProvider( pMyShell->m_pProvider ),
47       m_bLock( bLock ),
48       m_aFile( aUncPath ),
49       m_nErrorCode( TASKHANDLER_NO_ERROR ),
50       m_nMinorErrorCode( TASKHANDLER_NO_ERROR )
51 {
52     sal_uInt32 nFlags = OpenFlag_Read;
53     if ( !bLock )
54         nFlags |= OpenFlag_NoLock;
55 
56     osl::FileBase::RC err = m_aFile.open( nFlags );
57     if( err != osl::FileBase::E_None )
58     {
59         m_nIsOpen = false;
60         m_aFile.close();
61 
62         m_nErrorCode = TASKHANDLING_OPEN_FOR_INPUTSTREAM;
63         m_nMinorErrorCode = err;
64     }
65     else
66         m_nIsOpen = true;
67 }
68 
69 
70 XInputStream_impl::~XInputStream_impl()
71 {
72     try
73     {
74         closeInput();
75     }
76     catch (io::IOException const &)
77     {
78         OSL_ENSURE(false, "unexpected situation");
79     }
80     catch (uno::RuntimeException const &)
81     {
82         OSL_ENSURE(false, "unexpected situation");
83     }
84 }
85 
86 
87 sal_Int32 SAL_CALL XInputStream_impl::CtorSuccess()
88 {
89     return m_nErrorCode;
90 };
91 
92 
93 
94 sal_Int32 SAL_CALL XInputStream_impl::getMinorError()
95 {
96     return m_nMinorErrorCode;
97 }
98 
99 
100 //////////////////////////////////////////////////////////////////////////////////////////
101 //  XTypeProvider
102 //////////////////////////////////////////////////////////////////////////////////////////
103 
104 
105 XTYPEPROVIDER_IMPL_3( XInputStream_impl,
106                       lang::XTypeProvider,
107                       io::XSeekable,
108                       io::XInputStream )
109 
110 
111 
112 uno::Any SAL_CALL
113 XInputStream_impl::queryInterface(
114     const uno::Type& rType )
115     throw( uno::RuntimeException)
116 {
117     uno::Any aRet = cppu::queryInterface( rType,
118                                           SAL_STATIC_CAST( io::XInputStream*,this ),
119                                           SAL_STATIC_CAST( lang::XTypeProvider*,this ),
120                                           SAL_STATIC_CAST( io::XSeekable*,this ) );
121     return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
122 }
123 
124 
125 void SAL_CALL
126 XInputStream_impl::acquire(
127     void )
128     throw()
129 {
130     OWeakObject::acquire();
131 }
132 
133 
134 void SAL_CALL
135 XInputStream_impl::release(
136     void )
137     throw()
138 {
139     OWeakObject::release();
140 }
141 
142 
143 
144 sal_Int32 SAL_CALL
145 XInputStream_impl::readBytes(
146                  uno::Sequence< sal_Int8 >& aData,
147                  sal_Int32 nBytesToRead )
148     throw( io::NotConnectedException,
149            io::BufferSizeExceededException,
150            io::IOException,
151            uno::RuntimeException)
152 {
153     if( ! m_nIsOpen ) throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
154 
155     aData.realloc(nBytesToRead);
156         //TODO! translate memory exhaustion (if it were detectable...) into
157         // io::BufferSizeExceededException
158 
159     sal_uInt64 nrc(0);
160     if(m_aFile.read( aData.getArray(),sal_uInt64(nBytesToRead),nrc )
161        != osl::FileBase::E_None)
162         throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
163 
164     // Shrink aData in case we read less than nBytesToRead (XInputStream
165     // documentation does not tell whether this is required, and I do not know
166     // if any code relies on this, so be conservative---SB):
167     if (sal::static_int_cast<sal_Int32>(nrc) != nBytesToRead)
168         aData.realloc(sal_Int32(nrc));
169     return ( sal_Int32 ) nrc;
170 }
171 
172 sal_Int32 SAL_CALL
173 XInputStream_impl::readSomeBytes(
174     uno::Sequence< sal_Int8 >& aData,
175     sal_Int32 nMaxBytesToRead )
176     throw( io::NotConnectedException,
177            io::BufferSizeExceededException,
178            io::IOException,
179            uno::RuntimeException)
180 {
181     return readBytes( aData,nMaxBytesToRead );
182 }
183 
184 
185 void SAL_CALL
186 XInputStream_impl::skipBytes(
187     sal_Int32 nBytesToSkip )
188     throw( io::NotConnectedException,
189            io::BufferSizeExceededException,
190            io::IOException,
191            uno::RuntimeException)
192 {
193     m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) );
194 }
195 
196 
197 sal_Int32 SAL_CALL
198 XInputStream_impl::available(
199     void )
200     throw( io::NotConnectedException,
201            io::IOException,
202            uno::RuntimeException)
203 {
204     return 0;
205 }
206 
207 
208 void SAL_CALL
209 XInputStream_impl::closeInput(
210     void )
211     throw( io::NotConnectedException,
212            io::IOException,
213            uno::RuntimeException )
214 {
215     if( m_nIsOpen )
216     {
217         osl::FileBase::RC err = m_aFile.close();
218         if( err != osl::FileBase::E_None )
219             throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
220         m_nIsOpen = false;
221     }
222 }
223 
224 
225 void SAL_CALL
226 XInputStream_impl::seek(
227     sal_Int64 location )
228     throw( lang::IllegalArgumentException,
229            io::IOException,
230            uno::RuntimeException )
231 {
232     if( location < 0 )
233         throw lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 0 );
234     if( osl::FileBase::E_None != m_aFile.setPos( Pos_Absolut, sal_uInt64( location ) ) )
235         throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
236 }
237 
238 
239 sal_Int64 SAL_CALL
240 XInputStream_impl::getPosition(
241     void )
242     throw( io::IOException,
243            uno::RuntimeException )
244 {
245     sal_uInt64 uPos;
246     if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
247         throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
248     return sal_Int64( uPos );
249 }
250 
251 sal_Int64 SAL_CALL
252 XInputStream_impl::getLength(
253     void )
254     throw( io::IOException,
255            uno::RuntimeException )
256 {
257     sal_uInt64 uEndPos;
258     if ( m_aFile.getSize(uEndPos) != osl::FileBase::E_None )
259         throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
260     else
261         return sal_Int64( uEndPos );
262 }
263