1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_package.hxx"
26 #include <osl/diagnose.h>
27 
28 #include "wrapstreamforshare.hxx"
29 
30 using namespace ::com::sun::star;
31 
32 
WrapStreamForShare(const uno::Reference<io::XInputStream> & xInStream,const SotMutexHolderRef & rMutexRef)33 WrapStreamForShare::WrapStreamForShare( const uno::Reference< io::XInputStream >& xInStream,
34 										const SotMutexHolderRef& rMutexRef )
35 : m_rMutexRef( rMutexRef )
36 , m_xInStream( xInStream )
37 , m_nCurPos( 0 )
38 {
39 	m_xSeekable = uno::Reference< io::XSeekable >( m_xInStream, uno::UNO_QUERY );
40 	if ( !m_rMutexRef.Is() || !m_xInStream.is() || !m_xSeekable.is() )
41 	{
42 		OSL_ENSURE( sal_False, "Wrong initialization of wrapping stream!\n" );
43 		throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
44 	}
45 }
46 
~WrapStreamForShare()47 WrapStreamForShare::~WrapStreamForShare()
48 {
49 }
50 
51 // XInputStream
readBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)52 sal_Int32 SAL_CALL WrapStreamForShare::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
53 		throw ( io::NotConnectedException,
54 				io::BufferSizeExceededException,
55 				io::IOException,
56 				uno::RuntimeException )
57 {
58 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
59 
60 	if ( !m_xInStream.is() )
61 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
62 
63 	m_xSeekable->seek( m_nCurPos );
64 
65 	sal_Int32 nRead = m_xInStream->readBytes( aData, nBytesToRead );
66 	m_nCurPos += nRead;
67 
68 	return nRead;
69 }
70 
readSomeBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)71 sal_Int32 SAL_CALL WrapStreamForShare::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
72 		throw ( io::NotConnectedException,
73 				io::BufferSizeExceededException,
74 				io::IOException,
75 				uno::RuntimeException )
76 {
77 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
78 
79 	if ( !m_xInStream.is() )
80 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
81 
82 	m_xSeekable->seek( m_nCurPos );
83 
84 	sal_Int32 nRead = m_xInStream->readSomeBytes( aData, nMaxBytesToRead );
85 	m_nCurPos += nRead;
86 
87 	return nRead;
88 }
89 
skipBytes(sal_Int32 nBytesToSkip)90 void SAL_CALL WrapStreamForShare::skipBytes( sal_Int32 nBytesToSkip )
91 		throw ( io::NotConnectedException,
92 				io::BufferSizeExceededException,
93 				io::IOException,
94 				uno::RuntimeException )
95 {
96 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
97 
98 	if ( !m_xInStream.is() )
99 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
100 
101 	m_xSeekable->seek( m_nCurPos );
102 
103 	m_xInStream->skipBytes( nBytesToSkip );
104 	m_nCurPos = m_xSeekable->getPosition();
105 }
106 
available()107 sal_Int32 SAL_CALL WrapStreamForShare::available()
108 		throw ( io::NotConnectedException,
109 				io::IOException,
110 				uno::RuntimeException )
111 {
112 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
113 
114 	if ( !m_xInStream.is() )
115 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
116 
117 	return m_xInStream->available();
118 }
119 
closeInput()120 void SAL_CALL WrapStreamForShare::closeInput()
121 		throw ( io::NotConnectedException,
122 				io::IOException,
123 				uno::RuntimeException )
124 {
125 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
126 
127 	if ( !m_xInStream.is() )
128 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
129 
130 	// the package is the owner so it will close the stream
131 	// m_xInStream->closeInput();
132 	m_xInStream = uno::Reference< io::XInputStream >();
133 	m_xSeekable = uno::Reference< io::XSeekable >();
134 }
135 
136 // XSeekable
seek(sal_Int64 location)137 void SAL_CALL WrapStreamForShare::seek( sal_Int64 location )
138 		throw ( lang::IllegalArgumentException,
139 				io::IOException,
140 				uno::RuntimeException )
141 {
142 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
143 
144 	if ( !m_xInStream.is() )
145 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
146 
147 	// let stream implementation do all the checking
148 	m_xSeekable->seek( location );
149 
150 	m_nCurPos = m_xSeekable->getPosition();
151 }
152 
getPosition()153 sal_Int64 SAL_CALL WrapStreamForShare::getPosition()
154 		throw ( io::IOException,
155 				uno::RuntimeException)
156 {
157 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
158 
159 	if ( !m_xInStream.is() )
160 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
161 
162 	return m_nCurPos;
163 }
164 
getLength()165 sal_Int64 SAL_CALL WrapStreamForShare::getLength()
166 		throw ( io::IOException,
167 				uno::RuntimeException )
168 {
169 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
170 
171 	if ( !m_xInStream.is() )
172 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
173 
174 	return m_xSeekable->getLength();
175 }
176 
177