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