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