1*89dcb3daSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*89dcb3daSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*89dcb3daSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*89dcb3daSAndrew Rist  * distributed with this work for additional information
6*89dcb3daSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*89dcb3daSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*89dcb3daSAndrew Rist  * "License"); you may not use this file except in compliance
9*89dcb3daSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*89dcb3daSAndrew Rist  *
11*89dcb3daSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*89dcb3daSAndrew Rist  *
13*89dcb3daSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*89dcb3daSAndrew Rist  * software distributed under the License is distributed on an
15*89dcb3daSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*89dcb3daSAndrew Rist  * KIND, either express or implied.  See the License for the
17*89dcb3daSAndrew Rist  * specific language governing permissions and limitations
18*89dcb3daSAndrew Rist  * under the License.
19*89dcb3daSAndrew Rist  *
20*89dcb3daSAndrew Rist  *************************************************************/
21*89dcb3daSAndrew Rist 
22*89dcb3daSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
26cdf0e10cSrcweir #include "precompiled_xmlhelp.hxx"
27cdf0e10cSrcweir #include "inputstream.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir 
30cdf0e10cSrcweir using namespace chelp;
31cdf0e10cSrcweir using namespace com::sun::star;
32cdf0e10cSrcweir using namespace com::sun::star::ucb;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
XInputStream_impl(const rtl::OUString & aUncPath)36cdf0e10cSrcweir XInputStream_impl::XInputStream_impl( const rtl::OUString& aUncPath )
37cdf0e10cSrcweir     : m_bIsOpen( false ),
38cdf0e10cSrcweir       m_aFile( aUncPath )
39cdf0e10cSrcweir {
40cdf0e10cSrcweir 	m_bIsOpen = ( osl::FileBase::E_None == m_aFile.open( OpenFlag_Read ) );
41cdf0e10cSrcweir }
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 
~XInputStream_impl()44cdf0e10cSrcweir XInputStream_impl::~XInputStream_impl()
45cdf0e10cSrcweir {
46cdf0e10cSrcweir 	closeInput();
47cdf0e10cSrcweir }
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 
CtorSuccess()50cdf0e10cSrcweir bool SAL_CALL XInputStream_impl::CtorSuccess()
51cdf0e10cSrcweir {
52cdf0e10cSrcweir 	return m_bIsOpen;
53cdf0e10cSrcweir };
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 
56cdf0e10cSrcweir uno::Any SAL_CALL
queryInterface(const uno::Type & rType)57cdf0e10cSrcweir XInputStream_impl::queryInterface(
58cdf0e10cSrcweir 	const uno::Type& rType )
59cdf0e10cSrcweir 	throw( uno::RuntimeException)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir 	uno::Any aRet = cppu::queryInterface( rType,
62cdf0e10cSrcweir 										  SAL_STATIC_CAST( io::XInputStream*,this ),
63cdf0e10cSrcweir 										  SAL_STATIC_CAST( io::XSeekable*,this ) );
64cdf0e10cSrcweir 	return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 
68cdf0e10cSrcweir void SAL_CALL
acquire(void)69cdf0e10cSrcweir XInputStream_impl::acquire(
70cdf0e10cSrcweir 	void )
71cdf0e10cSrcweir 	throw()
72cdf0e10cSrcweir {
73cdf0e10cSrcweir 	OWeakObject::acquire();
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 
77cdf0e10cSrcweir void SAL_CALL
release(void)78cdf0e10cSrcweir XInputStream_impl::release(
79cdf0e10cSrcweir 	void )
80cdf0e10cSrcweir 	throw()
81cdf0e10cSrcweir {
82cdf0e10cSrcweir 	OWeakObject::release();
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 
87cdf0e10cSrcweir sal_Int32 SAL_CALL
readBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)88cdf0e10cSrcweir XInputStream_impl::readBytes(
89cdf0e10cSrcweir 			     uno::Sequence< sal_Int8 >& aData,
90cdf0e10cSrcweir 			     sal_Int32 nBytesToRead )
91cdf0e10cSrcweir 	throw( io::NotConnectedException,
92cdf0e10cSrcweir 		   io::BufferSizeExceededException,
93cdf0e10cSrcweir 		   io::IOException,
94cdf0e10cSrcweir 		   uno::RuntimeException)
95cdf0e10cSrcweir {
96cdf0e10cSrcweir 	if( ! m_bIsOpen )
97cdf0e10cSrcweir         throw io::IOException();
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     aData.realloc(nBytesToRead);
100cdf0e10cSrcweir     //TODO! translate memory exhaustion (if it were detectable...) into
101cdf0e10cSrcweir     // io::BufferSizeExceededException
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	sal_uInt64 nrc;
104cdf0e10cSrcweir 	m_aFile.read( aData.getArray(),sal_uInt64(nBytesToRead),nrc );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     // Shrink aData in case we read less than nBytesToRead (XInputStream
107cdf0e10cSrcweir     // documentation does not tell whether this is required, and I do not know
108cdf0e10cSrcweir     // if any code relies on this, so be conservative---SB):
109cdf0e10cSrcweir     if (nrc != sal::static_int_cast<sal_uInt64>( nBytesToRead) )
110cdf0e10cSrcweir         aData.realloc(sal_Int32(nrc));
111cdf0e10cSrcweir 	return ( sal_Int32 ) nrc;
112cdf0e10cSrcweir }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir sal_Int32 SAL_CALL
readSomeBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)115cdf0e10cSrcweir XInputStream_impl::readSomeBytes(
116cdf0e10cSrcweir 	uno::Sequence< sal_Int8 >& aData,
117cdf0e10cSrcweir 	sal_Int32 nMaxBytesToRead )
118cdf0e10cSrcweir 	throw( io::NotConnectedException,
119cdf0e10cSrcweir 		   io::BufferSizeExceededException,
120cdf0e10cSrcweir 		   io::IOException,
121cdf0e10cSrcweir 		   uno::RuntimeException)
122cdf0e10cSrcweir {
123cdf0e10cSrcweir 	return readBytes( aData,nMaxBytesToRead );
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 
127cdf0e10cSrcweir void SAL_CALL
skipBytes(sal_Int32 nBytesToSkip)128cdf0e10cSrcweir XInputStream_impl::skipBytes(
129cdf0e10cSrcweir 	sal_Int32 nBytesToSkip )
130cdf0e10cSrcweir 	throw( io::NotConnectedException,
131cdf0e10cSrcweir 		   io::BufferSizeExceededException,
132cdf0e10cSrcweir 		   io::IOException,
133cdf0e10cSrcweir 		   uno::RuntimeException)
134cdf0e10cSrcweir {
135cdf0e10cSrcweir 	m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) );
136cdf0e10cSrcweir }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 
139cdf0e10cSrcweir sal_Int32 SAL_CALL
available(void)140cdf0e10cSrcweir XInputStream_impl::available(
141cdf0e10cSrcweir 	void )
142cdf0e10cSrcweir 	throw( io::NotConnectedException,
143cdf0e10cSrcweir 		   io::IOException,
144cdf0e10cSrcweir 		   uno::RuntimeException)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir 	return 0;
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 
150cdf0e10cSrcweir void SAL_CALL
closeInput(void)151cdf0e10cSrcweir XInputStream_impl::closeInput(
152cdf0e10cSrcweir 	void )
153cdf0e10cSrcweir 	throw( io::NotConnectedException,
154cdf0e10cSrcweir 		   io::IOException,
155cdf0e10cSrcweir 		   uno::RuntimeException )
156cdf0e10cSrcweir {
157cdf0e10cSrcweir 	if( m_bIsOpen )
158cdf0e10cSrcweir 	{
159cdf0e10cSrcweir 		osl::FileBase::RC err = m_aFile.close();
160cdf0e10cSrcweir 		if( err != osl::FileBase::E_None )
161cdf0e10cSrcweir 			throw io::IOException();
162cdf0e10cSrcweir 		m_bIsOpen = false;
163cdf0e10cSrcweir 	}
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 
167cdf0e10cSrcweir void SAL_CALL
seek(sal_Int64 location)168cdf0e10cSrcweir XInputStream_impl::seek(
169cdf0e10cSrcweir 	sal_Int64 location )
170cdf0e10cSrcweir 	throw( lang::IllegalArgumentException,
171cdf0e10cSrcweir 		   io::IOException,
172cdf0e10cSrcweir 		   uno::RuntimeException )
173cdf0e10cSrcweir {
174cdf0e10cSrcweir 	if( location < 0 )
175cdf0e10cSrcweir 		throw lang::IllegalArgumentException();
176cdf0e10cSrcweir 	if( osl::FileBase::E_None != m_aFile.setPos( Pos_Absolut, sal_uInt64( location ) ) )
177cdf0e10cSrcweir 		throw io::IOException();
178cdf0e10cSrcweir }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 
181cdf0e10cSrcweir sal_Int64 SAL_CALL
getPosition(void)182cdf0e10cSrcweir XInputStream_impl::getPosition(
183cdf0e10cSrcweir 	void )
184cdf0e10cSrcweir 	throw( io::IOException,
185cdf0e10cSrcweir 		   uno::RuntimeException )
186cdf0e10cSrcweir {
187cdf0e10cSrcweir 	sal_uInt64 uPos;
188cdf0e10cSrcweir 	if( osl::FileBase::E_None != m_aFile.getPos( uPos ) )
189cdf0e10cSrcweir 		throw io::IOException();
190cdf0e10cSrcweir 	return sal_Int64( uPos );
191cdf0e10cSrcweir }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir sal_Int64 SAL_CALL
getLength(void)194cdf0e10cSrcweir XInputStream_impl::getLength(
195cdf0e10cSrcweir 	void )
196cdf0e10cSrcweir 	throw( io::IOException,
197cdf0e10cSrcweir 		   uno::RuntimeException )
198cdf0e10cSrcweir {
199cdf0e10cSrcweir 	osl::FileBase::RC	err;
200cdf0e10cSrcweir 	sal_uInt64			uCurrentPos, uEndPos;
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 	err = m_aFile.getPos( uCurrentPos );
203cdf0e10cSrcweir 	if( err != osl::FileBase::E_None )
204cdf0e10cSrcweir 		throw io::IOException();
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 	err = m_aFile.setPos( Pos_End, 0 );
207cdf0e10cSrcweir 	if( err != osl::FileBase::E_None )
208cdf0e10cSrcweir 		throw io::IOException();
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 	err = m_aFile.getPos( uEndPos );
211cdf0e10cSrcweir 	if( err != osl::FileBase::E_None )
212cdf0e10cSrcweir 		throw io::IOException();
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 	err = m_aFile.setPos( Pos_Absolut, uCurrentPos );
215cdf0e10cSrcweir 	if( err != osl::FileBase::E_None )
216cdf0e10cSrcweir 		throw io::IOException();
217cdf0e10cSrcweir 	else
218cdf0e10cSrcweir 		return sal_Int64( uEndPos );
219cdf0e10cSrcweir }
220