1*b5088357SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b5088357SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b5088357SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b5088357SAndrew Rist  * distributed with this work for additional information
6*b5088357SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b5088357SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b5088357SAndrew Rist  * "License"); you may not use this file except in compliance
9*b5088357SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b5088357SAndrew Rist  *
11*b5088357SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b5088357SAndrew Rist  *
13*b5088357SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b5088357SAndrew Rist  * software distributed under the License is distributed on an
15*b5088357SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b5088357SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b5088357SAndrew Rist  * specific language governing permissions and limitations
18*b5088357SAndrew Rist  * under the License.
19*b5088357SAndrew Rist  *
20*b5088357SAndrew Rist  *************************************************************/
21*b5088357SAndrew Rist 
22*b5088357SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_unotools.hxx"
26cdf0e10cSrcweir #include <unotools/streamhelper.hxx>
27cdf0e10cSrcweir #include <tools/debug.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir namespace utl
30cdf0e10cSrcweir {
31cdf0e10cSrcweir 
32cdf0e10cSrcweir //------------------------------------------------------------------------------
acquire()33cdf0e10cSrcweir void SAL_CALL OInputStreamHelper::acquire() throw ()
34cdf0e10cSrcweir {
35cdf0e10cSrcweir 	InputStreamHelper_Base::acquire();
36cdf0e10cSrcweir }
37cdf0e10cSrcweir 
38cdf0e10cSrcweir //------------------------------------------------------------------------------
release()39cdf0e10cSrcweir void SAL_CALL OInputStreamHelper::release() throw ()
40cdf0e10cSrcweir {
41cdf0e10cSrcweir 	InputStreamHelper_Base::release();
42cdf0e10cSrcweir }
43cdf0e10cSrcweir 
44cdf0e10cSrcweir //------------------------------------------------------------------------------
readBytes(staruno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)45cdf0e10cSrcweir sal_Int32 SAL_CALL OInputStreamHelper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
46cdf0e10cSrcweir 	throw(stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
47cdf0e10cSrcweir {
48cdf0e10cSrcweir 	if (!m_xLockBytes.Is())
49cdf0e10cSrcweir 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	if (nBytesToRead < 0)
52cdf0e10cSrcweir 		throw stario::BufferSizeExceededException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
55cdf0e10cSrcweir 	aData.realloc(nBytesToRead);
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 	sal_Size nRead;
58cdf0e10cSrcweir 	ErrCode nError = m_xLockBytes->ReadAt(m_nActPos, (void*)aData.getArray(), nBytesToRead, &nRead);
59cdf0e10cSrcweir 	// FIXME  nRead could be truncated on 64-bit arches
60cdf0e10cSrcweir 	m_nActPos += (sal_uInt32)nRead;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 	if (nError != ERRCODE_NONE)
63cdf0e10cSrcweir 		throw stario::IOException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 	// adjust sequence if data read is lower than the desired data
66cdf0e10cSrcweir 	if (nRead < (sal_uInt32)nBytesToRead)
67cdf0e10cSrcweir 		aData.realloc( nRead );
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 	return nRead;
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
seek(sal_Int64 location)72cdf0e10cSrcweir void SAL_CALL OInputStreamHelper::seek( sal_Int64 location ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
73cdf0e10cSrcweir {
74cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
75cdf0e10cSrcweir     // cast is truncating, but position would be truncated as soon as
76cdf0e10cSrcweir     // put into SvLockBytes anyway
77cdf0e10cSrcweir     m_nActPos = sal::static_int_cast<sal_uInt32>(location);
78cdf0e10cSrcweir }
79cdf0e10cSrcweir 
getPosition()80cdf0e10cSrcweir sal_Int64 SAL_CALL OInputStreamHelper::getPosition(  ) throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
81cdf0e10cSrcweir {
82cdf0e10cSrcweir     return m_nActPos;
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
getLength()85cdf0e10cSrcweir sal_Int64 SAL_CALL OInputStreamHelper::getLength(  ) throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
86cdf0e10cSrcweir {
87cdf0e10cSrcweir 	if (!m_xLockBytes.Is())
88cdf0e10cSrcweir         return 0;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
91cdf0e10cSrcweir     SvLockBytesStat aStat;
92cdf0e10cSrcweir     m_xLockBytes->Stat( &aStat, SVSTATFLAG_DEFAULT );
93cdf0e10cSrcweir 	return aStat.nSize;
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir //------------------------------------------------------------------------------
readSomeBytes(staruno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)97cdf0e10cSrcweir sal_Int32 SAL_CALL OInputStreamHelper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData,
98cdf0e10cSrcweir 													 sal_Int32 nMaxBytesToRead)
99cdf0e10cSrcweir 	throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
100cdf0e10cSrcweir {
101cdf0e10cSrcweir 	// read all data desired
102cdf0e10cSrcweir 	return readBytes(aData, nMaxBytesToRead);
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir //------------------------------------------------------------------------------
skipBytes(sal_Int32 nBytesToSkip)106cdf0e10cSrcweir void SAL_CALL OInputStreamHelper::skipBytes(sal_Int32 nBytesToSkip)
107cdf0e10cSrcweir 	throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
108cdf0e10cSrcweir {
109cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
110cdf0e10cSrcweir 	if (!m_xLockBytes.Is())
111cdf0e10cSrcweir 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	if (nBytesToSkip < 0)
114cdf0e10cSrcweir 		throw stario::BufferSizeExceededException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	m_nActPos += nBytesToSkip;
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir //------------------------------------------------------------------------------
available()120cdf0e10cSrcweir sal_Int32 SAL_CALL OInputStreamHelper::available()
121cdf0e10cSrcweir 	throw (stario::NotConnectedException, stario::IOException, staruno::RuntimeException)
122cdf0e10cSrcweir {
123cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
124cdf0e10cSrcweir 	if (!m_xLockBytes.Is())
125cdf0e10cSrcweir 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 	return m_nAvailable;
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir //------------------------------------------------------------------------------
closeInput()131cdf0e10cSrcweir void SAL_CALL OInputStreamHelper::closeInput()
132cdf0e10cSrcweir 	throw (stario::NotConnectedException, stario::IOException, staruno::RuntimeException)
133cdf0e10cSrcweir {
134cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
135cdf0e10cSrcweir 	if (!m_xLockBytes.Is())
136cdf0e10cSrcweir 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 	m_xLockBytes = NULL;
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir /*************************************************************************/
142cdf0e10cSrcweir //------------------------------------------------------------------------------
acquire()143cdf0e10cSrcweir void SAL_CALL OOutputStreamHelper::acquire() throw ()
144cdf0e10cSrcweir {
145cdf0e10cSrcweir 	OutputStreamHelper_Base::acquire();
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir //------------------------------------------------------------------------------
release()149cdf0e10cSrcweir void SAL_CALL OOutputStreamHelper::release() throw ()
150cdf0e10cSrcweir {
151cdf0e10cSrcweir 	OutputStreamHelper_Base::release();
152cdf0e10cSrcweir }
153cdf0e10cSrcweir // stario::XOutputStream
154cdf0e10cSrcweir //------------------------------------------------------------------------------
writeBytes(const staruno::Sequence<sal_Int8> & aData)155cdf0e10cSrcweir void SAL_CALL OOutputStreamHelper::writeBytes(const staruno::Sequence< sal_Int8 >& aData)
156cdf0e10cSrcweir 	throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
157cdf0e10cSrcweir {
158cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
159cdf0e10cSrcweir 	if (!m_xLockBytes.Is())
160cdf0e10cSrcweir 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	sal_Size nWritten;
163cdf0e10cSrcweir 	ErrCode nError = m_xLockBytes->WriteAt( m_nActPos, aData.getConstArray(), aData.getLength(), &nWritten );
164cdf0e10cSrcweir 	// FIXME  nWritten could be truncated on 64-bit arches
165cdf0e10cSrcweir 	m_nActPos += (sal_uInt32)nWritten;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 	if (nError != ERRCODE_NONE ||
168cdf0e10cSrcweir 		sal::static_int_cast<sal_Int32>(nWritten) != aData.getLength())
169cdf0e10cSrcweir 	{
170cdf0e10cSrcweir 		throw stario::IOException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
171cdf0e10cSrcweir 	}
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir //------------------------------------------------------------------
flush()175cdf0e10cSrcweir void SAL_CALL OOutputStreamHelper::flush()
176cdf0e10cSrcweir 	throw (stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
177cdf0e10cSrcweir {
178cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
179cdf0e10cSrcweir 	if (!m_xLockBytes.Is())
180cdf0e10cSrcweir 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 	ErrCode nError = m_xLockBytes->Flush();
183cdf0e10cSrcweir 	if (nError != ERRCODE_NONE)
184cdf0e10cSrcweir 		throw stario::IOException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
185cdf0e10cSrcweir }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir //------------------------------------------------------------------
closeOutput()188cdf0e10cSrcweir void SAL_CALL OOutputStreamHelper::closeOutput(  )
189cdf0e10cSrcweir 	throw(stario::NotConnectedException, stario::BufferSizeExceededException, stario::IOException, staruno::RuntimeException)
190cdf0e10cSrcweir {
191cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
192cdf0e10cSrcweir 	if (!m_xLockBytes.Is())
193cdf0e10cSrcweir 		throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this));
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 	m_xLockBytes = NULL;
196cdf0e10cSrcweir }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir } // namespace utl
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 
201