Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / unotools / streamwrap.hxx
blob479f73e514e8f1f380845f83bf07ffda633eb46a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
33 #include <memory>
34 #include <mutex>
36 class SvStream;
38 namespace utl
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
47 protected:
48 std::mutex m_aMutex;
49 SvStream* m_pSvStream;
50 bool m_bSvStreamOwner : 1;
51 OInputStreamWrapper()
52 { m_pSvStream = nullptr; m_bSvStreamOwner = false; }
53 void SetStream(SvStream* _pStream, bool bOwner )
54 { m_pSvStream = _pStream; m_bSvStreamOwner = bOwner; }
56 public:
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 // utl::ByteReader
70 virtual sal_Int32 readSomeBytes( sal_Int8* aData, sal_Int32 nMaxBytesToRead ) final override;
72 protected:
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 >
87 protected:
88 OSeekableInputStreamWrapper() {}
89 ~OSeekableInputStreamWrapper() override;
91 public:
92 OSeekableInputStreamWrapper(SvStream& _rStream);
93 OSeekableInputStreamWrapper(SvStream* _pStream, bool _bOwner = false);
95 // XSeekable
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>
105 public:
106 UNOTOOLS_DLLPUBLIC OOutputStreamWrapper(SvStream& _rStream);
108 protected:
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!
120 SvStream& rStream;
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
134 public:
135 OSeekableOutputStreamWrapper(SvStream& _rStream);
137 private:
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(); }
147 // XSeekable
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,
155 css::io::XStream,
156 css::io::XOutputStream,
157 css::io::XTruncate>
159 ~OStreamWrapper() override;
161 public:
162 OStreamWrapper(SvStream& _rStream);
163 OStreamWrapper(std::unique_ptr<SvStream> _rStream);
164 OStreamWrapper(SvStream* _pStream, bool _bOwner = false);
166 // css::io::XStream
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;
178 // namespace utl
180 #endif // INCLUDED_UNOTOOLS_STREAMWRAP_HXX
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */