xref: /AOO41X/main/package/source/xstor/switchpersistencestream.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_package.hxx"
30*cdf0e10cSrcweir #include <osl/diagnose.h>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
33*cdf0e10cSrcweir #include <switchpersistencestream.hxx>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir using namespace ::com::sun::star;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir // ========================================================================
38*cdf0e10cSrcweir struct SPStreamData_Impl
39*cdf0e10cSrcweir {
40*cdf0e10cSrcweir 	uno::Reference< lang::XMultiServiceFactory > m_xFactory;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 	sal_Bool m_bInStreamBased;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 	// the streams below are not visible from outside so there is no need to remember position
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir 	// original stream related members
47*cdf0e10cSrcweir 	uno::Reference< io::XStream > m_xOrigStream;
48*cdf0e10cSrcweir 	uno::Reference< io::XTruncate > m_xOrigTruncate;
49*cdf0e10cSrcweir 	uno::Reference< io::XSeekable > m_xOrigSeekable;
50*cdf0e10cSrcweir 	uno::Reference< io::XInputStream > m_xOrigInStream;
51*cdf0e10cSrcweir 	uno::Reference< io::XOutputStream > m_xOrigOutStream;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 	sal_Bool m_bInOpen;
54*cdf0e10cSrcweir 	sal_Bool m_bOutOpen;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir 	SPStreamData_Impl(
58*cdf0e10cSrcweir 			const uno::Reference< lang::XMultiServiceFactory >& xFactory,
59*cdf0e10cSrcweir 			sal_Bool bInStreamBased,
60*cdf0e10cSrcweir 			const uno::Reference< io::XStream >& xOrigStream,
61*cdf0e10cSrcweir 			const uno::Reference< io::XTruncate >& xOrigTruncate,
62*cdf0e10cSrcweir 			const uno::Reference< io::XSeekable >& xOrigSeekable,
63*cdf0e10cSrcweir 			const uno::Reference< io::XInputStream >& xOrigInStream,
64*cdf0e10cSrcweir 			const uno::Reference< io::XOutputStream >& xOrigOutStream,
65*cdf0e10cSrcweir 			sal_Bool bInOpen,
66*cdf0e10cSrcweir 			sal_Bool bOutOpen )
67*cdf0e10cSrcweir 	: m_xFactory( xFactory )
68*cdf0e10cSrcweir 	, m_bInStreamBased( bInStreamBased )
69*cdf0e10cSrcweir 	, m_xOrigStream( xOrigStream )
70*cdf0e10cSrcweir 	, m_xOrigTruncate( xOrigTruncate )
71*cdf0e10cSrcweir 	, m_xOrigSeekable( xOrigSeekable )
72*cdf0e10cSrcweir 	, m_xOrigInStream( xOrigInStream )
73*cdf0e10cSrcweir 	, m_xOrigOutStream( xOrigOutStream )
74*cdf0e10cSrcweir 	, m_bInOpen( bInOpen )
75*cdf0e10cSrcweir 	, m_bOutOpen( bOutOpen )
76*cdf0e10cSrcweir 	{
77*cdf0e10cSrcweir 	}
78*cdf0e10cSrcweir };
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir // ========================================================================
81*cdf0e10cSrcweir // ------------------------------------------------------------------------
82*cdf0e10cSrcweir SwitchablePersistenceStream::SwitchablePersistenceStream(
83*cdf0e10cSrcweir 		const uno::Reference< lang::XMultiServiceFactory >& xFactory,
84*cdf0e10cSrcweir 		const uno::Reference< io::XStream >& xStream )
85*cdf0e10cSrcweir : m_xFactory( xFactory )
86*cdf0e10cSrcweir , m_pStreamData( NULL )
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir 	SwitchPersistenceTo( xStream );
89*cdf0e10cSrcweir }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir // ------------------------------------------------------------------------
92*cdf0e10cSrcweir SwitchablePersistenceStream::SwitchablePersistenceStream(
93*cdf0e10cSrcweir 		const uno::Reference< lang::XMultiServiceFactory >& xFactory,
94*cdf0e10cSrcweir 		const uno::Reference< io::XInputStream >& xInputStream )
95*cdf0e10cSrcweir : m_xFactory( xFactory )
96*cdf0e10cSrcweir , m_pStreamData( NULL )
97*cdf0e10cSrcweir {
98*cdf0e10cSrcweir 	SwitchPersistenceTo( xInputStream );
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir // ------------------------------------------------------------------------
102*cdf0e10cSrcweir SwitchablePersistenceStream::~SwitchablePersistenceStream()
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir 	CloseAll_Impl();
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir // ------------------------------------------------------------------------
108*cdf0e10cSrcweir void SwitchablePersistenceStream::SwitchPersistenceTo( const uno::Reference< io::XStream >& xStream )
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir 	uno::Reference< io::XTruncate > xNewTruncate( xStream, uno::UNO_QUERY_THROW );
111*cdf0e10cSrcweir 	uno::Reference< io::XSeekable > xNewSeekable( xStream, uno::UNO_QUERY_THROW );
112*cdf0e10cSrcweir 	uno::Reference< io::XInputStream > xNewInStream = xStream->getInputStream();
113*cdf0e10cSrcweir 	uno::Reference< io::XOutputStream > xNewOutStream = xStream->getOutputStream();
114*cdf0e10cSrcweir 	if ( !xNewInStream.is() || !xNewOutStream.is() )
115*cdf0e10cSrcweir 		throw uno::RuntimeException();
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 	sal_Int64 nPos = 0;
118*cdf0e10cSrcweir 	sal_Bool bInOpen = sal_False;
119*cdf0e10cSrcweir 	sal_Bool bOutOpen = sal_False;
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 	if ( m_pStreamData && m_pStreamData->m_xOrigSeekable.is() )
122*cdf0e10cSrcweir 	{
123*cdf0e10cSrcweir 		// check that the length is the same
124*cdf0e10cSrcweir 		if ( m_pStreamData->m_xOrigSeekable->getLength() != xNewSeekable->getLength() )
125*cdf0e10cSrcweir 			throw uno::RuntimeException();
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 		// get the current position
128*cdf0e10cSrcweir 		nPos = m_pStreamData->m_xOrigSeekable->getPosition();
129*cdf0e10cSrcweir 		bInOpen = m_pStreamData->m_bInOpen;
130*cdf0e10cSrcweir 		bOutOpen = m_pStreamData->m_bOutOpen;
131*cdf0e10cSrcweir 	}
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 	xNewSeekable->seek( nPos );
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 	CloseAll_Impl();
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 	m_pStreamData = new SPStreamData_Impl( m_xFactory, sal_False,
138*cdf0e10cSrcweir 											xStream, xNewTruncate, xNewSeekable, xNewInStream, xNewOutStream,
139*cdf0e10cSrcweir 											bInOpen, bOutOpen );
140*cdf0e10cSrcweir }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir // ------------------------------------------------------------------------
143*cdf0e10cSrcweir void SwitchablePersistenceStream::SwitchPersistenceTo( const uno::Reference< io::XInputStream >& xInputStream )
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir 	uno::Reference< io::XStream > xNewStream;
146*cdf0e10cSrcweir 	uno::Reference< io::XTruncate > xNewTruncate;
147*cdf0e10cSrcweir 	uno::Reference< io::XSeekable > xNewSeekable( xInputStream, uno::UNO_QUERY_THROW );
148*cdf0e10cSrcweir 	uno::Reference< io::XOutputStream > xNewOutStream;
149*cdf0e10cSrcweir 	if ( !xInputStream.is() )
150*cdf0e10cSrcweir 		throw uno::RuntimeException();
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	sal_Int64 nPos = 0;
153*cdf0e10cSrcweir 	sal_Bool bInOpen = sal_False;
154*cdf0e10cSrcweir 	sal_Bool bOutOpen = sal_False;
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 	if ( m_pStreamData && m_pStreamData->m_xOrigSeekable.is() )
157*cdf0e10cSrcweir 	{
158*cdf0e10cSrcweir 		// check that the length is the same
159*cdf0e10cSrcweir 		if ( m_pStreamData->m_xOrigSeekable->getLength() != xNewSeekable->getLength() )
160*cdf0e10cSrcweir 			throw uno::RuntimeException();
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 		// get the current position
163*cdf0e10cSrcweir 		nPos = m_pStreamData->m_xOrigSeekable->getPosition();
164*cdf0e10cSrcweir 		bInOpen = m_pStreamData->m_bInOpen;
165*cdf0e10cSrcweir 		bOutOpen = m_pStreamData->m_bOutOpen;
166*cdf0e10cSrcweir 	}
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 	xNewSeekable->seek( nPos );
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 	CloseAll_Impl();
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 	m_pStreamData = new SPStreamData_Impl( m_xFactory, sal_True,
173*cdf0e10cSrcweir 											xNewStream, xNewTruncate, xNewSeekable, xInputStream, xNewOutStream,
174*cdf0e10cSrcweir 											bInOpen, bOutOpen );
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir // ------------------------------------------------------------------------
179*cdf0e10cSrcweir void SwitchablePersistenceStream::CopyAndSwitchPersistenceTo( const uno::Reference< io::XStream >& xStream )
180*cdf0e10cSrcweir {
181*cdf0e10cSrcweir 	uno::Reference< io::XStream > xTargetStream = xStream;
182*cdf0e10cSrcweir 	uno::Reference< io::XSeekable > xTargetSeek;
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 	if ( !xTargetStream.is() )
185*cdf0e10cSrcweir 	{
186*cdf0e10cSrcweir 		xTargetStream = uno::Reference < io::XStream >(
187*cdf0e10cSrcweir 			m_xFactory->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
188*cdf0e10cSrcweir 			uno::UNO_QUERY_THROW );
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 		xTargetSeek = uno::Reference< io::XSeekable >( xTargetStream, uno::UNO_QUERY_THROW );
191*cdf0e10cSrcweir 	}
192*cdf0e10cSrcweir 	else
193*cdf0e10cSrcweir 	{
194*cdf0e10cSrcweir 		// the provided stream must be empty
195*cdf0e10cSrcweir 		xTargetSeek = uno::Reference< io::XSeekable >( xTargetStream, uno::UNO_QUERY_THROW );
196*cdf0e10cSrcweir 		if ( xTargetSeek->getLength() )
197*cdf0e10cSrcweir 			throw io::IOException();
198*cdf0e10cSrcweir 	}
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 	uno::Reference< io::XTruncate > xTargetTruncate( xTargetStream, uno::UNO_QUERY_THROW );
201*cdf0e10cSrcweir 	uno::Reference< io::XInputStream > xTargetInStream = xTargetStream->getInputStream();
202*cdf0e10cSrcweir 	uno::Reference< io::XOutputStream > xTargetOutStream = xTargetStream->getOutputStream();
203*cdf0e10cSrcweir 	if ( !xTargetInStream.is() || !xTargetOutStream.is() )
204*cdf0e10cSrcweir 		throw uno::RuntimeException();
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir 	if ( !m_pStreamData->m_xOrigInStream.is() || !m_pStreamData->m_xOrigSeekable.is() )
207*cdf0e10cSrcweir 		throw uno::RuntimeException();
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 	sal_Int64 nPos = m_pStreamData->m_xOrigSeekable->getPosition();
210*cdf0e10cSrcweir 	m_pStreamData->m_xOrigSeekable->seek( 0 );
211*cdf0e10cSrcweir 	::comphelper::OStorageHelper::CopyInputToOutput( m_pStreamData->m_xOrigInStream, xTargetOutStream );
212*cdf0e10cSrcweir 	xTargetOutStream->flush();
213*cdf0e10cSrcweir 	xTargetSeek->seek( nPos );
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 	sal_Bool bInOpen = m_pStreamData->m_bInOpen;
216*cdf0e10cSrcweir 	sal_Bool bOutOpen = m_pStreamData->m_bOutOpen;
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 	CloseAll_Impl();
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 	m_pStreamData = new SPStreamData_Impl( m_xFactory, sal_False,
221*cdf0e10cSrcweir 										xTargetStream, xTargetTruncate, xTargetSeek, xTargetInStream, xTargetOutStream,
222*cdf0e10cSrcweir 										bInOpen, bOutOpen );
223*cdf0e10cSrcweir }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir // ------------------------------------------------------------------------
226*cdf0e10cSrcweir void SwitchablePersistenceStream::CloseAll_Impl()
227*cdf0e10cSrcweir {
228*cdf0e10cSrcweir 	if ( m_pStreamData )
229*cdf0e10cSrcweir 	{
230*cdf0e10cSrcweir 		delete m_pStreamData;
231*cdf0e10cSrcweir 		m_pStreamData = NULL;
232*cdf0e10cSrcweir 	}
233*cdf0e10cSrcweir }
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir // com::sun::star::io::XStream
236*cdf0e10cSrcweir // ------------------------------------------------------------------------
237*cdf0e10cSrcweir uno::Reference< io::XInputStream > SAL_CALL SwitchablePersistenceStream::getInputStream(  )
238*cdf0e10cSrcweir 	throw (uno::RuntimeException)
239*cdf0e10cSrcweir {
240*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir     if ( m_pStreamData )
243*cdf0e10cSrcweir         m_pStreamData->m_bInOpen = sal_True;
244*cdf0e10cSrcweir 	return static_cast< io::XInputStream* >( this );
245*cdf0e10cSrcweir }
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir // ------------------------------------------------------------------------
249*cdf0e10cSrcweir uno::Reference< io::XOutputStream > SAL_CALL SwitchablePersistenceStream::getOutputStream(  )
250*cdf0e10cSrcweir 	throw (uno::RuntimeException)
251*cdf0e10cSrcweir {
252*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir     if ( m_pStreamData )
255*cdf0e10cSrcweir         m_pStreamData->m_bOutOpen = sal_True;
256*cdf0e10cSrcweir 	return static_cast< io::XOutputStream* >( this );
257*cdf0e10cSrcweir }
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir // com::sun::star::io::XInputStream
262*cdf0e10cSrcweir // ------------------------------------------------------------------------
263*cdf0e10cSrcweir ::sal_Int32 SAL_CALL SwitchablePersistenceStream::readBytes( uno::Sequence< ::sal_Int8 >& aData, ::sal_Int32 nBytesToRead )
264*cdf0e10cSrcweir 	throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException)
265*cdf0e10cSrcweir {
266*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 	if ( !m_pStreamData )
269*cdf0e10cSrcweir 		throw io::NotConnectedException();
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir 	// the original stream data should be provided
272*cdf0e10cSrcweir 	if ( !m_pStreamData->m_xOrigInStream.is() )
273*cdf0e10cSrcweir 		throw uno::RuntimeException();
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir 	return m_pStreamData->m_xOrigInStream->readBytes( aData, nBytesToRead );
276*cdf0e10cSrcweir }
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir // ------------------------------------------------------------------------
280*cdf0e10cSrcweir ::sal_Int32 SAL_CALL SwitchablePersistenceStream::readSomeBytes( uno::Sequence< ::sal_Int8 >& aData, ::sal_Int32 nMaxBytesToRead )
281*cdf0e10cSrcweir 	throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException)
282*cdf0e10cSrcweir {
283*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir 	if ( !m_pStreamData )
286*cdf0e10cSrcweir 		throw io::NotConnectedException();
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir 	// the original stream data should be provided
289*cdf0e10cSrcweir 	if ( !m_pStreamData->m_xOrigInStream.is() )
290*cdf0e10cSrcweir 		throw uno::RuntimeException();
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir 	return m_pStreamData->m_xOrigInStream->readBytes( aData, nMaxBytesToRead );
293*cdf0e10cSrcweir }
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir // ------------------------------------------------------------------------
296*cdf0e10cSrcweir void SAL_CALL SwitchablePersistenceStream::skipBytes( ::sal_Int32 nBytesToSkip )
297*cdf0e10cSrcweir 	throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException)
298*cdf0e10cSrcweir {
299*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir 	if ( !m_pStreamData )
302*cdf0e10cSrcweir 		throw io::NotConnectedException();
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir 	// the original stream data should be provided
305*cdf0e10cSrcweir 	if ( !m_pStreamData->m_xOrigInStream.is() )
306*cdf0e10cSrcweir 		throw uno::RuntimeException();
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir 	m_pStreamData->m_xOrigInStream->skipBytes( nBytesToSkip );
309*cdf0e10cSrcweir }
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir // ------------------------------------------------------------------------
313*cdf0e10cSrcweir ::sal_Int32 SAL_CALL SwitchablePersistenceStream::available(  )
314*cdf0e10cSrcweir 	throw (io::NotConnectedException, io::IOException, uno::RuntimeException)
315*cdf0e10cSrcweir {
316*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir 	if ( !m_pStreamData )
319*cdf0e10cSrcweir 		throw io::NotConnectedException();
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir 	// the original stream data should be provided
322*cdf0e10cSrcweir 	if ( !m_pStreamData->m_xOrigInStream.is() )
323*cdf0e10cSrcweir 		throw uno::RuntimeException();
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir 	return m_pStreamData->m_xOrigInStream->available();
326*cdf0e10cSrcweir }
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir // ------------------------------------------------------------------------
330*cdf0e10cSrcweir void SAL_CALL SwitchablePersistenceStream::closeInput()
331*cdf0e10cSrcweir 	throw (io::NotConnectedException, io::IOException, uno::RuntimeException)
332*cdf0e10cSrcweir {
333*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir 	if ( !m_pStreamData )
336*cdf0e10cSrcweir 		throw io::NotConnectedException();
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir 	m_pStreamData->m_bInOpen = sal_False;
339*cdf0e10cSrcweir 	if ( !m_pStreamData->m_bOutOpen )
340*cdf0e10cSrcweir 		CloseAll_Impl();
341*cdf0e10cSrcweir }
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir // com::sun::star::io::XOutputStream
346*cdf0e10cSrcweir // ------------------------------------------------------------------------
347*cdf0e10cSrcweir void SAL_CALL SwitchablePersistenceStream::writeBytes( const uno::Sequence< ::sal_Int8 >& aData )
348*cdf0e10cSrcweir 	throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException)
349*cdf0e10cSrcweir {
350*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir 	if ( !m_pStreamData )
353*cdf0e10cSrcweir 		throw io::NotConnectedException();
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir 	if ( m_pStreamData->m_bInStreamBased )
356*cdf0e10cSrcweir 		throw io::IOException();
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir 	// the original stream data should be provided
359*cdf0e10cSrcweir 	if ( !m_pStreamData->m_xOrigOutStream.is() )
360*cdf0e10cSrcweir 		throw uno::RuntimeException();
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir 	m_pStreamData->m_xOrigOutStream->writeBytes( aData );
363*cdf0e10cSrcweir }
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir // ------------------------------------------------------------------------
367*cdf0e10cSrcweir void SAL_CALL SwitchablePersistenceStream::flush(  )
368*cdf0e10cSrcweir 	throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException)
369*cdf0e10cSrcweir {
370*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir 	if ( !m_pStreamData || m_pStreamData->m_bInStreamBased )
373*cdf0e10cSrcweir 	{
374*cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "flush() is not acceptable!\n" );
375*cdf0e10cSrcweir 		return;
376*cdf0e10cSrcweir 		// in future throw exception, for now some code might call flush() on closed stream
377*cdf0e10cSrcweir 		// since file ucp implementation allows it
378*cdf0e10cSrcweir 		// throw io::NotConnectedException();
379*cdf0e10cSrcweir 	}
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir 	// the original stream data should be provided
382*cdf0e10cSrcweir 	if ( !m_pStreamData->m_xOrigOutStream.is() )
383*cdf0e10cSrcweir 		throw uno::RuntimeException();
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir 	m_pStreamData->m_xOrigOutStream->flush();
386*cdf0e10cSrcweir }
387*cdf0e10cSrcweir 
388*cdf0e10cSrcweir 
389*cdf0e10cSrcweir // ------------------------------------------------------------------------
390*cdf0e10cSrcweir void SAL_CALL SwitchablePersistenceStream::closeOutput(  )
391*cdf0e10cSrcweir 	throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException)
392*cdf0e10cSrcweir {
393*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
394*cdf0e10cSrcweir 
395*cdf0e10cSrcweir 	if ( !m_pStreamData )
396*cdf0e10cSrcweir 		throw io::NotConnectedException();
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir 	m_pStreamData->m_bOutOpen = sal_False;
399*cdf0e10cSrcweir 	if ( !m_pStreamData->m_bInOpen )
400*cdf0e10cSrcweir 		CloseAll_Impl();
401*cdf0e10cSrcweir }
402*cdf0e10cSrcweir 
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir 
405*cdf0e10cSrcweir // com::sun::star::io::XTruncate
406*cdf0e10cSrcweir // ------------------------------------------------------------------------
407*cdf0e10cSrcweir void SAL_CALL SwitchablePersistenceStream::truncate(  )
408*cdf0e10cSrcweir 	throw (io::IOException, uno::RuntimeException)
409*cdf0e10cSrcweir {
410*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
411*cdf0e10cSrcweir 
412*cdf0e10cSrcweir 	if ( !m_pStreamData )
413*cdf0e10cSrcweir 		throw io::NotConnectedException();
414*cdf0e10cSrcweir 
415*cdf0e10cSrcweir 	if ( m_pStreamData->m_bInStreamBased )
416*cdf0e10cSrcweir 		throw io::IOException();
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir 	// the original stream data should be provided
419*cdf0e10cSrcweir 	if ( !m_pStreamData->m_xOrigTruncate.is() )
420*cdf0e10cSrcweir 		throw uno::RuntimeException();
421*cdf0e10cSrcweir 
422*cdf0e10cSrcweir 	m_pStreamData->m_xOrigTruncate->truncate();
423*cdf0e10cSrcweir }
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir 
426*cdf0e10cSrcweir // com::sun::star::io::XSeekable
427*cdf0e10cSrcweir // ------------------------------------------------------------------------
428*cdf0e10cSrcweir void SAL_CALL SwitchablePersistenceStream::seek( ::sal_Int64 location )
429*cdf0e10cSrcweir 	throw (lang::IllegalArgumentException, io::IOException, uno::RuntimeException)
430*cdf0e10cSrcweir {
431*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir 	if ( !m_pStreamData )
434*cdf0e10cSrcweir 		throw io::NotConnectedException();
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir 	// the original stream data should be provided
437*cdf0e10cSrcweir 	if ( !m_pStreamData->m_xOrigSeekable.is() )
438*cdf0e10cSrcweir 		throw uno::RuntimeException();
439*cdf0e10cSrcweir 
440*cdf0e10cSrcweir 	m_pStreamData->m_xOrigSeekable->seek( location );
441*cdf0e10cSrcweir }
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir 
444*cdf0e10cSrcweir // ------------------------------------------------------------------------
445*cdf0e10cSrcweir ::sal_Int64 SAL_CALL SwitchablePersistenceStream::getPosition(  )
446*cdf0e10cSrcweir 	throw (io::IOException, uno::RuntimeException)
447*cdf0e10cSrcweir {
448*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
449*cdf0e10cSrcweir 
450*cdf0e10cSrcweir 	if ( !m_pStreamData )
451*cdf0e10cSrcweir 		throw io::NotConnectedException();
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir 	// the original stream data should be provided
454*cdf0e10cSrcweir 	if ( !m_pStreamData->m_xOrigSeekable.is() )
455*cdf0e10cSrcweir 		throw uno::RuntimeException();
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir 	return m_pStreamData->m_xOrigSeekable->getPosition();
458*cdf0e10cSrcweir }
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir 
461*cdf0e10cSrcweir // ------------------------------------------------------------------------
462*cdf0e10cSrcweir ::sal_Int64 SAL_CALL SwitchablePersistenceStream::getLength(  )
463*cdf0e10cSrcweir 	throw (io::IOException, uno::RuntimeException)
464*cdf0e10cSrcweir {
465*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
466*cdf0e10cSrcweir 
467*cdf0e10cSrcweir 	if ( !m_pStreamData )
468*cdf0e10cSrcweir 		throw io::NotConnectedException();
469*cdf0e10cSrcweir 
470*cdf0e10cSrcweir 	// the original stream data should be provided
471*cdf0e10cSrcweir 	if ( !m_pStreamData->m_xOrigSeekable.is() )
472*cdf0e10cSrcweir 		throw uno::RuntimeException();
473*cdf0e10cSrcweir 
474*cdf0e10cSrcweir 	return m_pStreamData->m_xOrigSeekable->getLength();
475*cdf0e10cSrcweir }
476*cdf0e10cSrcweir 
477*cdf0e10cSrcweir // ------------------------------------------------------------------------
478*cdf0e10cSrcweir void SAL_CALL SwitchablePersistenceStream::waitForCompletion()
479*cdf0e10cSrcweir 	throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
480*cdf0e10cSrcweir {
481*cdf0e10cSrcweir 	if ( !m_pStreamData )
482*cdf0e10cSrcweir 		throw io::NotConnectedException();
483*cdf0e10cSrcweir 
484*cdf0e10cSrcweir 	uno::Reference< io::XAsyncOutputMonitor > asyncOutputMonitor( m_pStreamData->m_xOrigOutStream, uno::UNO_QUERY );
485*cdf0e10cSrcweir 	if ( asyncOutputMonitor.is() )
486*cdf0e10cSrcweir 		asyncOutputMonitor->waitForCompletion();
487*cdf0e10cSrcweir }
488*cdf0e10cSrcweir 
489