xref: /AOO41X/main/ucb/source/ucp/odma/odma_inputstream.cxx (revision 2f86921c33504fdff5a030df6c0b258927045abb)
1*2f86921cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2f86921cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2f86921cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2f86921cSAndrew Rist  * distributed with this work for additional information
6*2f86921cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2f86921cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2f86921cSAndrew Rist  * "License"); you may not use this file except in compliance
9*2f86921cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*2f86921cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*2f86921cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2f86921cSAndrew Rist  * software distributed under the License is distributed on an
15*2f86921cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2f86921cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2f86921cSAndrew Rist  * specific language governing permissions and limitations
18*2f86921cSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*2f86921cSAndrew Rist  *************************************************************/
21*2f86921cSAndrew Rist 
22*2f86921cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_ucb.hxx"
26cdf0e10cSrcweir #include "odma_inputstream.hxx"
27cdf0e10cSrcweir #include "com/sun/star/io/IOException.hpp"
28cdf0e10cSrcweir #include <com/sun/star/ucb/OpenCommandArgument2.hpp>
29cdf0e10cSrcweir #include <com/sun/star/ucb/OpenMode.hpp>
30cdf0e10cSrcweir #include <ucbhelper/content.hxx>
31cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataStreamer.hpp>
32cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
33cdf0e10cSrcweir #include "odma_contentprops.hxx"
34cdf0e10cSrcweir #include "odma_provider.hxx"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace odma;
37cdf0e10cSrcweir using namespace com::sun::star;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir class OActiveDataStreamer : public ::cppu::WeakImplHelper1< io::XActiveDataStreamer>
40cdf0e10cSrcweir {
41cdf0e10cSrcweir 	uno::Reference< io::XStream > m_xStream;
42cdf0e10cSrcweir public:
OActiveDataStreamer()43cdf0e10cSrcweir 	OActiveDataStreamer(){}
setStream(const uno::Reference<io::XStream> & _rStream)44cdf0e10cSrcweir 	virtual void SAL_CALL setStream( const uno::Reference< io::XStream >& _rStream ) throw (uno::RuntimeException)
45cdf0e10cSrcweir 	{
46cdf0e10cSrcweir 		m_xStream = _rStream;
47cdf0e10cSrcweir 	}
getStream()48cdf0e10cSrcweir     virtual uno::Reference< io::XStream > SAL_CALL getStream(  ) throw (uno::RuntimeException)
49cdf0e10cSrcweir 	{
50cdf0e10cSrcweir 		return m_xStream;
51cdf0e10cSrcweir 	}
52cdf0e10cSrcweir };
53cdf0e10cSrcweir // -----------------------------------------------------------------------------
OOdmaStream(::ucbhelper::Content * _pContent,ContentProvider * _pProvider,const::rtl::Reference<ContentProperties> & _rProp)54cdf0e10cSrcweir OOdmaStream::OOdmaStream(::ucbhelper::Content* _pContent,
55cdf0e10cSrcweir 						 ContentProvider* _pProvider,
56cdf0e10cSrcweir 						 const ::rtl::Reference<ContentProperties>& _rProp)
57cdf0e10cSrcweir  :m_pContent(_pContent)
58cdf0e10cSrcweir  ,m_bInputStreamCalled(sal_False)
59cdf0e10cSrcweir  ,m_bOutputStreamCalled(sal_False)
60cdf0e10cSrcweir  ,m_bModified(sal_False)
61cdf0e10cSrcweir  ,m_pProvider(_pProvider)
62cdf0e10cSrcweir  ,m_aProp(_rProp)
63cdf0e10cSrcweir {
64cdf0e10cSrcweir }
65cdf0e10cSrcweir // -----------------------------------------------------------------------------
~OOdmaStream()66cdf0e10cSrcweir OOdmaStream::~OOdmaStream()
67cdf0e10cSrcweir {
68cdf0e10cSrcweir     try
69cdf0e10cSrcweir     {
70cdf0e10cSrcweir         closeStream();
71cdf0e10cSrcweir 		delete m_pContent;
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir     catch (io::IOException const &)
74cdf0e10cSrcweir     {
75cdf0e10cSrcweir         OSL_ENSURE(false, "unexpected situation");
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir     catch (uno::RuntimeException const &)
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         OSL_ENSURE(false, "unexpected situation");
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir }
82cdf0e10cSrcweir // -----------------------------------------------------------------------------
getInputStream()83cdf0e10cSrcweir uno::Reference< io::XInputStream > SAL_CALL OOdmaStream::getInputStream(  ) throw( uno::RuntimeException)
84cdf0e10cSrcweir {
85cdf0e10cSrcweir 	{
86cdf0e10cSrcweir 		osl::MutexGuard aGuard( m_aMutex );
87cdf0e10cSrcweir 		m_bInputStreamCalled = sal_True;
88cdf0e10cSrcweir 	}
89cdf0e10cSrcweir 	return uno::Reference< io::XInputStream >( this );
90cdf0e10cSrcweir }
91cdf0e10cSrcweir // -----------------------------------------------------------------------------
getOutputStream()92cdf0e10cSrcweir uno::Reference< io::XOutputStream > SAL_CALL OOdmaStream::getOutputStream(  ) throw( uno::RuntimeException )
93cdf0e10cSrcweir {
94cdf0e10cSrcweir 	{
95cdf0e10cSrcweir 		osl::MutexGuard aGuard( m_aMutex );
96cdf0e10cSrcweir 		m_bOutputStreamCalled = sal_True;
97cdf0e10cSrcweir 	}
98cdf0e10cSrcweir 	return uno::Reference< io::XOutputStream >( this );
99cdf0e10cSrcweir }
100cdf0e10cSrcweir // -----------------------------------------------------------------------------
readBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)101cdf0e10cSrcweir sal_Int32 SAL_CALL OOdmaStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
102cdf0e10cSrcweir 	throw( io::NotConnectedException,
103cdf0e10cSrcweir 		   io::BufferSizeExceededException,
104cdf0e10cSrcweir 		   io::IOException,
105cdf0e10cSrcweir 		   uno::RuntimeException)
106cdf0e10cSrcweir {
107cdf0e10cSrcweir 	ensureInputStream();
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 	return m_xInput->readBytes(aData,nBytesToRead);
110cdf0e10cSrcweir }
111cdf0e10cSrcweir // -----------------------------------------------------------------------------
readSomeBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)112cdf0e10cSrcweir sal_Int32 SAL_CALL OOdmaStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData,sal_Int32 nMaxBytesToRead )
113cdf0e10cSrcweir 	throw( io::NotConnectedException,
114cdf0e10cSrcweir 		   io::BufferSizeExceededException,
115cdf0e10cSrcweir 		   io::IOException,
116cdf0e10cSrcweir 		   uno::RuntimeException)
117cdf0e10cSrcweir {
118cdf0e10cSrcweir 	return readBytes( aData,nMaxBytesToRead );
119cdf0e10cSrcweir }
120cdf0e10cSrcweir // -----------------------------------------------------------------------------
skipBytes(sal_Int32 nBytesToSkip)121cdf0e10cSrcweir void SAL_CALL OOdmaStream::skipBytes( sal_Int32 nBytesToSkip )
122cdf0e10cSrcweir 	throw( io::NotConnectedException,
123cdf0e10cSrcweir 		   io::BufferSizeExceededException,
124cdf0e10cSrcweir 		   io::IOException,
125cdf0e10cSrcweir 		   uno::RuntimeException )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir 	ensureInputStream();
128cdf0e10cSrcweir 	m_xInput->skipBytes(nBytesToSkip );
129cdf0e10cSrcweir }
130cdf0e10cSrcweir // -----------------------------------------------------------------------------
available()131cdf0e10cSrcweir sal_Int32 SAL_CALL OOdmaStream::available()
132cdf0e10cSrcweir 	throw( io::NotConnectedException,
133cdf0e10cSrcweir 		   io::IOException,
134cdf0e10cSrcweir 		   uno::RuntimeException)
135cdf0e10cSrcweir {
136cdf0e10cSrcweir 	ensureInputStream();
137cdf0e10cSrcweir 	return m_xInput->available();
138cdf0e10cSrcweir }
139cdf0e10cSrcweir // -----------------------------------------------------------------------------
writeBytes(const uno::Sequence<sal_Int8> & aData)140cdf0e10cSrcweir void SAL_CALL OOdmaStream::writeBytes( const uno::Sequence< sal_Int8 >& aData )
141cdf0e10cSrcweir 	throw( io::NotConnectedException,
142cdf0e10cSrcweir 		   io::BufferSizeExceededException,
143cdf0e10cSrcweir 		   io::IOException,
144cdf0e10cSrcweir 		   uno::RuntimeException)
145cdf0e10cSrcweir {
146cdf0e10cSrcweir 	ensureOutputStream();
147cdf0e10cSrcweir 	m_xOutput->writeBytes(aData);
148cdf0e10cSrcweir 	m_bModified = sal_True;
149cdf0e10cSrcweir }
150cdf0e10cSrcweir // -----------------------------------------------------------------------------
closeStream()151cdf0e10cSrcweir void SAL_CALL OOdmaStream::closeStream() throw( io::NotConnectedException,io::IOException,uno::RuntimeException )
152cdf0e10cSrcweir {
153cdf0e10cSrcweir 	if( m_xInput.is() )
154cdf0e10cSrcweir 	{
155cdf0e10cSrcweir 		m_xInput->closeInput();
156cdf0e10cSrcweir 		m_xInput		= NULL;
157cdf0e10cSrcweir 		m_xInputSeek	= NULL;
158cdf0e10cSrcweir 	}
159cdf0e10cSrcweir 	if(m_xOutput.is())
160cdf0e10cSrcweir 	{
161cdf0e10cSrcweir 		m_xOutput->closeOutput();
162cdf0e10cSrcweir 		m_xOutput		= NULL;
163cdf0e10cSrcweir 		m_xTruncate		= NULL;
164cdf0e10cSrcweir 		if(m_bModified)
165cdf0e10cSrcweir 			m_pProvider->saveDocument(m_aProp->m_sDocumentId);
166cdf0e10cSrcweir 	}
167cdf0e10cSrcweir }
168cdf0e10cSrcweir // -----------------------------------------------------------------------------
closeInput()169cdf0e10cSrcweir void SAL_CALL OOdmaStream::closeInput()
170cdf0e10cSrcweir 	throw( io::NotConnectedException,
171cdf0e10cSrcweir 		   io::IOException,
172cdf0e10cSrcweir 		   uno::RuntimeException )
173cdf0e10cSrcweir {
174cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
175cdf0e10cSrcweir 	m_bInputStreamCalled = sal_False;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir 	if( ! m_bOutputStreamCalled )
178cdf0e10cSrcweir 		closeStream();
179cdf0e10cSrcweir }
180cdf0e10cSrcweir // -----------------------------------------------------------------------------
closeOutput()181cdf0e10cSrcweir void SAL_CALL OOdmaStream::closeOutput()
182cdf0e10cSrcweir 	throw( io::NotConnectedException,
183cdf0e10cSrcweir 		   io::IOException,
184cdf0e10cSrcweir 		   uno::RuntimeException )
185cdf0e10cSrcweir {
186cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
187cdf0e10cSrcweir 	m_bOutputStreamCalled = sal_False;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 	if( ! m_bInputStreamCalled )
190cdf0e10cSrcweir 		closeStream();
191cdf0e10cSrcweir }
192cdf0e10cSrcweir // -----------------------------------------------------------------------------
flush()193cdf0e10cSrcweir void SAL_CALL OOdmaStream::flush()
194cdf0e10cSrcweir 	throw( io::NotConnectedException,
195cdf0e10cSrcweir 		   io::BufferSizeExceededException,
196cdf0e10cSrcweir 		   io::IOException,
197cdf0e10cSrcweir 		   uno::RuntimeException )
198cdf0e10cSrcweir {
199cdf0e10cSrcweir 	ensureOutputStream();
200cdf0e10cSrcweir 	m_xOutput->flush();
201cdf0e10cSrcweir }
202cdf0e10cSrcweir // -----------------------------------------------------------------------------
ensureInputStream()203cdf0e10cSrcweir void OOdmaStream::ensureInputStream() throw( io::IOException )
204cdf0e10cSrcweir {
205cdf0e10cSrcweir 	try
206cdf0e10cSrcweir 	{
207cdf0e10cSrcweir 		if(!m_xInput.is())
208cdf0e10cSrcweir 		{
209cdf0e10cSrcweir 			m_xInput = m_pContent->openStream();
210cdf0e10cSrcweir 			m_xInputSeek = uno::Reference< io::XSeekable>(m_xInput,uno::UNO_QUERY);
211cdf0e10cSrcweir 		}
212cdf0e10cSrcweir 	}
213cdf0e10cSrcweir 	catch(const uno::Exception&)
214cdf0e10cSrcweir 	{
215cdf0e10cSrcweir 	}
216cdf0e10cSrcweir 	if(!m_xInput.is())
217cdf0e10cSrcweir 		throw io::IOException();
218cdf0e10cSrcweir }
219cdf0e10cSrcweir // -----------------------------------------------------------------------------
ensureOutputStream()220cdf0e10cSrcweir void OOdmaStream::ensureOutputStream() throw( io::IOException )
221cdf0e10cSrcweir {
222cdf0e10cSrcweir 	try
223cdf0e10cSrcweir 	{
224cdf0e10cSrcweir 		if(!m_xOutput.is())
225cdf0e10cSrcweir 		{
226cdf0e10cSrcweir 			ucb::OpenCommandArgument2 aCommand;
227cdf0e10cSrcweir 			aCommand.Mode = ucb::OpenMode::DOCUMENT;
228cdf0e10cSrcweir 			uno::Reference< io::XActiveDataStreamer > xActiveStreamer = new OActiveDataStreamer();
229cdf0e10cSrcweir 			aCommand.Sink = xActiveStreamer;
230cdf0e10cSrcweir 			m_pContent->executeCommand(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("open")),uno::makeAny(aCommand));
231cdf0e10cSrcweir 			if(xActiveStreamer.is())
232cdf0e10cSrcweir 			{
233cdf0e10cSrcweir 				uno::Reference< io::XStream> xStream = xActiveStreamer->getStream();
234cdf0e10cSrcweir 				if(xStream.is())
235cdf0e10cSrcweir 					m_xOutput = xStream->getOutputStream();
236cdf0e10cSrcweir 			}
237cdf0e10cSrcweir 		}
238cdf0e10cSrcweir 	}
239cdf0e10cSrcweir 	catch(const uno::Exception&)
240cdf0e10cSrcweir 	{
241cdf0e10cSrcweir 	}
242cdf0e10cSrcweir 	if(!m_xOutput.is())
243cdf0e10cSrcweir 		throw io::IOException();
244cdf0e10cSrcweir 	m_xTruncate = uno::Reference< io::XTruncate>(m_xOutput,uno::UNO_QUERY);
245cdf0e10cSrcweir }
246cdf0e10cSrcweir // -----------------------------------------------------------------------------
247cdf0e10cSrcweir // XTruncate
truncate(void)248cdf0e10cSrcweir void SAL_CALL OOdmaStream::truncate( void )
249cdf0e10cSrcweir 	throw( io::IOException,
250cdf0e10cSrcweir 		   uno::RuntimeException )
251cdf0e10cSrcweir {
252cdf0e10cSrcweir 	if(m_xTruncate.is())
253cdf0e10cSrcweir 		m_xTruncate->truncate();
254cdf0e10cSrcweir }
255cdf0e10cSrcweir // -----------------------------------------------------------------------------
256cdf0e10cSrcweir // XSeekable
seek(sal_Int64 location)257cdf0e10cSrcweir void SAL_CALL OOdmaStream::seek(sal_Int64 location )
258cdf0e10cSrcweir 	throw( lang::IllegalArgumentException,
259cdf0e10cSrcweir 		   io::IOException,
260cdf0e10cSrcweir 		   uno::RuntimeException )
261cdf0e10cSrcweir {
262cdf0e10cSrcweir 	ensureInputStream();
263cdf0e10cSrcweir 	if(m_xInputSeek.is())
264cdf0e10cSrcweir 		m_xInputSeek->seek(location);
265cdf0e10cSrcweir }
266cdf0e10cSrcweir // -----------------------------------------------------------------------------
getPosition()267cdf0e10cSrcweir sal_Int64 SAL_CALL OOdmaStream::getPosition()
268cdf0e10cSrcweir 	throw( io::IOException,
269cdf0e10cSrcweir 		   uno::RuntimeException )
270cdf0e10cSrcweir {
271cdf0e10cSrcweir 	ensureInputStream();
272cdf0e10cSrcweir 	return m_xInputSeek.is() ? m_xInputSeek->getPosition() : sal_Int64(0);
273cdf0e10cSrcweir }
274cdf0e10cSrcweir // -----------------------------------------------------------------------------
getLength()275cdf0e10cSrcweir sal_Int64 SAL_CALL OOdmaStream::getLength()
276cdf0e10cSrcweir 	throw( io::IOException,
277cdf0e10cSrcweir 		   uno::RuntimeException )
278cdf0e10cSrcweir {
279cdf0e10cSrcweir 	ensureInputStream();
280cdf0e10cSrcweir 	return m_xInputSeek.is() ? m_xInputSeek->getLength() : sal_Int64(0);
281cdf0e10cSrcweir }
282cdf0e10cSrcweir // -----------------------------------------------------------------------------
283