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
10*a3872823SAndrew Rist  *
11*a3872823SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a3872823SAndrew Rist  *
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.
19*a3872823SAndrew Rist  *
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 "wrapstreamforshare.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir using namespace ::com::sun::star;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
WrapStreamForShare(const uno::Reference<io::XInputStream> & xInStream,const SotMutexHolderRef & rMutexRef)33cdf0e10cSrcweir WrapStreamForShare::WrapStreamForShare( const uno::Reference< io::XInputStream >& xInStream,
34cdf0e10cSrcweir 										const SotMutexHolderRef& rMutexRef )
35cdf0e10cSrcweir : m_rMutexRef( rMutexRef )
36cdf0e10cSrcweir , m_xInStream( xInStream )
37cdf0e10cSrcweir , m_nCurPos( 0 )
38cdf0e10cSrcweir {
39cdf0e10cSrcweir 	m_xSeekable = uno::Reference< io::XSeekable >( m_xInStream, uno::UNO_QUERY );
40cdf0e10cSrcweir 	if ( !m_rMutexRef.Is() || !m_xInStream.is() || !m_xSeekable.is() )
41cdf0e10cSrcweir 	{
42cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Wrong initialization of wrapping stream!\n" );
43cdf0e10cSrcweir 		throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
44cdf0e10cSrcweir 	}
45cdf0e10cSrcweir }
46cdf0e10cSrcweir 
~WrapStreamForShare()47cdf0e10cSrcweir WrapStreamForShare::~WrapStreamForShare()
48cdf0e10cSrcweir {
49cdf0e10cSrcweir }
50cdf0e10cSrcweir 
51cdf0e10cSrcweir // XInputStream
readBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)52cdf0e10cSrcweir sal_Int32 SAL_CALL WrapStreamForShare::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
53cdf0e10cSrcweir 		throw ( io::NotConnectedException,
54cdf0e10cSrcweir 				io::BufferSizeExceededException,
55cdf0e10cSrcweir 				io::IOException,
56cdf0e10cSrcweir 				uno::RuntimeException )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	if ( !m_xInStream.is() )
61cdf0e10cSrcweir 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 	m_xSeekable->seek( m_nCurPos );
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 	sal_Int32 nRead = m_xInStream->readBytes( aData, nBytesToRead );
66cdf0e10cSrcweir 	m_nCurPos += nRead;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	return nRead;
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
readSomeBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)71cdf0e10cSrcweir sal_Int32 SAL_CALL WrapStreamForShare::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
72cdf0e10cSrcweir 		throw ( io::NotConnectedException,
73cdf0e10cSrcweir 				io::BufferSizeExceededException,
74cdf0e10cSrcweir 				io::IOException,
75cdf0e10cSrcweir 				uno::RuntimeException )
76cdf0e10cSrcweir {
77cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 	if ( !m_xInStream.is() )
80cdf0e10cSrcweir 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 	m_xSeekable->seek( m_nCurPos );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 	sal_Int32 nRead = m_xInStream->readSomeBytes( aData, nMaxBytesToRead );
85cdf0e10cSrcweir 	m_nCurPos += nRead;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 	return nRead;
88cdf0e10cSrcweir }
89cdf0e10cSrcweir 
skipBytes(sal_Int32 nBytesToSkip)90cdf0e10cSrcweir void SAL_CALL WrapStreamForShare::skipBytes( sal_Int32 nBytesToSkip )
91cdf0e10cSrcweir 		throw ( io::NotConnectedException,
92cdf0e10cSrcweir 				io::BufferSizeExceededException,
93cdf0e10cSrcweir 				io::IOException,
94cdf0e10cSrcweir 				uno::RuntimeException )
95cdf0e10cSrcweir {
96cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	if ( !m_xInStream.is() )
99cdf0e10cSrcweir 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 	m_xSeekable->seek( m_nCurPos );
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	m_xInStream->skipBytes( nBytesToSkip );
104cdf0e10cSrcweir 	m_nCurPos = m_xSeekable->getPosition();
105cdf0e10cSrcweir }
106cdf0e10cSrcweir 
available()107cdf0e10cSrcweir sal_Int32 SAL_CALL WrapStreamForShare::available()
108cdf0e10cSrcweir 		throw ( io::NotConnectedException,
109cdf0e10cSrcweir 				io::IOException,
110cdf0e10cSrcweir 				uno::RuntimeException )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	if ( !m_xInStream.is() )
115cdf0e10cSrcweir 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 	return m_xInStream->available();
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
closeInput()120cdf0e10cSrcweir void SAL_CALL WrapStreamForShare::closeInput()
121cdf0e10cSrcweir 		throw ( io::NotConnectedException,
122cdf0e10cSrcweir 				io::IOException,
123cdf0e10cSrcweir 				uno::RuntimeException )
124cdf0e10cSrcweir {
125cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 	if ( !m_xInStream.is() )
128cdf0e10cSrcweir 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 	// the package is the owner so it will close the stream
131cdf0e10cSrcweir 	// m_xInStream->closeInput();
132cdf0e10cSrcweir 	m_xInStream = uno::Reference< io::XInputStream >();
133cdf0e10cSrcweir 	m_xSeekable = uno::Reference< io::XSeekable >();
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir // XSeekable
seek(sal_Int64 location)137cdf0e10cSrcweir void SAL_CALL WrapStreamForShare::seek( sal_Int64 location )
138cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
139cdf0e10cSrcweir 				io::IOException,
140cdf0e10cSrcweir 				uno::RuntimeException )
141cdf0e10cSrcweir {
142cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 	if ( !m_xInStream.is() )
145cdf0e10cSrcweir 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	// let stream implementation do all the checking
148cdf0e10cSrcweir 	m_xSeekable->seek( location );
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 	m_nCurPos = m_xSeekable->getPosition();
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
getPosition()153cdf0e10cSrcweir sal_Int64 SAL_CALL WrapStreamForShare::getPosition()
154cdf0e10cSrcweir 		throw ( io::IOException,
155cdf0e10cSrcweir 				uno::RuntimeException)
156cdf0e10cSrcweir {
157cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	if ( !m_xInStream.is() )
160cdf0e10cSrcweir 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	return m_nCurPos;
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
getLength()165cdf0e10cSrcweir sal_Int64 SAL_CALL WrapStreamForShare::getLength()
166cdf0e10cSrcweir 		throw ( io::IOException,
167cdf0e10cSrcweir 				uno::RuntimeException )
168cdf0e10cSrcweir {
169cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	if ( !m_xInStream.is() )
172cdf0e10cSrcweir 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 	return m_xSeekable->getLength();
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
177