Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <unotools/streamwrap.hxx>
21 : #include <tools/stream.hxx>
22 : #include <tools/debug.hxx>
23 :
24 : namespace utl
25 : {
26 :
27 : using namespace ::com::sun::star::uno;
28 : using namespace ::com::sun::star::io;
29 : using namespace ::com::sun::star::lang;
30 :
31 7616 : OInputStreamWrapper::OInputStreamWrapper( SvStream& _rStream )
32 : :m_pSvStream(&_rStream)
33 7616 : ,m_bSvStreamOwner(false)
34 : {
35 7616 : }
36 :
37 22 : OInputStreamWrapper::OInputStreamWrapper( SvStream* pStream, bool bOwner )
38 : :m_pSvStream( pStream )
39 22 : ,m_bSvStreamOwner( bOwner )
40 : {
41 22 : }
42 :
43 24578 : OInputStreamWrapper::~OInputStreamWrapper()
44 : {
45 8470 : if( m_bSvStreamOwner )
46 4 : delete m_pSvStream;
47 16108 : }
48 :
49 10189 : sal_Int32 SAL_CALL OInputStreamWrapper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
50 : throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException, std::exception )
51 : {
52 10189 : checkConnected();
53 :
54 10189 : if (nBytesToRead < 0)
55 0 : throw stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak*>(this));
56 :
57 10189 : ::osl::MutexGuard aGuard( m_aMutex );
58 :
59 10189 : aData.realloc(nBytesToRead);
60 :
61 10189 : sal_uInt32 nRead = m_pSvStream->Read((void*)aData.getArray(), nBytesToRead);
62 10189 : checkError();
63 :
64 : // Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
65 10189 : if (nRead < (sal_uInt32)nBytesToRead)
66 676 : aData.realloc( nRead );
67 :
68 10189 : return nRead;
69 : }
70 :
71 8 : sal_Int32 SAL_CALL OInputStreamWrapper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException, std::exception )
72 : {
73 8 : checkError();
74 :
75 8 : if (nMaxBytesToRead < 0)
76 0 : throw stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak*>(this));
77 :
78 8 : if (m_pSvStream->IsEof())
79 : {
80 4 : aData.realloc(0);
81 4 : return 0;
82 : }
83 : else
84 4 : return readBytes(aData, nMaxBytesToRead);
85 : }
86 :
87 570 : void SAL_CALL OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException, std::exception )
88 : {
89 570 : ::osl::MutexGuard aGuard( m_aMutex );
90 570 : checkError();
91 :
92 570 : m_pSvStream->SeekRel(nBytesToSkip);
93 570 : checkError();
94 570 : }
95 :
96 0 : sal_Int32 SAL_CALL OInputStreamWrapper::available() throw( stario::NotConnectedException, staruno::RuntimeException, std::exception )
97 : {
98 0 : ::osl::MutexGuard aGuard( m_aMutex );
99 0 : checkConnected();
100 :
101 0 : sal_uInt32 nPos = m_pSvStream->Tell();
102 0 : checkError();
103 :
104 0 : m_pSvStream->Seek(STREAM_SEEK_TO_END);
105 0 : checkError();
106 :
107 0 : sal_Int32 nAvailable = (sal_Int32)m_pSvStream->Tell() - nPos;
108 0 : m_pSvStream->Seek(nPos);
109 0 : checkError();
110 :
111 0 : return nAvailable;
112 : }
113 :
114 77 : void SAL_CALL OInputStreamWrapper::closeInput() throw( stario::NotConnectedException, staruno::RuntimeException, std::exception )
115 : {
116 77 : ::osl::MutexGuard aGuard( m_aMutex );
117 77 : checkConnected();
118 :
119 77 : if (m_bSvStreamOwner)
120 0 : delete m_pSvStream;
121 :
122 77 : m_pSvStream = NULL;
123 77 : }
124 :
125 49764 : void OInputStreamWrapper::checkConnected() const
126 : {
127 49764 : if (!m_pSvStream)
128 0 : throw stario::NotConnectedException(OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
129 49764 : }
130 :
131 26583 : void OInputStreamWrapper::checkError() const
132 : {
133 26583 : checkConnected();
134 :
135 26583 : if (m_pSvStream->SvStream::GetError() != ERRCODE_NONE)
136 : // TODO: really evaluate the error
137 0 : throw stario::NotConnectedException(OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
138 26583 : }
139 :
140 : //= OSeekableInputStreamWrapper
141 :
142 465 : OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream& _rStream)
143 : {
144 465 : SetStream( &_rStream, false );
145 465 : }
146 :
147 1 : OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream* _pStream, bool _bOwner)
148 : {
149 1 : SetStream( _pStream, _bOwner );
150 1 : }
151 :
152 4259 : void SAL_CALL OSeekableInputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception)
153 : {
154 4259 : ::osl::MutexGuard aGuard( m_aMutex );
155 4259 : checkConnected();
156 :
157 4259 : m_pSvStream->Seek((sal_uInt32)_nLocation);
158 4259 : checkError();
159 4259 : }
160 :
161 6325 : sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getPosition( ) throw (IOException, RuntimeException, std::exception)
162 : {
163 6325 : ::osl::MutexGuard aGuard( m_aMutex );
164 6325 : checkConnected();
165 :
166 6325 : sal_uInt32 nPos = m_pSvStream->Tell();
167 6325 : checkError();
168 6325 : return (sal_Int64)nPos;
169 : }
170 :
171 2331 : sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getLength( ) throw (IOException, RuntimeException, std::exception)
172 : {
173 2331 : ::osl::MutexGuard aGuard( m_aMutex );
174 2331 : checkConnected();
175 :
176 2331 : sal_uInt32 nCurrentPos = m_pSvStream->Tell();
177 2331 : checkError();
178 :
179 2331 : m_pSvStream->Seek(STREAM_SEEK_TO_END);
180 2331 : sal_uInt32 nEndPos = m_pSvStream->Tell();
181 2331 : m_pSvStream->Seek(nCurrentPos);
182 :
183 2331 : checkError();
184 :
185 2331 : return (sal_Int64)nEndPos;
186 : }
187 :
188 : //= OOutputStreamWrapper
189 :
190 494 : OOutputStreamWrapper::OOutputStreamWrapper(SvStream& _rStream):
191 494 : rStream(_rStream)
192 494 : {}
193 :
194 985 : OOutputStreamWrapper::~OOutputStreamWrapper() {}
195 :
196 458 : void SAL_CALL OOutputStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException, std::exception )
197 : {
198 458 : sal_uInt32 nWritten = rStream.Write(aData.getConstArray(),aData.getLength());
199 458 : ErrCode err = rStream.GetError();
200 458 : if ( (ERRCODE_NONE != err)
201 458 : || (nWritten != (sal_uInt32)aData.getLength())
202 : )
203 : {
204 0 : throw stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak*>(this));
205 : }
206 458 : }
207 :
208 3 : void SAL_CALL OOutputStreamWrapper::flush() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException, std::exception )
209 : {
210 3 : rStream.Flush();
211 3 : checkError();
212 3 : }
213 :
214 146 : void SAL_CALL OOutputStreamWrapper::closeOutput() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException, std::exception )
215 : {
216 146 : }
217 :
218 62 : void OOutputStreamWrapper::checkError() const
219 : {
220 62 : if (rStream.GetError() != ERRCODE_NONE)
221 : // TODO: really evaluate the error
222 0 : throw stario::NotConnectedException(OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
223 62 : }
224 :
225 : //= OSeekableOutputStreamWrapper
226 :
227 3 : OSeekableOutputStreamWrapper::OSeekableOutputStreamWrapper(SvStream& _rStream)
228 3 : :OOutputStreamWrapper(_rStream)
229 : {
230 3 : }
231 :
232 6 : OSeekableOutputStreamWrapper::~OSeekableOutputStreamWrapper() {}
233 :
234 6 : Any SAL_CALL OSeekableOutputStreamWrapper::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
235 : {
236 6 : Any aReturn = OOutputStreamWrapper::queryInterface(_rType);
237 6 : if (!aReturn.hasValue())
238 6 : aReturn = OSeekableOutputStreamWrapper_Base::queryInterface(_rType);
239 6 : return aReturn;
240 : }
241 :
242 21 : void SAL_CALL OSeekableOutputStreamWrapper::acquire( ) throw ()
243 : {
244 21 : OOutputStreamWrapper::acquire();
245 21 : }
246 :
247 21 : void SAL_CALL OSeekableOutputStreamWrapper::release( ) throw ()
248 : {
249 21 : OOutputStreamWrapper::release();
250 21 : }
251 :
252 4 : void SAL_CALL OSeekableOutputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception)
253 : {
254 4 : rStream.Seek((sal_uInt32)_nLocation);
255 4 : checkError();
256 4 : }
257 :
258 51 : sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getPosition( ) throw (IOException, RuntimeException, std::exception)
259 : {
260 51 : sal_uInt32 nPos = rStream.Tell();
261 51 : checkError();
262 51 : return (sal_Int64)nPos;
263 : }
264 :
265 2 : sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getLength( ) throw (IOException, RuntimeException, std::exception)
266 : {
267 2 : sal_uInt32 nCurrentPos = rStream.Tell();
268 2 : checkError();
269 :
270 2 : rStream.Seek(STREAM_SEEK_TO_END);
271 2 : sal_uInt32 nEndPos = rStream.Tell();
272 2 : rStream.Seek(nCurrentPos);
273 :
274 2 : checkError();
275 :
276 2 : return (sal_Int64)nEndPos;
277 : }
278 :
279 368 : OStreamWrapper::OStreamWrapper(SvStream& _rStream)
280 : {
281 368 : SetStream( &_rStream, false );
282 368 : }
283 :
284 993 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL OStreamWrapper::getInputStream( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
285 : {
286 993 : return this;
287 : }
288 :
289 741 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > SAL_CALL OStreamWrapper::getOutputStream( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
290 : {
291 741 : return this;
292 : }
293 :
294 169830 : void SAL_CALL OStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException, std::exception)
295 : {
296 169830 : sal_uInt32 nWritten = m_pSvStream->Write(aData.getConstArray(),aData.getLength());
297 169830 : ErrCode err = m_pSvStream->GetError();
298 169830 : if ( (ERRCODE_NONE != err)
299 169830 : || (nWritten != (sal_uInt32)aData.getLength())
300 : )
301 : {
302 0 : throw stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak*>(this));
303 : }
304 169830 : }
305 :
306 635 : void SAL_CALL OStreamWrapper::flush() throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException, std::exception)
307 : {
308 635 : m_pSvStream->Flush();
309 635 : if (m_pSvStream->GetError() != ERRCODE_NONE)
310 0 : throw stario::NotConnectedException(OUString(),static_cast<staruno::XWeak*>(this));
311 635 : }
312 :
313 10 : void SAL_CALL OStreamWrapper::closeOutput() throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException, std::exception)
314 : {
315 10 : }
316 :
317 0 : void SAL_CALL OStreamWrapper::truncate() throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception)
318 : {
319 0 : m_pSvStream->SetStreamSize(0);
320 0 : }
321 :
322 : } // namespace utl
323 :
324 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|