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