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