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