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