xref: /AOO41X/main/ucb/source/ucp/odma/odma_inputstream.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_ucb.hxx"
30*cdf0e10cSrcweir #include "odma_inputstream.hxx"
31*cdf0e10cSrcweir #include "com/sun/star/io/IOException.hpp"
32*cdf0e10cSrcweir #include <com/sun/star/ucb/OpenCommandArgument2.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/ucb/OpenMode.hpp>
34*cdf0e10cSrcweir #include <ucbhelper/content.hxx>
35*cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataStreamer.hpp>
36*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
37*cdf0e10cSrcweir #include "odma_contentprops.hxx"
38*cdf0e10cSrcweir #include "odma_provider.hxx"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using namespace odma;
41*cdf0e10cSrcweir using namespace com::sun::star;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir class OActiveDataStreamer : public ::cppu::WeakImplHelper1< io::XActiveDataStreamer>
44*cdf0e10cSrcweir {
45*cdf0e10cSrcweir 	uno::Reference< io::XStream > m_xStream;
46*cdf0e10cSrcweir public:
47*cdf0e10cSrcweir 	OActiveDataStreamer(){}
48*cdf0e10cSrcweir 	virtual void SAL_CALL setStream( const uno::Reference< io::XStream >& _rStream ) throw (uno::RuntimeException)
49*cdf0e10cSrcweir 	{
50*cdf0e10cSrcweir 		m_xStream = _rStream;
51*cdf0e10cSrcweir 	}
52*cdf0e10cSrcweir     virtual uno::Reference< io::XStream > SAL_CALL getStream(  ) throw (uno::RuntimeException)
53*cdf0e10cSrcweir 	{
54*cdf0e10cSrcweir 		return m_xStream;
55*cdf0e10cSrcweir 	}
56*cdf0e10cSrcweir };
57*cdf0e10cSrcweir // -----------------------------------------------------------------------------
58*cdf0e10cSrcweir OOdmaStream::OOdmaStream(::ucbhelper::Content* _pContent,
59*cdf0e10cSrcweir 						 ContentProvider* _pProvider,
60*cdf0e10cSrcweir 						 const ::rtl::Reference<ContentProperties>& _rProp)
61*cdf0e10cSrcweir  :m_pContent(_pContent)
62*cdf0e10cSrcweir  ,m_bInputStreamCalled(sal_False)
63*cdf0e10cSrcweir  ,m_bOutputStreamCalled(sal_False)
64*cdf0e10cSrcweir  ,m_bModified(sal_False)
65*cdf0e10cSrcweir  ,m_pProvider(_pProvider)
66*cdf0e10cSrcweir  ,m_aProp(_rProp)
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir }
69*cdf0e10cSrcweir // -----------------------------------------------------------------------------
70*cdf0e10cSrcweir OOdmaStream::~OOdmaStream()
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir     try
73*cdf0e10cSrcweir     {
74*cdf0e10cSrcweir         closeStream();
75*cdf0e10cSrcweir 		delete m_pContent;
76*cdf0e10cSrcweir     }
77*cdf0e10cSrcweir     catch (io::IOException const &)
78*cdf0e10cSrcweir     {
79*cdf0e10cSrcweir         OSL_ENSURE(false, "unexpected situation");
80*cdf0e10cSrcweir     }
81*cdf0e10cSrcweir     catch (uno::RuntimeException const &)
82*cdf0e10cSrcweir     {
83*cdf0e10cSrcweir         OSL_ENSURE(false, "unexpected situation");
84*cdf0e10cSrcweir     }
85*cdf0e10cSrcweir }
86*cdf0e10cSrcweir // -----------------------------------------------------------------------------
87*cdf0e10cSrcweir uno::Reference< io::XInputStream > SAL_CALL OOdmaStream::getInputStream(  ) throw( uno::RuntimeException)
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir 	{
90*cdf0e10cSrcweir 		osl::MutexGuard aGuard( m_aMutex );
91*cdf0e10cSrcweir 		m_bInputStreamCalled = sal_True;
92*cdf0e10cSrcweir 	}
93*cdf0e10cSrcweir 	return uno::Reference< io::XInputStream >( this );
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir // -----------------------------------------------------------------------------
96*cdf0e10cSrcweir uno::Reference< io::XOutputStream > SAL_CALL OOdmaStream::getOutputStream(  ) throw( uno::RuntimeException )
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir 	{
99*cdf0e10cSrcweir 		osl::MutexGuard aGuard( m_aMutex );
100*cdf0e10cSrcweir 		m_bOutputStreamCalled = sal_True;
101*cdf0e10cSrcweir 	}
102*cdf0e10cSrcweir 	return uno::Reference< io::XOutputStream >( this );
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir // -----------------------------------------------------------------------------
105*cdf0e10cSrcweir sal_Int32 SAL_CALL OOdmaStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
106*cdf0e10cSrcweir 	throw( io::NotConnectedException,
107*cdf0e10cSrcweir 		   io::BufferSizeExceededException,
108*cdf0e10cSrcweir 		   io::IOException,
109*cdf0e10cSrcweir 		   uno::RuntimeException)
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir 	ensureInputStream();
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 	return m_xInput->readBytes(aData,nBytesToRead);
114*cdf0e10cSrcweir }
115*cdf0e10cSrcweir // -----------------------------------------------------------------------------
116*cdf0e10cSrcweir sal_Int32 SAL_CALL OOdmaStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData,sal_Int32 nMaxBytesToRead )
117*cdf0e10cSrcweir 	throw( io::NotConnectedException,
118*cdf0e10cSrcweir 		   io::BufferSizeExceededException,
119*cdf0e10cSrcweir 		   io::IOException,
120*cdf0e10cSrcweir 		   uno::RuntimeException)
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir 	return readBytes( aData,nMaxBytesToRead );
123*cdf0e10cSrcweir }
124*cdf0e10cSrcweir // -----------------------------------------------------------------------------
125*cdf0e10cSrcweir void SAL_CALL OOdmaStream::skipBytes( sal_Int32 nBytesToSkip )
126*cdf0e10cSrcweir 	throw( io::NotConnectedException,
127*cdf0e10cSrcweir 		   io::BufferSizeExceededException,
128*cdf0e10cSrcweir 		   io::IOException,
129*cdf0e10cSrcweir 		   uno::RuntimeException )
130*cdf0e10cSrcweir {
131*cdf0e10cSrcweir 	ensureInputStream();
132*cdf0e10cSrcweir 	m_xInput->skipBytes(nBytesToSkip );
133*cdf0e10cSrcweir }
134*cdf0e10cSrcweir // -----------------------------------------------------------------------------
135*cdf0e10cSrcweir sal_Int32 SAL_CALL OOdmaStream::available()
136*cdf0e10cSrcweir 	throw( io::NotConnectedException,
137*cdf0e10cSrcweir 		   io::IOException,
138*cdf0e10cSrcweir 		   uno::RuntimeException)
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir 	ensureInputStream();
141*cdf0e10cSrcweir 	return m_xInput->available();
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir // -----------------------------------------------------------------------------
144*cdf0e10cSrcweir void SAL_CALL OOdmaStream::writeBytes( const uno::Sequence< sal_Int8 >& aData )
145*cdf0e10cSrcweir 	throw( io::NotConnectedException,
146*cdf0e10cSrcweir 		   io::BufferSizeExceededException,
147*cdf0e10cSrcweir 		   io::IOException,
148*cdf0e10cSrcweir 		   uno::RuntimeException)
149*cdf0e10cSrcweir {
150*cdf0e10cSrcweir 	ensureOutputStream();
151*cdf0e10cSrcweir 	m_xOutput->writeBytes(aData);
152*cdf0e10cSrcweir 	m_bModified = sal_True;
153*cdf0e10cSrcweir }
154*cdf0e10cSrcweir // -----------------------------------------------------------------------------
155*cdf0e10cSrcweir void SAL_CALL OOdmaStream::closeStream() throw( io::NotConnectedException,io::IOException,uno::RuntimeException )
156*cdf0e10cSrcweir {
157*cdf0e10cSrcweir 	if( m_xInput.is() )
158*cdf0e10cSrcweir 	{
159*cdf0e10cSrcweir 		m_xInput->closeInput();
160*cdf0e10cSrcweir 		m_xInput		= NULL;
161*cdf0e10cSrcweir 		m_xInputSeek	= NULL;
162*cdf0e10cSrcweir 	}
163*cdf0e10cSrcweir 	if(m_xOutput.is())
164*cdf0e10cSrcweir 	{
165*cdf0e10cSrcweir 		m_xOutput->closeOutput();
166*cdf0e10cSrcweir 		m_xOutput		= NULL;
167*cdf0e10cSrcweir 		m_xTruncate		= NULL;
168*cdf0e10cSrcweir 		if(m_bModified)
169*cdf0e10cSrcweir 			m_pProvider->saveDocument(m_aProp->m_sDocumentId);
170*cdf0e10cSrcweir 	}
171*cdf0e10cSrcweir }
172*cdf0e10cSrcweir // -----------------------------------------------------------------------------
173*cdf0e10cSrcweir void SAL_CALL OOdmaStream::closeInput()
174*cdf0e10cSrcweir 	throw( io::NotConnectedException,
175*cdf0e10cSrcweir 		   io::IOException,
176*cdf0e10cSrcweir 		   uno::RuntimeException )
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
179*cdf0e10cSrcweir 	m_bInputStreamCalled = sal_False;
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 	if( ! m_bOutputStreamCalled )
182*cdf0e10cSrcweir 		closeStream();
183*cdf0e10cSrcweir }
184*cdf0e10cSrcweir // -----------------------------------------------------------------------------
185*cdf0e10cSrcweir void SAL_CALL OOdmaStream::closeOutput()
186*cdf0e10cSrcweir 	throw( io::NotConnectedException,
187*cdf0e10cSrcweir 		   io::IOException,
188*cdf0e10cSrcweir 		   uno::RuntimeException )
189*cdf0e10cSrcweir {
190*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
191*cdf0e10cSrcweir 	m_bOutputStreamCalled = sal_False;
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 	if( ! m_bInputStreamCalled )
194*cdf0e10cSrcweir 		closeStream();
195*cdf0e10cSrcweir }
196*cdf0e10cSrcweir // -----------------------------------------------------------------------------
197*cdf0e10cSrcweir void SAL_CALL OOdmaStream::flush()
198*cdf0e10cSrcweir 	throw( io::NotConnectedException,
199*cdf0e10cSrcweir 		   io::BufferSizeExceededException,
200*cdf0e10cSrcweir 		   io::IOException,
201*cdf0e10cSrcweir 		   uno::RuntimeException )
202*cdf0e10cSrcweir {
203*cdf0e10cSrcweir 	ensureOutputStream();
204*cdf0e10cSrcweir 	m_xOutput->flush();
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir // -----------------------------------------------------------------------------
207*cdf0e10cSrcweir void OOdmaStream::ensureInputStream() throw( io::IOException )
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir 	try
210*cdf0e10cSrcweir 	{
211*cdf0e10cSrcweir 		if(!m_xInput.is())
212*cdf0e10cSrcweir 		{
213*cdf0e10cSrcweir 			m_xInput = m_pContent->openStream();
214*cdf0e10cSrcweir 			m_xInputSeek = uno::Reference< io::XSeekable>(m_xInput,uno::UNO_QUERY);
215*cdf0e10cSrcweir 		}
216*cdf0e10cSrcweir 	}
217*cdf0e10cSrcweir 	catch(const uno::Exception&)
218*cdf0e10cSrcweir 	{
219*cdf0e10cSrcweir 	}
220*cdf0e10cSrcweir 	if(!m_xInput.is())
221*cdf0e10cSrcweir 		throw io::IOException();
222*cdf0e10cSrcweir }
223*cdf0e10cSrcweir // -----------------------------------------------------------------------------
224*cdf0e10cSrcweir void OOdmaStream::ensureOutputStream() throw( io::IOException )
225*cdf0e10cSrcweir {
226*cdf0e10cSrcweir 	try
227*cdf0e10cSrcweir 	{
228*cdf0e10cSrcweir 		if(!m_xOutput.is())
229*cdf0e10cSrcweir 		{
230*cdf0e10cSrcweir 			ucb::OpenCommandArgument2 aCommand;
231*cdf0e10cSrcweir 			aCommand.Mode = ucb::OpenMode::DOCUMENT;
232*cdf0e10cSrcweir 			uno::Reference< io::XActiveDataStreamer > xActiveStreamer = new OActiveDataStreamer();
233*cdf0e10cSrcweir 			aCommand.Sink = xActiveStreamer;
234*cdf0e10cSrcweir 			m_pContent->executeCommand(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("open")),uno::makeAny(aCommand));
235*cdf0e10cSrcweir 			if(xActiveStreamer.is())
236*cdf0e10cSrcweir 			{
237*cdf0e10cSrcweir 				uno::Reference< io::XStream> xStream = xActiveStreamer->getStream();
238*cdf0e10cSrcweir 				if(xStream.is())
239*cdf0e10cSrcweir 					m_xOutput = xStream->getOutputStream();
240*cdf0e10cSrcweir 			}
241*cdf0e10cSrcweir 		}
242*cdf0e10cSrcweir 	}
243*cdf0e10cSrcweir 	catch(const uno::Exception&)
244*cdf0e10cSrcweir 	{
245*cdf0e10cSrcweir 	}
246*cdf0e10cSrcweir 	if(!m_xOutput.is())
247*cdf0e10cSrcweir 		throw io::IOException();
248*cdf0e10cSrcweir 	m_xTruncate = uno::Reference< io::XTruncate>(m_xOutput,uno::UNO_QUERY);
249*cdf0e10cSrcweir }
250*cdf0e10cSrcweir // -----------------------------------------------------------------------------
251*cdf0e10cSrcweir // XTruncate
252*cdf0e10cSrcweir void SAL_CALL OOdmaStream::truncate( void )
253*cdf0e10cSrcweir 	throw( io::IOException,
254*cdf0e10cSrcweir 		   uno::RuntimeException )
255*cdf0e10cSrcweir {
256*cdf0e10cSrcweir 	if(m_xTruncate.is())
257*cdf0e10cSrcweir 		m_xTruncate->truncate();
258*cdf0e10cSrcweir }
259*cdf0e10cSrcweir // -----------------------------------------------------------------------------
260*cdf0e10cSrcweir // XSeekable
261*cdf0e10cSrcweir void SAL_CALL OOdmaStream::seek(sal_Int64 location )
262*cdf0e10cSrcweir 	throw( lang::IllegalArgumentException,
263*cdf0e10cSrcweir 		   io::IOException,
264*cdf0e10cSrcweir 		   uno::RuntimeException )
265*cdf0e10cSrcweir {
266*cdf0e10cSrcweir 	ensureInputStream();
267*cdf0e10cSrcweir 	if(m_xInputSeek.is())
268*cdf0e10cSrcweir 		m_xInputSeek->seek(location);
269*cdf0e10cSrcweir }
270*cdf0e10cSrcweir // -----------------------------------------------------------------------------
271*cdf0e10cSrcweir sal_Int64 SAL_CALL OOdmaStream::getPosition()
272*cdf0e10cSrcweir 	throw( io::IOException,
273*cdf0e10cSrcweir 		   uno::RuntimeException )
274*cdf0e10cSrcweir {
275*cdf0e10cSrcweir 	ensureInputStream();
276*cdf0e10cSrcweir 	return m_xInputSeek.is() ? m_xInputSeek->getPosition() : sal_Int64(0);
277*cdf0e10cSrcweir }
278*cdf0e10cSrcweir // -----------------------------------------------------------------------------
279*cdf0e10cSrcweir sal_Int64 SAL_CALL OOdmaStream::getLength()
280*cdf0e10cSrcweir 	throw( io::IOException,
281*cdf0e10cSrcweir 		   uno::RuntimeException )
282*cdf0e10cSrcweir {
283*cdf0e10cSrcweir 	ensureInputStream();
284*cdf0e10cSrcweir 	return m_xInputSeek.is() ? m_xInputSeek->getLength() : sal_Int64(0);
285*cdf0e10cSrcweir }
286*cdf0e10cSrcweir // -----------------------------------------------------------------------------
287