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
, css::lang::XUnoTunnel
> {};
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
;
69 // css::lang::XUnoTunnel
70 virtual sal_Int64 SAL_CALL
getSomething( const css::uno::Sequence
< sal_Int8
>& _rIdentifier
) override
;
73 virtual sal_Int32
readSomeBytes( sal_Int8
* aData
, sal_Int32 nMaxBytesToRead
) final override
;
76 /// throws a NotConnectedException if the object is not connected anymore
77 void checkConnected() const;
78 /// throws an exception according to the error flag of m_pSvStream
79 void checkError() const;
82 //= OSeekableInputStreamWrapper
84 /** helper class for wrapping an SvStream into a com.sun.star.io::XInputStream
85 which is seekable (i.e. supports the com.sun.star.io::XSeekable interface).
87 class UNOTOOLS_DLLPUBLIC OSeekableInputStreamWrapper
88 : public cppu::ImplInheritanceHelper
< OInputStreamWrapper
, css::io::XSeekable
>
91 OSeekableInputStreamWrapper() {}
92 ~OSeekableInputStreamWrapper() override
;
95 OSeekableInputStreamWrapper(SvStream
& _rStream
);
96 OSeekableInputStreamWrapper(SvStream
* _pStream
, bool _bOwner
= false);
99 virtual void SAL_CALL
seek( sal_Int64 _nLocation
) override
;
100 virtual sal_Int64 SAL_CALL
getPosition( ) override
;
101 virtual sal_Int64 SAL_CALL
getLength( ) override
;
104 //= OOutputStreamWrapper
106 class OOutputStreamWrapper
: public cppu::WeakImplHelper
<css::io::XOutputStream
>
109 UNOTOOLS_DLLPUBLIC
OOutputStreamWrapper(SvStream
& _rStream
);
112 virtual ~OOutputStreamWrapper() override
;
114 // css::io::XOutputStream
115 virtual void SAL_CALL
writeBytes(const css::uno::Sequence
< sal_Int8
>& aData
) override
;
116 virtual void SAL_CALL
flush() override
;
117 virtual void SAL_CALL
closeOutput() override
;
119 /// throws an exception according to the error flag of m_pSvStream
120 void checkError() const;
122 // TODO: thread safety!
126 //= OSeekableOutputStreamWrapper
128 typedef ::cppu::ImplHelper1
< css::io::XSeekable
129 > OSeekableOutputStreamWrapper_Base
;
130 /** helper class for wrapping an SvStream into a com.sun.star.io::XOutputStream
131 which is seekable (i.e. supports the com.sun.star.io::XSeekable interface).
133 class UNOTOOLS_DLLPUBLIC OSeekableOutputStreamWrapper final
134 :public OOutputStreamWrapper
135 ,public OSeekableOutputStreamWrapper_Base
138 OSeekableOutputStreamWrapper(SvStream
& _rStream
);
141 virtual ~OSeekableOutputStreamWrapper() override
;
143 // disambiguate XInterface
144 virtual css::uno::Any SAL_CALL
queryInterface( const css::uno::Type
& _rType
) override
;
145 virtual void SAL_CALL
acquire( ) noexcept override
146 { OOutputStreamWrapper::acquire(); }
147 virtual void SAL_CALL
release( ) noexcept override
148 { OOutputStreamWrapper::release(); }
151 virtual void SAL_CALL
seek( sal_Int64 _nLocation
) override
;
152 virtual sal_Int64 SAL_CALL
getPosition( ) override
;
153 virtual sal_Int64 SAL_CALL
getLength( ) override
;
156 class UNOTOOLS_DLLPUBLIC OStreamWrapper final
157 : public cppu::ImplInheritanceHelper
<OSeekableInputStreamWrapper
,
159 css::io::XOutputStream
,
162 ~OStreamWrapper() override
;
165 OStreamWrapper(SvStream
& _rStream
);
166 OStreamWrapper(std::unique_ptr
<SvStream
> _rStream
);
167 OStreamWrapper(SvStream
* _pStream
, bool _bOwner
= false);
170 virtual css::uno::Reference
< css::io::XInputStream
> SAL_CALL
getInputStream( ) override
;
171 virtual css::uno::Reference
< css::io::XOutputStream
> SAL_CALL
getOutputStream( ) override
;
173 // css::io::XOutputStream
174 virtual void SAL_CALL
writeBytes(const css::uno::Sequence
< sal_Int8
>& aData
) override
;
175 virtual void SAL_CALL
flush() override
;
176 virtual void SAL_CALL
closeOutput() override
;
177 virtual void SAL_CALL
truncate() override
;
183 #endif // INCLUDED_UNOTOOLS_STREAMWRAP_HXX
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */