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