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 :
23 : namespace utl
24 : {
25 :
26 : using namespace ::com::sun::star::uno;
27 : using namespace ::com::sun::star::io;
28 : using namespace ::com::sun::star::lang;
29 :
30 3940 : OInputStreamWrapper::OInputStreamWrapper( SvStream& _rStream )
31 : :m_pSvStream(&_rStream)
32 3940 : ,m_bSvStreamOwner(false)
33 : {
34 3940 : }
35 :
36 49 : OInputStreamWrapper::OInputStreamWrapper( SvStream* pStream, bool bOwner )
37 : :m_pSvStream( pStream )
38 49 : ,m_bSvStreamOwner( bOwner )
39 : {
40 49 : }
41 :
42 14767 : OInputStreamWrapper::~OInputStreamWrapper()
43 : {
44 5389 : if( m_bSvStreamOwner )
45 10 : delete m_pSvStream;
46 9378 : }
47 :
48 27359 : sal_Int32 SAL_CALL OInputStreamWrapper::readBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
49 : throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
50 : {
51 27359 : checkConnected();
52 :
53 27359 : if (nBytesToRead < 0)
54 0 : throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
55 :
56 27359 : ::osl::MutexGuard aGuard( m_aMutex );
57 :
58 27359 : aData.realloc(nBytesToRead);
59 :
60 27359 : sal_uInt32 nRead = m_pSvStream->Read(static_cast<void*>(aData.getArray()), nBytesToRead);
61 27359 : checkError();
62 :
63 : // Wenn gelesene Zeichen < MaxLength, css::uno::Sequence anpassen
64 27359 : if (nRead < (sal_uInt32)nBytesToRead)
65 846 : aData.realloc( nRead );
66 :
67 27359 : return nRead;
68 : }
69 :
70 182 : sal_Int32 SAL_CALL OInputStreamWrapper::readSomeBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
71 : {
72 182 : checkError();
73 :
74 182 : if (nMaxBytesToRead < 0)
75 0 : throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
76 :
77 182 : if (m_pSvStream->IsEof())
78 : {
79 9 : aData.realloc(0);
80 9 : return 0;
81 : }
82 : else
83 173 : return readBytes(aData, nMaxBytesToRead);
84 : }
85 :
86 675 : void SAL_CALL OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
87 : {
88 675 : ::osl::MutexGuard aGuard( m_aMutex );
89 675 : checkError();
90 :
91 675 : m_pSvStream->SeekRel(nBytesToSkip);
92 675 : checkError();
93 675 : }
94 :
95 6 : sal_Int32 SAL_CALL OInputStreamWrapper::available() throw( css::io::NotConnectedException, css::uno::RuntimeException, std::exception )
96 : {
97 6 : ::osl::MutexGuard aGuard( m_aMutex );
98 6 : checkConnected();
99 :
100 6 : sal_uInt32 nPos = m_pSvStream->Tell();
101 6 : checkError();
102 :
103 6 : m_pSvStream->Seek(STREAM_SEEK_TO_END);
104 6 : checkError();
105 :
106 6 : sal_Int32 nAvailable = (sal_Int32)m_pSvStream->Tell() - nPos;
107 6 : m_pSvStream->Seek(nPos);
108 6 : checkError();
109 :
110 6 : return nAvailable;
111 : }
112 :
113 100 : void SAL_CALL OInputStreamWrapper::closeInput() throw( css::io::NotConnectedException, css::uno::RuntimeException, std::exception )
114 : {
115 100 : ::osl::MutexGuard aGuard( m_aMutex );
116 100 : checkConnected();
117 :
118 100 : if (m_bSvStreamOwner)
119 0 : delete m_pSvStream;
120 :
121 100 : m_pSvStream = NULL;
122 100 : }
123 :
124 1719252 : void OInputStreamWrapper::checkConnected() const
125 : {
126 1719252 : if (!m_pSvStream)
127 0 : throw css::io::NotConnectedException(OUString(), const_cast<css::uno::XWeak*>(static_cast<const css::uno::XWeak*>(this)));
128 1719252 : }
129 :
130 864071 : void OInputStreamWrapper::checkError() const
131 : {
132 864071 : checkConnected();
133 :
134 864071 : if (m_pSvStream->SvStream::GetError() != ERRCODE_NONE)
135 : // TODO: really evaluate the error
136 0 : throw css::io::NotConnectedException(OUString(), const_cast<css::uno::XWeak*>(static_cast<const css::uno::XWeak*>(this)));
137 864071 : }
138 :
139 : //= OSeekableInputStreamWrapper
140 :
141 593 : OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream& _rStream)
142 : {
143 593 : SetStream( &_rStream, false );
144 593 : }
145 :
146 96 : OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream* _pStream, bool _bOwner)
147 : {
148 96 : SetStream( _pStream, _bOwner );
149 96 : }
150 :
151 12714 : void SAL_CALL OSeekableInputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception)
152 : {
153 12714 : ::osl::MutexGuard aGuard( m_aMutex );
154 12714 : checkConnected();
155 :
156 12714 : m_pSvStream->Seek((sal_uInt32)_nLocation);
157 12714 : checkError();
158 12714 : }
159 :
160 807556 : sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getPosition( ) throw (IOException, RuntimeException, std::exception)
161 : {
162 807556 : ::osl::MutexGuard aGuard( m_aMutex );
163 807556 : checkConnected();
164 :
165 807556 : sal_uInt32 nPos = m_pSvStream->Tell();
166 807556 : checkError();
167 807556 : return (sal_Int64)nPos;
168 : }
169 :
170 7446 : sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getLength( ) throw (IOException, RuntimeException, std::exception)
171 : {
172 7446 : ::osl::MutexGuard aGuard( m_aMutex );
173 7446 : checkConnected();
174 :
175 7446 : sal_uInt32 nCurrentPos = m_pSvStream->Tell();
176 7446 : checkError();
177 :
178 7446 : m_pSvStream->Seek(STREAM_SEEK_TO_END);
179 7446 : sal_uInt32 nEndPos = m_pSvStream->Tell();
180 7446 : m_pSvStream->Seek(nCurrentPos);
181 :
182 7446 : checkError();
183 :
184 7446 : return (sal_Int64)nEndPos;
185 : }
186 :
187 : //= OOutputStreamWrapper
188 :
189 919 : OOutputStreamWrapper::OOutputStreamWrapper(SvStream& _rStream):
190 919 : rStream(_rStream)
191 919 : {}
192 :
193 1831 : OOutputStreamWrapper::~OOutputStreamWrapper() {}
194 :
195 1006 : void SAL_CALL OOutputStreamWrapper::writeBytes(const css::uno::Sequence< sal_Int8 >& aData) throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
196 : {
197 1006 : sal_uInt32 nWritten = rStream.Write(aData.getConstArray(),aData.getLength());
198 1006 : ErrCode err = rStream.GetError();
199 1006 : if ( (ERRCODE_NONE != err)
200 1006 : || (nWritten != (sal_uInt32)aData.getLength())
201 : )
202 : {
203 0 : throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
204 : }
205 1006 : }
206 :
207 7 : void SAL_CALL OOutputStreamWrapper::flush() throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
208 : {
209 7 : rStream.Flush();
210 7 : checkError();
211 7 : }
212 :
213 262 : void SAL_CALL OOutputStreamWrapper::closeOutput() throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
214 : {
215 262 : }
216 :
217 66 : void OOutputStreamWrapper::checkError() const
218 : {
219 66 : if (rStream.GetError() != ERRCODE_NONE)
220 : // TODO: really evaluate the error
221 0 : throw css::io::NotConnectedException(OUString(), const_cast<css::uno::XWeak*>(static_cast<const css::uno::XWeak*>(this)));
222 66 : }
223 :
224 : //= OSeekableOutputStreamWrapper
225 :
226 3 : OSeekableOutputStreamWrapper::OSeekableOutputStreamWrapper(SvStream& _rStream)
227 3 : :OOutputStreamWrapper(_rStream)
228 : {
229 3 : }
230 :
231 6 : OSeekableOutputStreamWrapper::~OSeekableOutputStreamWrapper() {}
232 :
233 6 : Any SAL_CALL OSeekableOutputStreamWrapper::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
234 : {
235 6 : Any aReturn = OOutputStreamWrapper::queryInterface(_rType);
236 6 : if (!aReturn.hasValue())
237 6 : aReturn = OSeekableOutputStreamWrapper_Base::queryInterface(_rType);
238 6 : return aReturn;
239 : }
240 :
241 21 : void SAL_CALL OSeekableOutputStreamWrapper::acquire( ) throw ()
242 : {
243 21 : OOutputStreamWrapper::acquire();
244 21 : }
245 :
246 21 : void SAL_CALL OSeekableOutputStreamWrapper::release( ) throw ()
247 : {
248 21 : OOutputStreamWrapper::release();
249 21 : }
250 :
251 4 : void SAL_CALL OSeekableOutputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception)
252 : {
253 4 : rStream.Seek((sal_uInt32)_nLocation);
254 4 : checkError();
255 4 : }
256 :
257 51 : sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getPosition( ) throw (IOException, RuntimeException, std::exception)
258 : {
259 51 : sal_uInt32 nPos = rStream.Tell();
260 51 : checkError();
261 51 : return (sal_Int64)nPos;
262 : }
263 :
264 2 : sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getLength( ) throw (IOException, RuntimeException, std::exception)
265 : {
266 2 : sal_uInt32 nCurrentPos = rStream.Tell();
267 2 : checkError();
268 :
269 2 : rStream.Seek(STREAM_SEEK_TO_END);
270 2 : sal_uInt32 nEndPos = rStream.Tell();
271 2 : rStream.Seek(nCurrentPos);
272 :
273 2 : checkError();
274 :
275 2 : return (sal_Int64)nEndPos;
276 : }
277 :
278 712 : OStreamWrapper::OStreamWrapper(SvStream& _rStream)
279 : {
280 712 : SetStream( &_rStream, false );
281 712 : }
282 :
283 2039 : css::uno::Reference< css::io::XInputStream > SAL_CALL OStreamWrapper::getInputStream( ) throw (css::uno::RuntimeException, std::exception)
284 : {
285 2039 : return this;
286 : }
287 :
288 1508 : css::uno::Reference< css::io::XOutputStream > SAL_CALL OStreamWrapper::getOutputStream( ) throw (css::uno::RuntimeException, std::exception)
289 : {
290 1508 : return this;
291 : }
292 :
293 326336 : void SAL_CALL OStreamWrapper::writeBytes(const css::uno::Sequence< sal_Int8 >& aData) throw(css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception)
294 : {
295 326336 : sal_uInt32 nWritten = m_pSvStream->Write(aData.getConstArray(),aData.getLength());
296 326336 : ErrCode err = m_pSvStream->GetError();
297 326336 : if ( (ERRCODE_NONE != err)
298 326336 : || (nWritten != (sal_uInt32)aData.getLength())
299 : )
300 : {
301 0 : throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
302 : }
303 326336 : }
304 :
305 1223 : void SAL_CALL OStreamWrapper::flush() throw(css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception)
306 : {
307 1223 : m_pSvStream->Flush();
308 1223 : if (m_pSvStream->GetError() != ERRCODE_NONE)
309 0 : throw css::io::NotConnectedException(OUString(),static_cast<css::uno::XWeak*>(this));
310 1223 : }
311 :
312 40 : void SAL_CALL OStreamWrapper::closeOutput() throw(css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception)
313 : {
314 40 : }
315 :
316 0 : void SAL_CALL OStreamWrapper::truncate() throw(css::io::IOException, css::uno::RuntimeException, std::exception)
317 : {
318 0 : m_pSvStream->SetStreamSize(0);
319 0 : }
320 :
321 : } // namespace utl
322 :
323 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|