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