xref: /AOO41X/main/ucb/source/ucp/file/filinpstr.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir  /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_ucb.hxx"
30*cdf0e10cSrcweir #include "filinpstr.hxx"
31*cdf0e10cSrcweir #ifndef _FILERROR_HXX_
32*cdf0e10cSrcweir #include "filerror.hxx"
33*cdf0e10cSrcweir #endif
34*cdf0e10cSrcweir #include "shell.hxx"
35*cdf0e10cSrcweir #include "prov.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir using namespace fileaccess;
39*cdf0e10cSrcweir using namespace com::sun::star;
40*cdf0e10cSrcweir using namespace com::sun::star::ucb;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir XInputStream_impl::XInputStream_impl( shell* pMyShell,const rtl::OUString& aUncPath, sal_Bool bLock )
45*cdf0e10cSrcweir 	: m_pMyShell( pMyShell ),
46*cdf0e10cSrcweir 	  m_xProvider( pMyShell->m_pProvider ),
47*cdf0e10cSrcweir       m_bLock( bLock ),
48*cdf0e10cSrcweir 	  m_aFile( aUncPath ),
49*cdf0e10cSrcweir 	  m_nErrorCode( TASKHANDLER_NO_ERROR ),
50*cdf0e10cSrcweir 	  m_nMinorErrorCode( TASKHANDLER_NO_ERROR )
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir     sal_uInt32 nFlags = OpenFlag_Read;
53*cdf0e10cSrcweir     if ( !bLock )
54*cdf0e10cSrcweir         nFlags |= OpenFlag_NoLock;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 	osl::FileBase::RC err = m_aFile.open( nFlags );
57*cdf0e10cSrcweir 	if( err != osl::FileBase::E_None )
58*cdf0e10cSrcweir 	{
59*cdf0e10cSrcweir 		m_nIsOpen = false;
60*cdf0e10cSrcweir 		m_aFile.close();
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 		m_nErrorCode = TASKHANDLING_OPEN_FOR_INPUTSTREAM;
63*cdf0e10cSrcweir 		m_nMinorErrorCode = err;
64*cdf0e10cSrcweir 	}
65*cdf0e10cSrcweir 	else
66*cdf0e10cSrcweir 		m_nIsOpen = true;
67*cdf0e10cSrcweir }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir XInputStream_impl::~XInputStream_impl()
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir 	try
73*cdf0e10cSrcweir 	{
74*cdf0e10cSrcweir 		closeInput();
75*cdf0e10cSrcweir 	}
76*cdf0e10cSrcweir     catch (io::IOException const &)
77*cdf0e10cSrcweir     {
78*cdf0e10cSrcweir         OSL_ENSURE(false, "unexpected situation");
79*cdf0e10cSrcweir     }
80*cdf0e10cSrcweir     catch (uno::RuntimeException const &)
81*cdf0e10cSrcweir     {
82*cdf0e10cSrcweir         OSL_ENSURE(false, "unexpected situation");
83*cdf0e10cSrcweir     }
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir sal_Int32 SAL_CALL XInputStream_impl::CtorSuccess()
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir 	return m_nErrorCode;
90*cdf0e10cSrcweir };
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir sal_Int32 SAL_CALL XInputStream_impl::getMinorError()
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir 	return m_nMinorErrorCode;
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////////////////
101*cdf0e10cSrcweir //  XTypeProvider
102*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////////////////
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir XTYPEPROVIDER_IMPL_3( XInputStream_impl,
106*cdf0e10cSrcweir 					  lang::XTypeProvider,
107*cdf0e10cSrcweir 					  io::XSeekable,
108*cdf0e10cSrcweir 					  io::XInputStream )
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir uno::Any SAL_CALL
113*cdf0e10cSrcweir XInputStream_impl::queryInterface(
114*cdf0e10cSrcweir 	const uno::Type& rType )
115*cdf0e10cSrcweir 	throw( uno::RuntimeException)
116*cdf0e10cSrcweir {
117*cdf0e10cSrcweir 	uno::Any aRet = cppu::queryInterface( rType,
118*cdf0e10cSrcweir 										  SAL_STATIC_CAST( io::XInputStream*,this ),
119*cdf0e10cSrcweir 										  SAL_STATIC_CAST( lang::XTypeProvider*,this ),
120*cdf0e10cSrcweir 										  SAL_STATIC_CAST( io::XSeekable*,this ) );
121*cdf0e10cSrcweir 	return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir void SAL_CALL
126*cdf0e10cSrcweir XInputStream_impl::acquire(
127*cdf0e10cSrcweir 	void )
128*cdf0e10cSrcweir 	throw()
129*cdf0e10cSrcweir {
130*cdf0e10cSrcweir 	OWeakObject::acquire();
131*cdf0e10cSrcweir }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir void SAL_CALL
135*cdf0e10cSrcweir XInputStream_impl::release(
136*cdf0e10cSrcweir 	void )
137*cdf0e10cSrcweir 	throw()
138*cdf0e10cSrcweir {
139*cdf0e10cSrcweir 	OWeakObject::release();
140*cdf0e10cSrcweir }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir sal_Int32 SAL_CALL
145*cdf0e10cSrcweir XInputStream_impl::readBytes(
146*cdf0e10cSrcweir 			     uno::Sequence< sal_Int8 >& aData,
147*cdf0e10cSrcweir 			     sal_Int32 nBytesToRead )
148*cdf0e10cSrcweir 	throw( io::NotConnectedException,
149*cdf0e10cSrcweir 		   io::BufferSizeExceededException,
150*cdf0e10cSrcweir 		   io::IOException,
151*cdf0e10cSrcweir 		   uno::RuntimeException)
152*cdf0e10cSrcweir {
153*cdf0e10cSrcweir 	if( ! m_nIsOpen ) throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir     aData.realloc(nBytesToRead);
156*cdf0e10cSrcweir         //TODO! translate memory exhaustion (if it were detectable...) into
157*cdf0e10cSrcweir         // io::BufferSizeExceededException
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 	sal_uInt64 nrc(0);
160*cdf0e10cSrcweir 	if(m_aFile.read( aData.getArray(),sal_uInt64(nBytesToRead),nrc )
161*cdf0e10cSrcweir        != osl::FileBase::E_None)
162*cdf0e10cSrcweir         throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     // Shrink aData in case we read less than nBytesToRead (XInputStream
165*cdf0e10cSrcweir     // documentation does not tell whether this is required, and I do not know
166*cdf0e10cSrcweir     // if any code relies on this, so be conservative---SB):
167*cdf0e10cSrcweir     if (sal::static_int_cast<sal_Int32>(nrc) != nBytesToRead)
168*cdf0e10cSrcweir         aData.realloc(sal_Int32(nrc));
169*cdf0e10cSrcweir 	return ( sal_Int32 ) nrc;
170*cdf0e10cSrcweir }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir sal_Int32 SAL_CALL
173*cdf0e10cSrcweir XInputStream_impl::readSomeBytes(
174*cdf0e10cSrcweir 	uno::Sequence< sal_Int8 >& aData,
175*cdf0e10cSrcweir 	sal_Int32 nMaxBytesToRead )
176*cdf0e10cSrcweir 	throw( io::NotConnectedException,
177*cdf0e10cSrcweir 		   io::BufferSizeExceededException,
178*cdf0e10cSrcweir 		   io::IOException,
179*cdf0e10cSrcweir 		   uno::RuntimeException)
180*cdf0e10cSrcweir {
181*cdf0e10cSrcweir 	return readBytes( aData,nMaxBytesToRead );
182*cdf0e10cSrcweir }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir void SAL_CALL
186*cdf0e10cSrcweir XInputStream_impl::skipBytes(
187*cdf0e10cSrcweir 	sal_Int32 nBytesToSkip )
188*cdf0e10cSrcweir 	throw( io::NotConnectedException,
189*cdf0e10cSrcweir 		   io::BufferSizeExceededException,
190*cdf0e10cSrcweir 		   io::IOException,
191*cdf0e10cSrcweir 		   uno::RuntimeException)
192*cdf0e10cSrcweir {
193*cdf0e10cSrcweir 	m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) );
194*cdf0e10cSrcweir }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir sal_Int32 SAL_CALL
198*cdf0e10cSrcweir XInputStream_impl::available(
199*cdf0e10cSrcweir 	void )
200*cdf0e10cSrcweir 	throw( io::NotConnectedException,
201*cdf0e10cSrcweir 		   io::IOException,
202*cdf0e10cSrcweir 		   uno::RuntimeException)
203*cdf0e10cSrcweir {
204*cdf0e10cSrcweir 	return 0;
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir void SAL_CALL
209*cdf0e10cSrcweir XInputStream_impl::closeInput(
210*cdf0e10cSrcweir 	void )
211*cdf0e10cSrcweir 	throw( io::NotConnectedException,
212*cdf0e10cSrcweir 		   io::IOException,
213*cdf0e10cSrcweir 		   uno::RuntimeException )
214*cdf0e10cSrcweir {
215*cdf0e10cSrcweir 	if( m_nIsOpen )
216*cdf0e10cSrcweir 	{
217*cdf0e10cSrcweir 		osl::FileBase::RC err = m_aFile.close();
218*cdf0e10cSrcweir 		if( err != osl::FileBase::E_None )
219*cdf0e10cSrcweir 			throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
220*cdf0e10cSrcweir 		m_nIsOpen = false;
221*cdf0e10cSrcweir 	}
222*cdf0e10cSrcweir }
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir void SAL_CALL
226*cdf0e10cSrcweir XInputStream_impl::seek(
227*cdf0e10cSrcweir 	sal_Int64 location )
228*cdf0e10cSrcweir 	throw( lang::IllegalArgumentException,
229*cdf0e10cSrcweir 		   io::IOException,
230*cdf0e10cSrcweir 		   uno::RuntimeException )
231*cdf0e10cSrcweir {
232*cdf0e10cSrcweir 	if( location < 0 )
233*cdf0e10cSrcweir 		throw lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 0 );
234*cdf0e10cSrcweir 	if( osl::FileBase::E_None != m_aFile.setPos( Pos_Absolut, sal_uInt64( location ) ) )
235*cdf0e10cSrcweir 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
236*cdf0e10cSrcweir }
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir sal_Int64 SAL_CALL
240*cdf0e10cSrcweir XInputStream_impl::getPosition(
241*cdf0e10cSrcweir 	void )
242*cdf0e10cSrcweir 	throw( io::IOException,
243*cdf0e10cSrcweir 		   uno::RuntimeException )
244*cdf0e10cSrcweir {
245*cdf0e10cSrcweir 	sal_uInt64 uPos;
246*cdf0e10cSrcweir 	if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
247*cdf0e10cSrcweir 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
248*cdf0e10cSrcweir 	return sal_Int64( uPos );
249*cdf0e10cSrcweir }
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir sal_Int64 SAL_CALL
252*cdf0e10cSrcweir XInputStream_impl::getLength(
253*cdf0e10cSrcweir 	void )
254*cdf0e10cSrcweir 	throw( io::IOException,
255*cdf0e10cSrcweir 		   uno::RuntimeException )
256*cdf0e10cSrcweir {
257*cdf0e10cSrcweir 	sal_uInt64 uEndPos;
258*cdf0e10cSrcweir 	if ( m_aFile.getSize(uEndPos) != osl::FileBase::E_None )
259*cdf0e10cSrcweir 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
260*cdf0e10cSrcweir 	else
261*cdf0e10cSrcweir 		return sal_Int64( uEndPos );
262*cdf0e10cSrcweir }
263