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_toolkit.hxx"
26 
27 
28 #include <toolkit/helper/unomemorystream.hxx>
29 #include <algorithm>
30 
31 //	----------------------------------------------------
32 //	class UnoMemoryStream
33 //	----------------------------------------------------
UnoMemoryStream(sal_uInt32 nInitSize,sal_uInt32 nInitResize)34 UnoMemoryStream::UnoMemoryStream( sal_uInt32 nInitSize, sal_uInt32 nInitResize )
35 	: SvMemoryStream( nInitSize, nInitResize )
36 {
37 }
38 
39 // ::com::sun::star::uno::XInterface
queryInterface(const::com::sun::star::uno::Type & rType)40 ::com::sun::star::uno::Any UnoMemoryStream::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException)
41 {
42 	::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType,
43 										SAL_STATIC_CAST( ::com::sun::star::io::XInputStream*, this ) );
44 	return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
45 }
46 
47 
48 // ::com::sun::star::io::XInputStream
readBytes(::com::sun::star::uno::Sequence<sal_Int8> & rData,sal_Int32 nBytesToRead)49 sal_Int32 UnoMemoryStream::readBytes( ::com::sun::star::uno::Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
50 {
51 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
52 
53 	sal_Int32 nRead = available();
54 	if ( nRead > nBytesToRead )
55 		nRead = nBytesToRead;
56 
57 	rData = ::com::sun::star::uno::Sequence< sal_Int8 >( nRead );
58 	Read( rData.getArray(), nRead );
59 
60 	return nRead;
61 }
62 
readSomeBytes(::com::sun::star::uno::Sequence<sal_Int8> & rData,sal_Int32 nMaxBytesToRead)63 sal_Int32 UnoMemoryStream::readSomeBytes( ::com::sun::star::uno::Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
64 {
65 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
66 
67 	sal_Int32 nAvailable = available();
68 	if( nAvailable )
69 	{
70 		return readBytes( rData, std::min( nMaxBytesToRead , nAvailable ) );
71 	}
72 	else
73 	{
74 		// Not the most effective method, but it sticks to the specification
75 		return readBytes( rData, 1 );
76 	}
77 }
78 
skipBytes(sal_Int32 nBytesToSkip)79 void UnoMemoryStream::skipBytes( sal_Int32 nBytesToSkip ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
80 {
81 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
82 
83 	SeekRel( nBytesToSkip );
84 }
85 
available()86 sal_Int32 UnoMemoryStream::available() throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
87 {
88 	::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
89 
90 	sal_uInt32 nStreamPos = Tell();
91 	sal_uInt32 nEnd = Seek( STREAM_SEEK_TO_END );
92 	Seek( nStreamPos );
93 	return nEnd - nStreamPos;
94 }
95 
closeInput()96 void UnoMemoryStream::closeInput() throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
97 {
98 	// nothing to do
99 }
100 
101 
102 
103 
104 
105