1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: odma_inputstream.cxx,v $
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"
44 using namespace com::sun::star
;
46 class OActiveDataStreamer
: public ::cppu::WeakImplHelper1
< io::XActiveDataStreamer
>
48 uno::Reference
< io::XStream
> m_xStream
;
50 OActiveDataStreamer(){}
51 virtual void SAL_CALL
setStream( const uno::Reference
< io::XStream
>& _rStream
) throw (uno::RuntimeException
)
55 virtual uno::Reference
< io::XStream
> SAL_CALL
getStream( ) throw (uno::RuntimeException
)
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
)
72 // -----------------------------------------------------------------------------
73 OOdmaStream::~OOdmaStream()
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
,
112 uno::RuntimeException
)
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
,
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
,
132 uno::RuntimeException
)
135 m_xInput
->skipBytes(nBytesToSkip
);
137 // -----------------------------------------------------------------------------
138 sal_Int32 SAL_CALL
OOdmaStream::available()
139 throw( io::NotConnectedException
,
141 uno::RuntimeException
)
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
,
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
)
162 m_xInput
->closeInput();
168 m_xOutput
->closeOutput();
172 m_pProvider
->saveDocument(m_aProp
->m_sDocumentId
);
175 // -----------------------------------------------------------------------------
176 void SAL_CALL
OOdmaStream::closeInput()
177 throw( io::NotConnectedException
,
179 uno::RuntimeException
)
181 osl::MutexGuard
aGuard( m_aMutex
);
182 m_bInputStreamCalled
= sal_False
;
184 if( ! m_bOutputStreamCalled
)
187 // -----------------------------------------------------------------------------
188 void SAL_CALL
OOdmaStream::closeOutput()
189 throw( io::NotConnectedException
,
191 uno::RuntimeException
)
193 osl::MutexGuard
aGuard( m_aMutex
);
194 m_bOutputStreamCalled
= sal_False
;
196 if( ! m_bInputStreamCalled
)
199 // -----------------------------------------------------------------------------
200 void SAL_CALL
OOdmaStream::flush()
201 throw( io::NotConnectedException
,
202 io::BufferSizeExceededException
,
204 uno::RuntimeException
)
206 ensureOutputStream();
209 // -----------------------------------------------------------------------------
210 void OOdmaStream::ensureInputStream() throw( io::IOException
)
216 m_xInput
= m_pContent
->openStream();
217 m_xInputSeek
= uno::Reference
< io::XSeekable
>(m_xInput
,uno::UNO_QUERY
);
220 catch(const uno::Exception
&)
224 throw io::IOException();
226 // -----------------------------------------------------------------------------
227 void OOdmaStream::ensureOutputStream() throw( io::IOException
)
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();
242 m_xOutput
= xStream
->getOutputStream();
246 catch(const uno::Exception
&)
250 throw io::IOException();
251 m_xTruncate
= uno::Reference
< io::XTruncate
>(m_xOutput
,uno::UNO_QUERY
);
253 // -----------------------------------------------------------------------------
255 void SAL_CALL
OOdmaStream::truncate( void )
256 throw( io::IOException
,
257 uno::RuntimeException
)
260 m_xTruncate
->truncate();
262 // -----------------------------------------------------------------------------
264 void SAL_CALL
OOdmaStream::seek(sal_Int64 location
)
265 throw( lang::IllegalArgumentException
,
267 uno::RuntimeException
)
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
)
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
)
287 return m_xInputSeek
.is() ? m_xInputSeek
->getLength() : sal_Int64(0);
289 // -----------------------------------------------------------------------------