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 #include "unotools/unotoolsdllapi.h"
28 
29 #ifndef _UTL_STREAM_WRAPPER_HXX_
30 #define _UTL_STREAM_WRAPPER_HXX_
31 #include <osl/mutex.hxx>
32 #include <com/sun/star/io/XOutputStream.hpp>
33 #include <com/sun/star/io/XInputStream.hpp>
34 #include <com/sun/star/io/XSeekable.hpp>
35 #include <com/sun/star/io/XTruncate.hpp>
36 #include <com/sun/star/io/XStream.hpp>
37 #include <cppuhelper/implbase3.hxx>
38 #include <cppuhelper/implbase1.hxx>
39 #include <comphelper/uno3.hxx>
40 
41 class SvStream;
42 
43 namespace utl
44 {
45 	namespace stario	= ::com::sun::star::io;
46 	namespace staruno	= ::com::sun::star::uno;
47 
48 //==================================================================
49 //= OInputStreamWrapper
50 //==================================================================
51 typedef ::cppu::WeakImplHelper1	<	stario::XInputStream
52 								> InputStreamWrapper_Base;
53 	// needed for some compilers
54 /// helper class for wrapping an SvStream into an <type scope="com.sun.star.io">XInputStream</type>
55 class UNOTOOLS_DLLPUBLIC OInputStreamWrapper : public InputStreamWrapper_Base
56 {
57 protected:
58 	::osl::Mutex	m_aMutex;
59 	SvStream*		m_pSvStream;
60 	sal_Bool		m_bSvStreamOwner : 1;
61     OInputStreamWrapper()
62                     { m_pSvStream = 0; m_bSvStreamOwner = sal_False; }
63     void            SetStream(SvStream* _pStream, sal_Bool bOwner )
64                     { m_pSvStream = _pStream; m_bSvStreamOwner = bOwner; }
65 
66 public:
67 	OInputStreamWrapper(SvStream& _rStream);
68 	OInputStreamWrapper(SvStream* pStream, sal_Bool bOwner=sal_False);
69 	virtual ~OInputStreamWrapper();
70 
71 // UNO Anbindung
72 	DECLARE_UNO3_AGG_DEFAULTS(OInputStreamWrapper, InputStreamWrapper_Base);
73 
74 // stario::XInputStream
75 	virtual sal_Int32	SAL_CALL	readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead) throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException);
76 	virtual sal_Int32	SAL_CALL	readSomeBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException);
77 	virtual void		SAL_CALL	skipBytes(sal_Int32 nBytesToSkip) throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException);
78 	virtual sal_Int32	SAL_CALL	available() throw(stario::NotConnectedException, staruno::RuntimeException);
79 	virtual void		SAL_CALL	closeInput() throw(stario::NotConnectedException, staruno::RuntimeException);
80 
81 protected:
82 	/// throws a NotConnectedException if the object is not connected anymore
83 	void checkConnected() const;
84 	/// throws an exception according to the error flag of m_pSvStream
85 	void checkError() const;
86 };
87 
88 //==================================================================
89 //= OSeekableInputStreamWrapper
90 //==================================================================
91 typedef ::cppu::ImplHelper1	<	::com::sun::star::io::XSeekable
92 							>	OSeekableInputStreamWrapper_Base;
93 /** helper class for wrapping an SvStream into an <type scope="com.sun.star.io">XInputStream</type>
94 	which is seekable (i.e. supports the <type scope="com.sun.star.io">XSeekable</type> interface).
95 */
96 class UNOTOOLS_DLLPUBLIC OSeekableInputStreamWrapper : public ::cppu::ImplInheritanceHelper1 < OInputStreamWrapper, com::sun::star::io::XSeekable >
97 {
98 protected:
99     OSeekableInputStreamWrapper() {}
100 public:
101 	OSeekableInputStreamWrapper(SvStream& _rStream);
102 	OSeekableInputStreamWrapper(SvStream* _pStream, sal_Bool _bOwner = sal_False);
103 
104 	// XSeekable
105     virtual void SAL_CALL seek( sal_Int64 _nLocation ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
106     virtual sal_Int64 SAL_CALL getPosition(  ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
107     virtual sal_Int64 SAL_CALL getLength(  ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
108 };
109 
110 //==================================================================
111 //= OOutputStreamWrapper
112 //==================================================================
113 typedef ::cppu::WeakImplHelper1<stario::XOutputStream> OutputStreamWrapper_Base;
114 	// needed for some compilers
115 class UNOTOOLS_DLLPUBLIC OOutputStreamWrapper : public OutputStreamWrapper_Base
116 {
117 protected:
118     // TODO: thread safety!
119 	SvStream&		rStream;
120 
121 public:
122 	OOutputStreamWrapper(SvStream& _rStream) :rStream(_rStream) { }
123 
124 // UNO Anbindung
125 	DECLARE_UNO3_AGG_DEFAULTS(OOutputStreamWrapper, OutputStreamWrapper_Base);
126 
127 // stario::XOutputStream
128 	virtual void SAL_CALL writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException);
129 	virtual void SAL_CALL flush() throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException);
130 	virtual void SAL_CALL closeOutput() throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException);
131 
132 protected:
133 	/// throws an exception according to the error flag of m_pSvStream
134 	void checkError() const;
135 };
136 
137 //==================================================================
138 //= OSeekableOutputStreamWrapper
139 //==================================================================
140 typedef ::cppu::ImplHelper1	<	::com::sun::star::io::XSeekable
141 							>	OSeekableOutputStreamWrapper_Base;
142 /** helper class for wrapping an SvStream into an <type scope="com.sun.star.io">XOutputStream</type>
143 	which is seekable (i.e. supports the <type scope="com.sun.star.io">XSeekable</type> interface).
144 */
145 class UNOTOOLS_DLLPUBLIC OSeekableOutputStreamWrapper
146 				:public OOutputStreamWrapper
147 				,public OSeekableOutputStreamWrapper_Base
148 {
149 public:
150 	OSeekableOutputStreamWrapper(SvStream& _rStream);
151 
152 	// disambiguate XInterface
153     virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type& _rType ) throw (::com::sun::star::uno::RuntimeException);
154     virtual void SAL_CALL acquire(  ) throw ();
155     virtual void SAL_CALL release(  ) throw ();
156 
157 	// XSeekable
158     virtual void SAL_CALL seek( sal_Int64 _nLocation ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
159     virtual sal_Int64 SAL_CALL getPosition(  ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
160     virtual sal_Int64 SAL_CALL getLength(  ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
161 };
162 
163 class UNOTOOLS_DLLPUBLIC OStreamWrapper : public ::cppu::ImplInheritanceHelper3 < OSeekableInputStreamWrapper, com::sun::star::io::XStream, com::sun::star::io::XOutputStream, com::sun::star::io::XTruncate >
164 {
165 public:
166     OStreamWrapper(SvStream& _rStream);
167 
168 // stario::XStream
169     virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getInputStream(  ) throw (::com::sun::star::uno::RuntimeException);
170     virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > SAL_CALL getOutputStream(  ) throw (::com::sun::star::uno::RuntimeException);
171 
172 // stario::XOutputStream
173     virtual void SAL_CALL writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException);
174 	virtual void SAL_CALL flush() throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException);
175 	virtual void SAL_CALL closeOutput() throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException);
176     virtual void SAL_CALL truncate() throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
177 };
178 
179 }
180 // namespace utl
181 
182 #endif // _UTL_STREAM_WRAPPER_HXX_
183 
184