Update ooo320-m1
[ooovba.git] / ucb / source / ucp / odma / odma_inputstream.cxx
blob6de50a90c2fb14487538673160aa92ef91b8e82e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: odma_inputstream.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_ucb.hxx"
33 #include "odma_inputstream.hxx"
34 #include "com/sun/star/io/IOException.hpp"
35 #include <com/sun/star/ucb/OpenCommandArgument2.hpp>
36 #include <com/sun/star/ucb/OpenMode.hpp>
37 #include <ucbhelper/content.hxx>
38 #include <com/sun/star/io/XActiveDataStreamer.hpp>
39 #include <cppuhelper/implbase1.hxx>
40 #include "odma_contentprops.hxx"
41 #include "odma_provider.hxx"
43 using namespace odma;
44 using namespace com::sun::star;
46 class OActiveDataStreamer : public ::cppu::WeakImplHelper1< io::XActiveDataStreamer>
48 uno::Reference< io::XStream > m_xStream;
49 public:
50 OActiveDataStreamer(){}
51 virtual void SAL_CALL setStream( const uno::Reference< io::XStream >& _rStream ) throw (uno::RuntimeException)
53 m_xStream = _rStream;
55 virtual uno::Reference< io::XStream > SAL_CALL getStream( ) throw (uno::RuntimeException)
57 return m_xStream;
60 // -----------------------------------------------------------------------------
61 OOdmaStream::OOdmaStream(::ucbhelper::Content* _pContent,
62 ContentProvider* _pProvider,
63 const ::rtl::Reference<ContentProperties>& _rProp)
64 :m_pContent(_pContent)
65 ,m_bInputStreamCalled(sal_False)
66 ,m_bOutputStreamCalled(sal_False)
67 ,m_bModified(sal_False)
68 ,m_pProvider(_pProvider)
69 ,m_aProp(_rProp)
72 // -----------------------------------------------------------------------------
73 OOdmaStream::~OOdmaStream()
75 try
77 closeStream();
78 delete m_pContent;
80 catch (io::IOException const &)
82 OSL_ENSURE(false, "unexpected situation");
84 catch (uno::RuntimeException const &)
86 OSL_ENSURE(false, "unexpected situation");
89 // -----------------------------------------------------------------------------
90 uno::Reference< io::XInputStream > SAL_CALL OOdmaStream::getInputStream( ) throw( uno::RuntimeException)
93 osl::MutexGuard aGuard( m_aMutex );
94 m_bInputStreamCalled = sal_True;
96 return uno::Reference< io::XInputStream >( this );
98 // -----------------------------------------------------------------------------
99 uno::Reference< io::XOutputStream > SAL_CALL OOdmaStream::getOutputStream( ) throw( uno::RuntimeException )
102 osl::MutexGuard aGuard( m_aMutex );
103 m_bOutputStreamCalled = sal_True;
105 return uno::Reference< io::XOutputStream >( this );
107 // -----------------------------------------------------------------------------
108 sal_Int32 SAL_CALL OOdmaStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
109 throw( io::NotConnectedException,
110 io::BufferSizeExceededException,
111 io::IOException,
112 uno::RuntimeException)
114 ensureInputStream();
116 return m_xInput->readBytes(aData,nBytesToRead);
118 // -----------------------------------------------------------------------------
119 sal_Int32 SAL_CALL OOdmaStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData,sal_Int32 nMaxBytesToRead )
120 throw( io::NotConnectedException,
121 io::BufferSizeExceededException,
122 io::IOException,
123 uno::RuntimeException)
125 return readBytes( aData,nMaxBytesToRead );
127 // -----------------------------------------------------------------------------
128 void SAL_CALL OOdmaStream::skipBytes( sal_Int32 nBytesToSkip )
129 throw( io::NotConnectedException,
130 io::BufferSizeExceededException,
131 io::IOException,
132 uno::RuntimeException )
134 ensureInputStream();
135 m_xInput->skipBytes(nBytesToSkip );
137 // -----------------------------------------------------------------------------
138 sal_Int32 SAL_CALL OOdmaStream::available()
139 throw( io::NotConnectedException,
140 io::IOException,
141 uno::RuntimeException)
143 ensureInputStream();
144 return m_xInput->available();
146 // -----------------------------------------------------------------------------
147 void SAL_CALL OOdmaStream::writeBytes( const uno::Sequence< sal_Int8 >& aData )
148 throw( io::NotConnectedException,
149 io::BufferSizeExceededException,
150 io::IOException,
151 uno::RuntimeException)
153 ensureOutputStream();
154 m_xOutput->writeBytes(aData);
155 m_bModified = sal_True;
157 // -----------------------------------------------------------------------------
158 void SAL_CALL OOdmaStream::closeStream() throw( io::NotConnectedException,io::IOException,uno::RuntimeException )
160 if( m_xInput.is() )
162 m_xInput->closeInput();
163 m_xInput = NULL;
164 m_xInputSeek = NULL;
166 if(m_xOutput.is())
168 m_xOutput->closeOutput();
169 m_xOutput = NULL;
170 m_xTruncate = NULL;
171 if(m_bModified)
172 m_pProvider->saveDocument(m_aProp->m_sDocumentId);
175 // -----------------------------------------------------------------------------
176 void SAL_CALL OOdmaStream::closeInput()
177 throw( io::NotConnectedException,
178 io::IOException,
179 uno::RuntimeException )
181 osl::MutexGuard aGuard( m_aMutex );
182 m_bInputStreamCalled = sal_False;
184 if( ! m_bOutputStreamCalled )
185 closeStream();
187 // -----------------------------------------------------------------------------
188 void SAL_CALL OOdmaStream::closeOutput()
189 throw( io::NotConnectedException,
190 io::IOException,
191 uno::RuntimeException )
193 osl::MutexGuard aGuard( m_aMutex );
194 m_bOutputStreamCalled = sal_False;
196 if( ! m_bInputStreamCalled )
197 closeStream();
199 // -----------------------------------------------------------------------------
200 void SAL_CALL OOdmaStream::flush()
201 throw( io::NotConnectedException,
202 io::BufferSizeExceededException,
203 io::IOException,
204 uno::RuntimeException )
206 ensureOutputStream();
207 m_xOutput->flush();
209 // -----------------------------------------------------------------------------
210 void OOdmaStream::ensureInputStream() throw( io::IOException )
214 if(!m_xInput.is())
216 m_xInput = m_pContent->openStream();
217 m_xInputSeek = uno::Reference< io::XSeekable>(m_xInput,uno::UNO_QUERY);
220 catch(const uno::Exception&)
223 if(!m_xInput.is())
224 throw io::IOException();
226 // -----------------------------------------------------------------------------
227 void OOdmaStream::ensureOutputStream() throw( io::IOException )
231 if(!m_xOutput.is())
233 ucb::OpenCommandArgument2 aCommand;
234 aCommand.Mode = ucb::OpenMode::DOCUMENT;
235 uno::Reference< io::XActiveDataStreamer > xActiveStreamer = new OActiveDataStreamer();
236 aCommand.Sink = xActiveStreamer;
237 m_pContent->executeCommand(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("open")),uno::makeAny(aCommand));
238 if(xActiveStreamer.is())
240 uno::Reference< io::XStream> xStream = xActiveStreamer->getStream();
241 if(xStream.is())
242 m_xOutput = xStream->getOutputStream();
246 catch(const uno::Exception&)
249 if(!m_xOutput.is())
250 throw io::IOException();
251 m_xTruncate = uno::Reference< io::XTruncate>(m_xOutput,uno::UNO_QUERY);
253 // -----------------------------------------------------------------------------
254 // XTruncate
255 void SAL_CALL OOdmaStream::truncate( void )
256 throw( io::IOException,
257 uno::RuntimeException )
259 if(m_xTruncate.is())
260 m_xTruncate->truncate();
262 // -----------------------------------------------------------------------------
263 // XSeekable
264 void SAL_CALL OOdmaStream::seek(sal_Int64 location )
265 throw( lang::IllegalArgumentException,
266 io::IOException,
267 uno::RuntimeException )
269 ensureInputStream();
270 if(m_xInputSeek.is())
271 m_xInputSeek->seek(location);
273 // -----------------------------------------------------------------------------
274 sal_Int64 SAL_CALL OOdmaStream::getPosition()
275 throw( io::IOException,
276 uno::RuntimeException )
278 ensureInputStream();
279 return m_xInputSeek.is() ? m_xInputSeek->getPosition() : sal_Int64(0);
281 // -----------------------------------------------------------------------------
282 sal_Int64 SAL_CALL OOdmaStream::getLength()
283 throw( io::IOException,
284 uno::RuntimeException )
286 ensureInputStream();
287 return m_xInputSeek.is() ? m_xInputSeek->getLength() : sal_Int64(0);
289 // -----------------------------------------------------------------------------