cURL: follow redirects
[LibreOffice.git] / unotools / source / streaming / streamwrap.cxx
blob92f7aa4165d22f1c0340ac797f62d2704d7bbae2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <unotools/streamwrap.hxx>
21 #include <tools/stream.hxx>
23 namespace utl
26 using namespace ::com::sun::star::uno;
27 using namespace ::com::sun::star::io;
28 using namespace ::com::sun::star::lang;
30 OInputStreamWrapper::OInputStreamWrapper( SvStream& _rStream )
31 :m_pSvStream(&_rStream)
32 ,m_bSvStreamOwner(false)
36 OInputStreamWrapper::OInputStreamWrapper( SvStream* pStream, bool bOwner )
37 :m_pSvStream( pStream )
38 ,m_bSvStreamOwner( bOwner )
42 OInputStreamWrapper::~OInputStreamWrapper()
44 if( m_bSvStreamOwner )
45 delete m_pSvStream;
48 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 )
51 checkConnected();
53 if (nBytesToRead < 0)
54 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
56 ::osl::MutexGuard aGuard( m_aMutex );
58 if (aData.getLength() < nBytesToRead)
59 aData.realloc(nBytesToRead);
61 sal_uInt32 nRead = m_pSvStream->ReadBytes(static_cast<void*>(aData.getArray()), nBytesToRead);
62 checkError();
64 // Wenn gelesene Zeichen < MaxLength, css::uno::Sequence anpassen
65 if (nRead < (std::size_t)aData.getLength())
66 aData.realloc( nRead );
68 return nRead;
71 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 )
73 checkError();
75 if (nMaxBytesToRead < 0)
76 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
78 if (m_pSvStream->IsEof())
80 aData.realloc(0);
81 return 0;
83 else
84 return readBytes(aData, nMaxBytesToRead);
87 void SAL_CALL OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
89 ::osl::MutexGuard aGuard( m_aMutex );
90 checkError();
92 m_pSvStream->SeekRel(nBytesToSkip);
93 checkError();
96 sal_Int32 SAL_CALL OInputStreamWrapper::available() throw( css::io::NotConnectedException, css::uno::RuntimeException, std::exception )
98 ::osl::MutexGuard aGuard( m_aMutex );
99 checkConnected();
101 sal_uInt32 nPos = m_pSvStream->Tell();
102 checkError();
104 m_pSvStream->Seek(STREAM_SEEK_TO_END);
105 checkError();
107 sal_Int32 nAvailable = (sal_Int32)m_pSvStream->Tell() - nPos;
108 m_pSvStream->Seek(nPos);
109 checkError();
111 return nAvailable;
114 void SAL_CALL OInputStreamWrapper::closeInput() throw( css::io::NotConnectedException, css::uno::RuntimeException, std::exception )
116 ::osl::MutexGuard aGuard( m_aMutex );
117 checkConnected();
119 if (m_bSvStreamOwner)
120 delete m_pSvStream;
122 m_pSvStream = nullptr;
125 void OInputStreamWrapper::checkConnected() const
127 if (!m_pSvStream)
128 throw css::io::NotConnectedException(OUString(), const_cast<css::uno::XWeak*>(static_cast<const css::uno::XWeak*>(this)));
131 void OInputStreamWrapper::checkError() const
133 checkConnected();
135 if (m_pSvStream->SvStream::GetError() != ERRCODE_NONE)
136 // TODO: really evaluate the error
137 throw css::io::NotConnectedException(OUString(), const_cast<css::uno::XWeak*>(static_cast<const css::uno::XWeak*>(this)));
140 //= OSeekableInputStreamWrapper
142 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream& _rStream)
144 SetStream( &_rStream, false );
147 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream* _pStream, bool _bOwner)
149 SetStream( _pStream, _bOwner );
152 void SAL_CALL OSeekableInputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception)
154 ::osl::MutexGuard aGuard( m_aMutex );
155 checkConnected();
157 m_pSvStream->Seek((sal_uInt32)_nLocation);
158 checkError();
161 sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getPosition( ) throw (IOException, RuntimeException, std::exception)
163 ::osl::MutexGuard aGuard( m_aMutex );
164 checkConnected();
166 sal_uInt32 nPos = m_pSvStream->Tell();
167 checkError();
168 return (sal_Int64)nPos;
171 sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getLength( ) throw (IOException, RuntimeException, std::exception)
173 ::osl::MutexGuard aGuard( m_aMutex );
174 checkConnected();
176 sal_uInt32 nCurrentPos = m_pSvStream->Tell();
177 checkError();
179 m_pSvStream->Seek(STREAM_SEEK_TO_END);
180 sal_uInt32 nEndPos = m_pSvStream->Tell();
181 m_pSvStream->Seek(nCurrentPos);
183 checkError();
185 return (sal_Int64)nEndPos;
188 //= OOutputStreamWrapper
190 OOutputStreamWrapper::OOutputStreamWrapper(SvStream& _rStream):
191 rStream(_rStream)
194 OOutputStreamWrapper::~OOutputStreamWrapper() {}
196 void SAL_CALL OOutputStreamWrapper::writeBytes(const css::uno::Sequence< sal_Int8 >& aData) throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
198 sal_uInt32 nWritten = rStream.WriteBytes(aData.getConstArray(), aData.getLength());
199 ErrCode err = rStream.GetError();
200 if ( (ERRCODE_NONE != err)
201 || (nWritten != (sal_uInt32)aData.getLength())
204 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
208 void SAL_CALL OOutputStreamWrapper::flush() throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
210 rStream.Flush();
211 checkError();
214 void SAL_CALL OOutputStreamWrapper::closeOutput() throw( css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception )
218 void OOutputStreamWrapper::checkError() const
220 if (rStream.GetError() != ERRCODE_NONE)
221 // TODO: really evaluate the error
222 throw css::io::NotConnectedException(OUString(), const_cast<css::uno::XWeak*>(static_cast<const css::uno::XWeak*>(this)));
225 //= OSeekableOutputStreamWrapper
227 OSeekableOutputStreamWrapper::OSeekableOutputStreamWrapper(SvStream& _rStream)
228 :OOutputStreamWrapper(_rStream)
232 OSeekableOutputStreamWrapper::~OSeekableOutputStreamWrapper() {}
234 Any SAL_CALL OSeekableOutputStreamWrapper::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
236 Any aReturn = OOutputStreamWrapper::queryInterface(_rType);
237 if (!aReturn.hasValue())
238 aReturn = OSeekableOutputStreamWrapper_Base::queryInterface(_rType);
239 return aReturn;
242 void SAL_CALL OSeekableOutputStreamWrapper::acquire( ) throw ()
244 OOutputStreamWrapper::acquire();
247 void SAL_CALL OSeekableOutputStreamWrapper::release( ) throw ()
249 OOutputStreamWrapper::release();
252 void SAL_CALL OSeekableOutputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception)
254 rStream.Seek((sal_uInt32)_nLocation);
255 checkError();
258 sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getPosition( ) throw (IOException, RuntimeException, std::exception)
260 sal_uInt32 nPos = rStream.Tell();
261 checkError();
262 return (sal_Int64)nPos;
265 sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getLength( ) throw (IOException, RuntimeException, std::exception)
267 sal_uInt32 nCurrentPos = rStream.Tell();
268 checkError();
270 rStream.Seek(STREAM_SEEK_TO_END);
271 sal_uInt32 nEndPos = rStream.Tell();
272 rStream.Seek(nCurrentPos);
274 checkError();
276 return (sal_Int64)nEndPos;
279 OStreamWrapper::OStreamWrapper(SvStream& _rStream)
281 SetStream( &_rStream, false );
284 css::uno::Reference< css::io::XInputStream > SAL_CALL OStreamWrapper::getInputStream( ) throw (css::uno::RuntimeException, std::exception)
286 return this;
289 css::uno::Reference< css::io::XOutputStream > SAL_CALL OStreamWrapper::getOutputStream( ) throw (css::uno::RuntimeException, std::exception)
291 return this;
294 void SAL_CALL OStreamWrapper::writeBytes(const css::uno::Sequence< sal_Int8 >& aData) throw(css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception)
296 sal_uInt32 nWritten = m_pSvStream->WriteBytes(aData.getConstArray(), aData.getLength());
297 ErrCode err = m_pSvStream->GetError();
298 if ( (ERRCODE_NONE != err)
299 || (nWritten != (sal_uInt32)aData.getLength())
302 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
306 void SAL_CALL OStreamWrapper::flush() throw(css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception)
308 m_pSvStream->Flush();
309 if (m_pSvStream->GetError() != ERRCODE_NONE)
310 throw css::io::NotConnectedException(OUString(),static_cast<css::uno::XWeak*>(this));
313 void SAL_CALL OStreamWrapper::closeOutput() throw(css::io::NotConnectedException, css::io::BufferSizeExceededException, css::uno::RuntimeException, std::exception)
317 void SAL_CALL OStreamWrapper::truncate() throw(css::io::IOException, css::uno::RuntimeException, std::exception)
319 m_pSvStream->SetStreamSize(0);
322 } // namespace utl
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */