xref: /trunk/main/unotools/source/streaming/streamwrap.cxx (revision 45c5a00162811bd03b87fcb4fe9996782f2b59ec)
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_unotools.hxx"
26 #include <unotools/streamwrap.hxx>
27 #include <tools/stream.hxx>
28 #include <tools/debug.hxx>
29 
30 namespace utl
31 {
32 
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::io;
35 using namespace ::com::sun::star::lang;
36 
37 //==================================================================
38 //= OInputStreamWrapper
39 //==================================================================
40 DBG_NAME(OInputStreamWrapper)
41 //------------------------------------------------------------------
42 OInputStreamWrapper::OInputStreamWrapper( SvStream& _rStream )
43                  :m_pSvStream(&_rStream)
44                  ,m_bSvStreamOwner(sal_False)
45 {
46     DBG_CTOR(OInputStreamWrapper,NULL);
47 
48 }
49 
50 //------------------------------------------------------------------
51 OInputStreamWrapper::OInputStreamWrapper( SvStream* pStream, sal_Bool bOwner )
52                  :m_pSvStream( pStream )
53                  ,m_bSvStreamOwner( bOwner )
54 {
55     DBG_CTOR(OInputStreamWrapper,NULL);
56 
57 }
58 
59 //------------------------------------------------------------------
60 OInputStreamWrapper::~OInputStreamWrapper()
61 {
62     if( m_bSvStreamOwner )
63         delete m_pSvStream;
64 
65     DBG_DTOR(OInputStreamWrapper,NULL);
66 }
67 
68 //------------------------------------------------------------------------------
69 sal_Int32 SAL_CALL OInputStreamWrapper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
70 {
71     checkConnected();
72 
73     if (nBytesToRead < 0)
74         throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
75 
76     ::osl::MutexGuard aGuard( m_aMutex );
77 
78     aData.realloc(nBytesToRead);
79 
80     sal_uInt32 nRead = m_pSvStream->Read((void*)aData.getArray(), nBytesToRead);
81     checkError();
82 
83     // Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
84     if (nRead < (sal_uInt32)nBytesToRead)
85         aData.realloc( nRead );
86 
87     return nRead;
88 }
89 
90 //------------------------------------------------------------------------------
91 sal_Int32 SAL_CALL OInputStreamWrapper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead)
92 {
93     checkError();
94 
95     if (nMaxBytesToRead < 0)
96         throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
97 
98     if (m_pSvStream->IsEof())
99     {
100         aData.realloc(0);
101         return 0;
102     }
103     else
104         return readBytes(aData, nMaxBytesToRead);
105 }
106 
107 //------------------------------------------------------------------------------
108 void SAL_CALL OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip)
109 {
110     ::osl::MutexGuard aGuard( m_aMutex );
111     checkError();
112 
113 #ifdef DBG_UTIL
114     sal_uInt32 nCurrentPos = m_pSvStream->Tell();
115 #endif
116 
117     m_pSvStream->SeekRel(nBytesToSkip);
118     checkError();
119 
120 #ifdef DBG_UTIL
121     nCurrentPos = m_pSvStream->Tell();
122 #endif
123 }
124 
125 //------------------------------------------------------------------------------
126 sal_Int32 SAL_CALL OInputStreamWrapper::available()
127 {
128     ::osl::MutexGuard aGuard( m_aMutex );
129     checkConnected();
130 
131     sal_uInt32 nPos = m_pSvStream->Tell();
132     checkError();
133 
134     m_pSvStream->Seek(STREAM_SEEK_TO_END);
135     checkError();
136 
137     sal_Int32 nAvailable = (sal_Int32)m_pSvStream->Tell() - nPos;
138     m_pSvStream->Seek(nPos);
139     checkError();
140 
141     return nAvailable;
142 }
143 
144 //------------------------------------------------------------------------------
145 void SAL_CALL OInputStreamWrapper::closeInput()
146 {
147     ::osl::MutexGuard aGuard( m_aMutex );
148     checkConnected();
149 
150     if (m_bSvStreamOwner)
151         delete m_pSvStream;
152 
153     m_pSvStream = NULL;
154 }
155 
156 //------------------------------------------------------------------------------
157 void OInputStreamWrapper::checkConnected() const
158 {
159     if (!m_pSvStream)
160         throw stario::NotConnectedException(::rtl::OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
161 }
162 
163 //------------------------------------------------------------------------------
164 void OInputStreamWrapper::checkError() const
165 {
166     checkConnected();
167 
168     if (m_pSvStream->SvStream::GetError() != ERRCODE_NONE)
169         // TODO: really evaluate the error
170         throw stario::NotConnectedException(::rtl::OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
171 }
172 
173 //==================================================================
174 //= OSeekableInputStreamWrapper
175 //==================================================================
176 //------------------------------------------------------------------------------
177 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream& _rStream)
178 {
179     SetStream( &_rStream, sal_False );
180 }
181 
182 //------------------------------------------------------------------------------
183 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream* _pStream, sal_Bool _bOwner)
184 {
185     SetStream( _pStream, _bOwner );
186 }
187 
188 //------------------------------------------------------------------------------
189 void SAL_CALL OSeekableInputStreamWrapper::seek( sal_Int64 _nLocation )
190 {
191     ::osl::MutexGuard aGuard( m_aMutex );
192     checkConnected();
193 
194     m_pSvStream->Seek((sal_uInt32)_nLocation);
195     checkError();
196 }
197 
198 //------------------------------------------------------------------------------
199 sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getPosition(  )
200 {
201     ::osl::MutexGuard aGuard( m_aMutex );
202     checkConnected();
203 
204     sal_uInt32 nPos = m_pSvStream->Tell();
205     checkError();
206     return (sal_Int64)nPos;
207 }
208 
209 //------------------------------------------------------------------------------
210 sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getLength(  )
211 {
212     ::osl::MutexGuard aGuard( m_aMutex );
213     checkConnected();
214 
215     sal_uInt32 nCurrentPos = m_pSvStream->Tell();
216     checkError();
217 
218     m_pSvStream->Seek(STREAM_SEEK_TO_END);
219     sal_uInt32 nEndPos = m_pSvStream->Tell();
220     m_pSvStream->Seek(nCurrentPos);
221 
222     checkError();
223 
224     return (sal_Int64)nEndPos;
225 }
226 
227 //==================================================================
228 //= OOutputStreamWrapper
229 //==================================================================
230 //------------------------------------------------------------------------------
231 void SAL_CALL OOutputStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData)
232 {
233     sal_uInt32 nWritten = rStream.Write(aData.getConstArray(),aData.getLength());
234     ErrCode err = rStream.GetError();
235     if  (   (ERRCODE_NONE != err)
236         ||  (nWritten != (sal_uInt32)aData.getLength())
237         )
238     {
239         throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
240     }
241 }
242 
243 //------------------------------------------------------------------
244 void SAL_CALL OOutputStreamWrapper::flush()
245 {
246     rStream.Flush();
247     checkError();
248 }
249 
250 //------------------------------------------------------------------
251 void SAL_CALL OOutputStreamWrapper::closeOutput()
252 {
253 }
254 
255 //------------------------------------------------------------------------------
256 void OOutputStreamWrapper::checkError() const
257 {
258     if (rStream.GetError() != ERRCODE_NONE)
259         // TODO: really evaluate the error
260         throw stario::NotConnectedException(::rtl::OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
261 }
262 
263 //==================================================================
264 //= OSeekableOutputStreamWrapper
265 //==================================================================
266 //------------------------------------------------------------------------------
267 OSeekableOutputStreamWrapper::OSeekableOutputStreamWrapper(SvStream& _rStream)
268     :OOutputStreamWrapper(_rStream)
269 {
270 }
271 
272 //------------------------------------------------------------------------------
273 Any SAL_CALL OSeekableOutputStreamWrapper::queryInterface( const Type& _rType )
274 {
275     Any aReturn = OOutputStreamWrapper::queryInterface(_rType);
276     if (!aReturn.hasValue())
277         aReturn = OSeekableOutputStreamWrapper_Base::queryInterface(_rType);
278     return aReturn;
279 }
280 
281 //------------------------------------------------------------------------------
282 void SAL_CALL OSeekableOutputStreamWrapper::acquire(  ) throw ()
283 {
284     OOutputStreamWrapper::acquire();
285 }
286 
287 //------------------------------------------------------------------------------
288 void SAL_CALL OSeekableOutputStreamWrapper::release(  ) throw ()
289 {
290     OOutputStreamWrapper::release();
291 }
292 
293 //------------------------------------------------------------------------------
294 void SAL_CALL OSeekableOutputStreamWrapper::seek( sal_Int64 _nLocation )
295 {
296     rStream.Seek((sal_uInt32)_nLocation);
297     checkError();
298 }
299 
300 //------------------------------------------------------------------------------
301 sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getPosition(  )
302 {
303     sal_uInt32 nPos = rStream.Tell();
304     checkError();
305     return (sal_Int64)nPos;
306 }
307 
308 //------------------------------------------------------------------------------
309 sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getLength(  )
310 {
311     sal_uInt32 nCurrentPos = rStream.Tell();
312     checkError();
313 
314     rStream.Seek(STREAM_SEEK_TO_END);
315     sal_uInt32 nEndPos = rStream.Tell();
316     rStream.Seek(nCurrentPos);
317 
318     checkError();
319 
320     return (sal_Int64)nEndPos;
321 }
322 
323 //------------------------------------------------------------------------------
324 OStreamWrapper::OStreamWrapper(SvStream& _rStream)
325 {
326     SetStream( &_rStream, sal_False );
327 }
328 
329 //------------------------------------------------------------------------------
330 ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL OStreamWrapper::getInputStream(  )
331 {
332     return this;
333 }
334 
335 //------------------------------------------------------------------------------
336 ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > SAL_CALL OStreamWrapper::getOutputStream(  )
337 {
338     return this;
339 }
340 
341 //------------------------------------------------------------------------------
342 void SAL_CALL OStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData)
343 {
344     sal_uInt32 nWritten = m_pSvStream->Write(aData.getConstArray(),aData.getLength());
345     ErrCode err = m_pSvStream->GetError();
346     if  (   (ERRCODE_NONE != err)
347         ||  (nWritten != (sal_uInt32)aData.getLength())
348         )
349     {
350         throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
351     }
352 }
353 
354 //------------------------------------------------------------------------------
355 void SAL_CALL OStreamWrapper::flush()
356 {
357     m_pSvStream->Flush();
358     if (m_pSvStream->GetError() != ERRCODE_NONE)
359         throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
360 }
361 
362 //------------------------------------------------------------------------------
363 void SAL_CALL OStreamWrapper::closeOutput()
364 {
365 }
366 
367 //------------------------------------------------------------------------------
368 void SAL_CALL OStreamWrapper::truncate()
369 {
370     m_pSvStream->SetStreamSize(0);
371 }
372 
373 } // namespace utl
374