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 <com/sun/star/io/XOutputStream.hpp>
25 #include <com/sun/star/io/XInputStream.hpp>
26 #include <com/sun/star/io/XSeekable.hpp>
27 #include <com/sun/star/io/XTruncate.hpp>
28 #include <com/sun/star/io/XStream.hpp>
29 #include <com/sun/star/lang/XUnoTunnel.hpp>
30 #include <comphelper/bytereader.hxx>
31 #include <cppuhelper/implbase.hxx>
32 #include <cppuhelper/implbase1.hxx>
41 // workaround for incremental linking bugs in MSVC2015
42 class SAL_DLLPUBLIC_TEMPLATE OInputStreamWrapper_Base
: public cppu::WeakImplHelper
< css::io::XInputStream
> {};
44 /// helper class for wrapping an SvStream into a com.sun.star.io::XInputStream
45 class UNOTOOLS_DLLPUBLIC OInputStreamWrapper
: public OInputStreamWrapper_Base
, public comphelper::ByteReader
49 SvStream
* m_pSvStream
;
50 bool m_bSvStreamOwner
: 1;
52 { m_pSvStream
= nullptr; m_bSvStreamOwner
= false; }
53 void SetStream(SvStream
* _pStream
, bool bOwner
)
54 { m_pSvStream
= _pStream
; m_bSvStreamOwner
= bOwner
; }
57 OInputStreamWrapper(SvStream
& _rStream
);
58 OInputStreamWrapper(SvStream
* pStream
, bool bOwner
=false);
59 OInputStreamWrapper(std::unique_ptr
<SvStream
> pStream
);
60 virtual ~OInputStreamWrapper() override
;
62 // css::io::XInputStream
63 virtual sal_Int32 SAL_CALL
readBytes(css::uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
) override
;
64 virtual sal_Int32 SAL_CALL
readSomeBytes(css::uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
) override
;
65 virtual void SAL_CALL
skipBytes(sal_Int32 nBytesToSkip
) override
;
66 virtual sal_Int32 SAL_CALL
available() override
;
67 virtual void SAL_CALL
closeInput() override
;
70 virtual sal_Int32
readSomeBytes( sal_Int8
* aData
, sal_Int32 nMaxBytesToRead
) final override
;
73 /// throws a NotConnectedException if the object is not connected anymore
74 void checkConnected() const;
75 /// throws an exception according to the error flag of m_pSvStream
76 void checkError() const;
79 //= OSeekableInputStreamWrapper
81 /** helper class for wrapping an SvStream into a com.sun.star.io::XInputStream
82 which is seekable (i.e. supports the com.sun.star.io::XSeekable interface).
84 class UNOTOOLS_DLLPUBLIC OSeekableInputStreamWrapper
85 : public cppu::ImplInheritanceHelper
< OInputStreamWrapper
, css::io::XSeekable
>
88 OSeekableInputStreamWrapper() {}
89 ~OSeekableInputStreamWrapper() override
;
92 OSeekableInputStreamWrapper(SvStream
& _rStream
);
93 OSeekableInputStreamWrapper(SvStream
* _pStream
, bool _bOwner
= false);
96 virtual void SAL_CALL
seek( sal_Int64 _nLocation
) override
;
97 virtual sal_Int64 SAL_CALL
getPosition( ) override
;
98 virtual sal_Int64 SAL_CALL
getLength( ) override
;
101 //= OOutputStreamWrapper
103 class OOutputStreamWrapper
: public cppu::WeakImplHelper
<css::io::XOutputStream
>
106 UNOTOOLS_DLLPUBLIC
OOutputStreamWrapper(SvStream
& _rStream
);
109 virtual ~OOutputStreamWrapper() override
;
111 // css::io::XOutputStream
112 virtual void SAL_CALL
writeBytes(const css::uno::Sequence
< sal_Int8
>& aData
) override
;
113 virtual void SAL_CALL
flush() override
;
114 virtual void SAL_CALL
closeOutput() override
;
116 /// throws an exception according to the error flag of m_pSvStream
117 void checkError() const;
119 // TODO: thread safety!
123 //= OSeekableOutputStreamWrapper
125 typedef ::cppu::ImplHelper1
< css::io::XSeekable
126 > OSeekableOutputStreamWrapper_Base
;
127 /** helper class for wrapping an SvStream into a com.sun.star.io::XOutputStream
128 which is seekable (i.e. supports the com.sun.star.io::XSeekable interface).
130 class UNOTOOLS_DLLPUBLIC OSeekableOutputStreamWrapper final
131 :public OOutputStreamWrapper
132 ,public OSeekableOutputStreamWrapper_Base
135 OSeekableOutputStreamWrapper(SvStream
& _rStream
);
138 virtual ~OSeekableOutputStreamWrapper() override
;
140 // disambiguate XInterface
141 virtual css::uno::Any SAL_CALL
queryInterface( const css::uno::Type
& _rType
) override
;
142 virtual void SAL_CALL
acquire( ) noexcept override
143 { OOutputStreamWrapper::acquire(); }
144 virtual void SAL_CALL
release( ) noexcept override
145 { OOutputStreamWrapper::release(); }
148 virtual void SAL_CALL
seek( sal_Int64 _nLocation
) override
;
149 virtual sal_Int64 SAL_CALL
getPosition( ) override
;
150 virtual sal_Int64 SAL_CALL
getLength( ) override
;
153 class UNOTOOLS_DLLPUBLIC OStreamWrapper final
154 : public cppu::ImplInheritanceHelper
<OSeekableInputStreamWrapper
,
156 css::io::XOutputStream
,
159 ~OStreamWrapper() override
;
162 OStreamWrapper(SvStream
& _rStream
);
163 OStreamWrapper(std::unique_ptr
<SvStream
> _rStream
);
164 OStreamWrapper(SvStream
* _pStream
, bool _bOwner
= false);
167 virtual css::uno::Reference
< css::io::XInputStream
> SAL_CALL
getInputStream( ) override
;
168 virtual css::uno::Reference
< css::io::XOutputStream
> SAL_CALL
getOutputStream( ) override
;
170 // css::io::XOutputStream
171 virtual void SAL_CALL
writeBytes(const css::uno::Sequence
< sal_Int8
>& aData
) override
;
172 virtual void SAL_CALL
flush() override
;
173 virtual void SAL_CALL
closeOutput() override
;
174 virtual void SAL_CALL
truncate() override
;
180 #endif // INCLUDED_UNOTOOLS_STREAMWRAP_HXX
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */