1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_UNOTOOLS_STREAMWRAP_HXX
21 #define INCLUDED_UNOTOOLS_STREAMWRAP_HXX
23 #include <unotools/unotoolsdllapi.h>
24 #include <osl/mutex.hxx>
25 #include <com/sun/star/io/XOutputStream.hpp>
26 #include <com/sun/star/io/XInputStream.hpp>
27 #include <com/sun/star/io/XSeekable.hpp>
28 #include <com/sun/star/io/XTruncate.hpp>
29 #include <com/sun/star/io/XStream.hpp>
30 #include <cppuhelper/implbase.hxx>
31 #include <cppuhelper/implbase1.hxx>
39 // workaround for incremental linking bugs in MSVC2015
40 class SAL_DLLPUBLIC_TEMPLATE OInputStreamWrapper_Base
: public cppu::WeakImplHelper
< css::io::XInputStream
> {};
42 /// helper class for wrapping an SvStream into an com.sun.star.io::XInputStream
43 class UNOTOOLS_DLLPUBLIC OInputStreamWrapper
: public OInputStreamWrapper_Base
46 ::osl::Mutex m_aMutex
;
47 SvStream
* m_pSvStream
;
48 bool m_bSvStreamOwner
: 1;
50 { m_pSvStream
= nullptr; m_bSvStreamOwner
= false; }
51 void SetStream(SvStream
* _pStream
, bool bOwner
)
52 { m_pSvStream
= _pStream
; m_bSvStreamOwner
= bOwner
; }
55 OInputStreamWrapper(SvStream
& _rStream
);
56 OInputStreamWrapper(SvStream
* pStream
, bool bOwner
=false);
57 OInputStreamWrapper(std::unique_ptr
<SvStream
> pStream
);
58 virtual ~OInputStreamWrapper() override
;
60 // css::io::XInputStream
61 virtual sal_Int32 SAL_CALL
readBytes(css::uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
) override
;
62 virtual sal_Int32 SAL_CALL
readSomeBytes(css::uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
) override
;
63 virtual void SAL_CALL
skipBytes(sal_Int32 nBytesToSkip
) override
;
64 virtual sal_Int32 SAL_CALL
available() override
;
65 virtual void SAL_CALL
closeInput() override
;
68 /// throws a NotConnectedException if the object is not connected anymore
69 void checkConnected() const;
70 /// throws an exception according to the error flag of m_pSvStream
71 void checkError() const;
74 //= OSeekableInputStreamWrapper
76 /** helper class for wrapping an SvStream into an com.sun.star.io::XInputStream
77 which is seekable (i.e. supports the com.sun.star.io::XSeekable interface).
79 class UNOTOOLS_DLLPUBLIC OSeekableInputStreamWrapper
80 : public cppu::ImplInheritanceHelper
< OInputStreamWrapper
, css::io::XSeekable
>
83 OSeekableInputStreamWrapper() {}
84 ~OSeekableInputStreamWrapper() override
;
87 OSeekableInputStreamWrapper(SvStream
& _rStream
);
88 OSeekableInputStreamWrapper(SvStream
* _pStream
, bool _bOwner
= false);
91 virtual void SAL_CALL
seek( sal_Int64 _nLocation
) override
;
92 virtual sal_Int64 SAL_CALL
getPosition( ) override
;
93 virtual sal_Int64 SAL_CALL
getLength( ) override
;
96 //= OOutputStreamWrapper
98 class OOutputStreamWrapper
: public cppu::WeakImplHelper
<css::io::XOutputStream
>
101 UNOTOOLS_DLLPUBLIC
OOutputStreamWrapper(SvStream
& _rStream
);
104 virtual ~OOutputStreamWrapper() override
;
106 // css::io::XOutputStream
107 virtual void SAL_CALL
writeBytes(const css::uno::Sequence
< sal_Int8
>& aData
) override
;
108 virtual void SAL_CALL
flush() override
;
109 virtual void SAL_CALL
closeOutput() override
;
111 /// throws an exception according to the error flag of m_pSvStream
112 void checkError() const;
114 // TODO: thread safety!
118 //= OSeekableOutputStreamWrapper
120 typedef ::cppu::ImplHelper1
< css::io::XSeekable
121 > OSeekableOutputStreamWrapper_Base
;
122 /** helper class for wrapping an SvStream into an com.sun.star.io::XOutputStream
123 which is seekable (i.e. supports the com.sun.star.io::XSeekable interface).
125 class UNOTOOLS_DLLPUBLIC OSeekableOutputStreamWrapper
126 :public OOutputStreamWrapper
127 ,public OSeekableOutputStreamWrapper_Base
130 OSeekableOutputStreamWrapper(SvStream
& _rStream
);
133 virtual ~OSeekableOutputStreamWrapper() override
;
135 // disambiguate XInterface
136 virtual css::uno::Any SAL_CALL
queryInterface( const css::uno::Type
& _rType
) override
;
137 virtual void SAL_CALL
acquire( ) throw () override
;
138 virtual void SAL_CALL
release( ) throw () override
;
141 virtual void SAL_CALL
seek( sal_Int64 _nLocation
) override
;
142 virtual sal_Int64 SAL_CALL
getPosition( ) override
;
143 virtual sal_Int64 SAL_CALL
getLength( ) override
;
146 class UNOTOOLS_DLLPUBLIC OStreamWrapper
147 : public cppu::ImplInheritanceHelper
<OSeekableInputStreamWrapper
,
149 css::io::XOutputStream
,
153 ~OStreamWrapper() override
;
156 OStreamWrapper(SvStream
& _rStream
);
157 OStreamWrapper(std::unique_ptr
<SvStream
> _rStream
);
160 virtual css::uno::Reference
< css::io::XInputStream
> SAL_CALL
getInputStream( ) override
;
161 virtual css::uno::Reference
< css::io::XOutputStream
> SAL_CALL
getOutputStream( ) override
;
163 // css::io::XOutputStream
164 virtual void SAL_CALL
writeBytes(const css::uno::Sequence
< sal_Int8
>& aData
) override
;
165 virtual void SAL_CALL
flush() override
;
166 virtual void SAL_CALL
closeOutput() override
;
167 virtual void SAL_CALL
truncate() override
;
173 #endif // INCLUDED_UNOTOOLS_STREAMWRAP_HXX
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */