1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_package.hxx"
30 #include <osl/diagnose.h>
31 
32 #include "wrapstreamforshare.hxx"
33 
34 using namespace ::com::sun::star;
35 
36 
37 WrapStreamForShare::WrapStreamForShare( const uno::Reference< io::XInputStream >& xInStream,
38 										const SotMutexHolderRef& rMutexRef )
39 : m_rMutexRef( rMutexRef )
40 , m_xInStream( xInStream )
41 , m_nCurPos( 0 )
42 {
43 	m_xSeekable = uno::Reference< io::XSeekable >( m_xInStream, uno::UNO_QUERY );
44 	if ( !m_rMutexRef.Is() || !m_xInStream.is() || !m_xSeekable.is() )
45 	{
46 		OSL_ENSURE( sal_False, "Wrong initialization of wrapping stream!\n" );
47 		throw uno::RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
48 	}
49 }
50 
51 WrapStreamForShare::~WrapStreamForShare()
52 {
53 }
54 
55 // XInputStream
56 sal_Int32 SAL_CALL WrapStreamForShare::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
57 		throw ( io::NotConnectedException,
58 				io::BufferSizeExceededException,
59 				io::IOException,
60 				uno::RuntimeException )
61 {
62 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
63 
64 	if ( !m_xInStream.is() )
65 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
66 
67 	m_xSeekable->seek( m_nCurPos );
68 
69 	sal_Int32 nRead = m_xInStream->readBytes( aData, nBytesToRead );
70 	m_nCurPos += nRead;
71 
72 	return nRead;
73 }
74 
75 sal_Int32 SAL_CALL WrapStreamForShare::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
76 		throw ( io::NotConnectedException,
77 				io::BufferSizeExceededException,
78 				io::IOException,
79 				uno::RuntimeException )
80 {
81 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
82 
83 	if ( !m_xInStream.is() )
84 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
85 
86 	m_xSeekable->seek( m_nCurPos );
87 
88 	sal_Int32 nRead = m_xInStream->readSomeBytes( aData, nMaxBytesToRead );
89 	m_nCurPos += nRead;
90 
91 	return nRead;
92 }
93 
94 void SAL_CALL WrapStreamForShare::skipBytes( sal_Int32 nBytesToSkip )
95 		throw ( io::NotConnectedException,
96 				io::BufferSizeExceededException,
97 				io::IOException,
98 				uno::RuntimeException )
99 {
100 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
101 
102 	if ( !m_xInStream.is() )
103 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
104 
105 	m_xSeekable->seek( m_nCurPos );
106 
107 	m_xInStream->skipBytes( nBytesToSkip );
108 	m_nCurPos = m_xSeekable->getPosition();
109 }
110 
111 sal_Int32 SAL_CALL WrapStreamForShare::available()
112 		throw ( io::NotConnectedException,
113 				io::IOException,
114 				uno::RuntimeException )
115 {
116 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
117 
118 	if ( !m_xInStream.is() )
119 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
120 
121 	return m_xInStream->available();
122 }
123 
124 void SAL_CALL WrapStreamForShare::closeInput()
125 		throw ( io::NotConnectedException,
126 				io::IOException,
127 				uno::RuntimeException )
128 {
129 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
130 
131 	if ( !m_xInStream.is() )
132 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
133 
134 	// the package is the owner so it will close the stream
135 	// m_xInStream->closeInput();
136 	m_xInStream = uno::Reference< io::XInputStream >();
137 	m_xSeekable = uno::Reference< io::XSeekable >();
138 }
139 
140 // XSeekable
141 void SAL_CALL WrapStreamForShare::seek( sal_Int64 location )
142 		throw ( lang::IllegalArgumentException,
143 				io::IOException,
144 				uno::RuntimeException )
145 {
146 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
147 
148 	if ( !m_xInStream.is() )
149 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
150 
151 	// let stream implementation do all the checking
152 	m_xSeekable->seek( location );
153 
154 	m_nCurPos = m_xSeekable->getPosition();
155 }
156 
157 sal_Int64 SAL_CALL WrapStreamForShare::getPosition()
158 		throw ( io::IOException,
159 				uno::RuntimeException)
160 {
161 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
162 
163 	if ( !m_xInStream.is() )
164 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
165 
166 	return m_nCurPos;
167 }
168 
169 sal_Int64 SAL_CALL WrapStreamForShare::getLength()
170 		throw ( io::IOException,
171 				uno::RuntimeException )
172 {
173 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
174 
175 	if ( !m_xInStream.is() )
176 		throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() );
177 
178 	return m_xSeekable->getLength();
179 }
180 
181