xref: /trunk/main/ucb/source/ucp/file/filinpstr.cxx (revision 421ed02e)
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 
XInputStream_impl(shell * pMyShell,const rtl::OUString & aUncPath,sal_Bool bLock)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 
~XInputStream_impl()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 
CtorSuccess()83 sal_Int32 SAL_CALL XInputStream_impl::CtorSuccess()
84 {
85 	return m_nErrorCode;
86 };
87 
88 
89 
getMinorError()90 sal_Int32 SAL_CALL XInputStream_impl::getMinorError()
91 {
92 	return m_nMinorErrorCode;
93 }
94 
95 
96 //////////////////////////////////////////////////////////////////////////////////////////
97 //  XTypeProvider
98 //////////////////////////////////////////////////////////////////////////////////////////
99 
100 
XTYPEPROVIDER_IMPL_3(XInputStream_impl,lang::XTypeProvider,io::XSeekable,io::XInputStream)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 	throw( uno::RuntimeException)
112 {
113 	uno::Any aRet = cppu::queryInterface( rType,
114 										  SAL_STATIC_CAST( io::XInputStream*,this ),
115 										  SAL_STATIC_CAST( lang::XTypeProvider*,this ),
116 										  SAL_STATIC_CAST( io::XSeekable*,this ) );
117 	return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
118 }
119 
120 
121 void SAL_CALL
acquire(void)122 XInputStream_impl::acquire(
123 	void )
124 	throw()
125 {
126 	OWeakObject::acquire();
127 }
128 
129 
130 void SAL_CALL
release(void)131 XInputStream_impl::release(
132 	void )
133 	throw()
134 {
135 	OWeakObject::release();
136 }
137 
138 
139 
140 sal_Int32 SAL_CALL
readBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)141 XInputStream_impl::readBytes(
142 			     uno::Sequence< sal_Int8 >& aData,
143 			     sal_Int32 nBytesToRead )
144 	throw( io::NotConnectedException,
145 		   io::BufferSizeExceededException,
146 		   io::IOException,
147 		   uno::RuntimeException)
148 {
149 	if( ! m_nIsOpen ) throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
150 
151     aData.realloc(nBytesToRead);
152         //TODO! translate memory exhaustion (if it were detectable...) into
153         // io::BufferSizeExceededException
154 
155 	sal_uInt64 nrc(0);
156 	if(m_aFile.read( aData.getArray(),sal_uInt64(nBytesToRead),nrc )
157        != osl::FileBase::E_None)
158         throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
159 
160     // Shrink aData in case we read less than nBytesToRead (XInputStream
161     // documentation does not tell whether this is required, and I do not know
162     // if any code relies on this, so be conservative---SB):
163     if (sal::static_int_cast<sal_Int32>(nrc) != nBytesToRead)
164         aData.realloc(sal_Int32(nrc));
165 	return ( sal_Int32 ) nrc;
166 }
167 
168 sal_Int32 SAL_CALL
readSomeBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)169 XInputStream_impl::readSomeBytes(
170 	uno::Sequence< sal_Int8 >& aData,
171 	sal_Int32 nMaxBytesToRead )
172 	throw( io::NotConnectedException,
173 		   io::BufferSizeExceededException,
174 		   io::IOException,
175 		   uno::RuntimeException)
176 {
177 	return readBytes( aData,nMaxBytesToRead );
178 }
179 
180 
181 void SAL_CALL
skipBytes(sal_Int32 nBytesToSkip)182 XInputStream_impl::skipBytes(
183 	sal_Int32 nBytesToSkip )
184 	throw( io::NotConnectedException,
185 		   io::BufferSizeExceededException,
186 		   io::IOException,
187 		   uno::RuntimeException)
188 {
189 	m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) );
190 }
191 
192 
193 sal_Int32 SAL_CALL
available(void)194 XInputStream_impl::available(
195 	void )
196 	throw( io::NotConnectedException,
197 		   io::IOException,
198 		   uno::RuntimeException)
199 {
200 	return 0;
201 }
202 
203 
204 void SAL_CALL
closeInput(void)205 XInputStream_impl::closeInput(
206 	void )
207 	throw( io::NotConnectedException,
208 		   io::IOException,
209 		   uno::RuntimeException )
210 {
211 	if( m_nIsOpen )
212 	{
213 		osl::FileBase::RC err = m_aFile.close();
214 		if( err != osl::FileBase::E_None )
215 			throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
216 		m_nIsOpen = false;
217 	}
218 }
219 
220 
221 void SAL_CALL
seek(sal_Int64 location)222 XInputStream_impl::seek(
223 	sal_Int64 location )
224 	throw( lang::IllegalArgumentException,
225 		   io::IOException,
226 		   uno::RuntimeException )
227 {
228 	if( location < 0 )
229 		throw lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 0 );
230 	if( osl::FileBase::E_None != m_aFile.setPos( Pos_Absolut, sal_uInt64( location ) ) )
231 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
232 }
233 
234 
235 sal_Int64 SAL_CALL
getPosition(void)236 XInputStream_impl::getPosition(
237 	void )
238 	throw( io::IOException,
239 		   uno::RuntimeException )
240 {
241 	sal_uInt64 uPos;
242 	if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
243 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
244 	return sal_Int64( uPos );
245 }
246 
247 sal_Int64 SAL_CALL
getLength(void)248 XInputStream_impl::getLength(
249 	void )
250 	throw( io::IOException,
251 		   uno::RuntimeException )
252 {
253 	sal_uInt64 uEndPos;
254 	if ( m_aFile.getSize(uEndPos) != osl::FileBase::E_None )
255 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
256 	else
257 		return sal_Int64( uEndPos );
258 }
259