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 4249 : OInputStreamWrapper::OInputStreamWrapper( SvStream& _rStream )
31 : :m_pSvStream(&_rStream)
32 4249 : ,m_bSvStreamOwner(false)
33 : {
34 4249 : }
35 :
36 74 : OInputStreamWrapper::OInputStreamWrapper( SvStream* pStream, bool bOwner )
37 : :m_pSvStream( pStream )
38 74 : ,m_bSvStreamOwner( bOwner )
39 : {
40 74 : }
41 :
42 17701 : OInputStreamWrapper::~OInputStreamWrapper()
43 : {
44 6689 : if( m_bSvStreamOwner )
45 8 : delete m_pSvStream;
46 11012 : }
47 :
48 34697 : 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 34697 : checkConnected();
52 :
53 34697 : if (nBytesToRead < 0)
54 0 : throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
55 :
56 34697 : ::osl::MutexGuard aGuard( m_aMutex );
57 :
58 34697 : aData.realloc(nBytesToRead);
59 :
60 34697 : sal_uInt32 nRead = m_pSvStream->Read((void*)aData.getArray(), nBytesToRead);
61 34697 : checkError();
62 :
63 : // Wenn gelesene Zeichen < MaxLength, css::uno::Sequence anpassen
64 34697 : if (nRead < (sal_uInt32)nBytesToRead)
65 1469 : aData.realloc( nRead );
66 :
67 34697 : return nRead;
68 : }
69 :
70 310 : 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 310 : checkError();
73 :
74 310 : if (nMaxBytesToRead < 0)
75 0 : throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
76 :
77 310 : if (m_pSvStream->IsEof())
78 : {
79 8 : aData.realloc(0);
80 8 : return 0;
81 : }
82 : else
83 302 : return readBytes(aData, nMaxBytesToRead);
84 : }
85 :
86 1350 : void SAL_CALL OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
87 : {
88 1350 : ::osl::MutexGuard aGuard( m_aMutex );
89 1350 : checkError();
90 :
91 1350 : m_pSvStream->SeekRel(nBytesToSkip);
92 1350 : checkError();
93 1350 : }
94 :
95 0 : sal_Int32 SAL_CALL OInputStreamWrapper::available() throw( css::io::NotConnectedException, css::uno::RuntimeException, std::exception )
96 : {
97 0 : ::osl::MutexGuard aGuard( m_aMutex );
98 0 : checkConnected();
99 :
100 0 : sal_uInt32 nPos = m_pSvStream->Tell();
101 0 : checkError();
102 :
103 0 : m_pSvStream->Seek(STREAM_SEEK_TO_END);
104 0 : checkError();
105 :
106 0 : sal_Int32 nAvailable = (sal_Int32)m_pSvStream->Tell() - nPos;
107 0 : m_pSvStream->Seek(nPos);
108 0 : checkError();
109 :
110 0 : return nAvailable;
111 : }
112 :
113 184 : void SAL_CALL OInputStreamWrapper::closeInput() throw( css::io::NotConnectedException, css::uno::RuntimeException, std::exception )
114 : {
115 184 : ::osl::MutexGuard aGuard( m_aMutex );
116 184 : checkConnected();
117 :
118 184 : if (m_bSvStreamOwner)
119 0 : delete m_pSvStream;
120 :
121 184 : m_pSvStream = NULL;
122 184 : }
123 :
124 3336688 : void OInputStreamWrapper::checkConnected() const
125 : {
126 3336688 : if (!m_pSvStream)
127 0 : throw css::io::NotConnectedException(OUString(), const_cast<css::uno::XWeak*>(static_cast<const css::uno::XWeak*>(this)));
128 3336688 : }
129 :
130 1673531 : void OInputStreamWrapper::checkError() const
131 : {
132 1673531 : checkConnected();
133 :
134 1673531 : 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 1673531 : }
138 :
139 : //= OSeekableInputStreamWrapper
140 :
141 1068 : OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream& _rStream)
142 : {
143 1068 : SetStream( &_rStream, false );
144 1068 : }
145 :
146 158 : OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream* _pStream, bool _bOwner)
147 : {
148 158 : SetStream( _pStream, _bOwner );
149 158 : }
150 :
151 17940 : void SAL_CALL OSeekableInputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception)
152 : {
153 17940 : ::osl::MutexGuard aGuard( m_aMutex );
154 17940 : checkConnected();
155 :
156 17940 : m_pSvStream->Seek((sal_uInt32)_nLocation);
157 17940 : checkError();
158 17940 : }
159 :
160 1602788 : sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getPosition( ) throw (IOException, RuntimeException, std::exception)
161 : {
162 1602788 : ::osl::MutexGuard aGuard( m_aMutex );
163 1602788 : checkConnected();
164 :
165 1602788 : sal_uInt32 nPos = m_pSvStream->Tell();
166 1602788 : checkError();
167 1602788 : return (sal_Int64)nPos;
168 : }
169 :
170 7548 : sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getLength( ) throw (IOException, RuntimeException, std::exception)
171 : {
172 7548 : ::osl::MutexGuard aGuard( m_aMutex );
173 7548 : checkConnected();
174 :
175 7548 : sal_uInt32 nCurrentPos = m_pSvStream->Tell();
176 7548 : checkError();
177 :
178 7548 : m_pSvStream->Seek(STREAM_SEEK_TO_END);
179 7548 : sal_uInt32 nEndPos = m_pSvStream->Tell();
180 7548 : m_pSvStream->Seek(nCurrentPos);
181 :
182 7548 : checkError();
183 :
184 7548 : return (sal_Int64)nEndPos;
185 : }
186 :
187 : //= OOutputStreamWrapper
188 :
189 1556 : OOutputStreamWrapper::OOutputStreamWrapper(SvStream& _rStream):
190 1556 : rStream(_rStream)
191 1556 : {}
192 :
193 3106 : OOutputStreamWrapper::~OOutputStreamWrapper() {}
194 :
195 1972 : 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 1972 : sal_uInt32 nWritten = rStream.Write(aData.getConstArray(),aData.getLength());
198 1972 : ErrCode err = rStream.GetError();
199 1972 : if ( (ERRCODE_NONE != err)
200 1972 : || (nWritten != (sal_uInt32)aData.getLength())
201 : )
202 : {
203 0 : throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
204 : }
205 1972 : }
206 :
207 6 : void SAL_CALL OOutputStreamWrapper::flush() throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
208 : {
209 6 : rStream.Flush();
210 6 : checkError();
211 6 : }
212 :
213 496 : void SAL_CALL OOutputStreamWrapper::closeOutput() throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
214 : {
215 496 : }
216 :
217 124 : void OOutputStreamWrapper::checkError() const
218 : {
219 124 : 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 124 : }
223 :
224 : //= OSeekableOutputStreamWrapper
225 :
226 6 : OSeekableOutputStreamWrapper::OSeekableOutputStreamWrapper(SvStream& _rStream)
227 6 : :OOutputStreamWrapper(_rStream)
228 : {
229 6 : }
230 :
231 12 : OSeekableOutputStreamWrapper::~OSeekableOutputStreamWrapper() {}
232 :
233 12 : Any SAL_CALL OSeekableOutputStreamWrapper::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
234 : {
235 12 : Any aReturn = OOutputStreamWrapper::queryInterface(_rType);
236 12 : if (!aReturn.hasValue())
237 12 : aReturn = OSeekableOutputStreamWrapper_Base::queryInterface(_rType);
238 12 : return aReturn;
239 : }
240 :
241 42 : void SAL_CALL OSeekableOutputStreamWrapper::acquire( ) throw ()
242 : {
243 42 : OOutputStreamWrapper::acquire();
244 42 : }
245 :
246 42 : void SAL_CALL OSeekableOutputStreamWrapper::release( ) throw ()
247 : {
248 42 : OOutputStreamWrapper::release();
249 42 : }
250 :
251 8 : void SAL_CALL OSeekableOutputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception)
252 : {
253 8 : rStream.Seek((sal_uInt32)_nLocation);
254 8 : checkError();
255 8 : }
256 :
257 102 : sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getPosition( ) throw (IOException, RuntimeException, std::exception)
258 : {
259 102 : sal_uInt32 nPos = rStream.Tell();
260 102 : checkError();
261 102 : return (sal_Int64)nPos;
262 : }
263 :
264 4 : sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getLength( ) throw (IOException, RuntimeException, std::exception)
265 : {
266 4 : sal_uInt32 nCurrentPos = rStream.Tell();
267 4 : checkError();
268 :
269 4 : rStream.Seek(STREAM_SEEK_TO_END);
270 4 : sal_uInt32 nEndPos = rStream.Tell();
271 4 : rStream.Seek(nCurrentPos);
272 :
273 4 : checkError();
274 :
275 4 : return (sal_Int64)nEndPos;
276 : }
277 :
278 1142 : OStreamWrapper::OStreamWrapper(SvStream& _rStream)
279 : {
280 1142 : SetStream( &_rStream, false );
281 1142 : }
282 :
283 3092 : css::uno::Reference< css::io::XInputStream > SAL_CALL OStreamWrapper::getInputStream( ) throw (css::uno::RuntimeException, std::exception)
284 : {
285 3092 : return this;
286 : }
287 :
288 2292 : css::uno::Reference< css::io::XOutputStream > SAL_CALL OStreamWrapper::getOutputStream( ) throw (css::uno::RuntimeException, std::exception)
289 : {
290 2292 : return this;
291 : }
292 :
293 541258 : 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 541258 : sal_uInt32 nWritten = m_pSvStream->Write(aData.getConstArray(),aData.getLength());
296 541258 : ErrCode err = m_pSvStream->GetError();
297 541258 : if ( (ERRCODE_NONE != err)
298 541258 : || (nWritten != (sal_uInt32)aData.getLength())
299 : )
300 : {
301 0 : throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
302 : }
303 541258 : }
304 :
305 1972 : void SAL_CALL OStreamWrapper::flush() throw(css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception)
306 : {
307 1972 : m_pSvStream->Flush();
308 1972 : if (m_pSvStream->GetError() != ERRCODE_NONE)
309 0 : throw css::io::NotConnectedException(OUString(),static_cast<css::uno::XWeak*>(this));
310 1972 : }
311 :
312 54 : void SAL_CALL OStreamWrapper::closeOutput() throw(css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception)
313 : {
314 54 : }
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: */
|