Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 : #ifndef INCLUDED_COMPHELPER_SEQSTREAM_HXX
20 : #define INCLUDED_COMPHELPER_SEQSTREAM_HXX
21 :
22 : #include <com/sun/star/uno/Sequence.hxx>
23 : #include <com/sun/star/uno/Reference.hxx>
24 : #include <com/sun/star/io/XInputStream.hpp>
25 : #include <com/sun/star/io/XOutputStream.hpp>
26 : #include <com/sun/star/io/XSeekable.hpp>
27 : #include <osl/mutex.hxx>
28 : #include <cppuhelper/implbase1.hxx>
29 : #include <cppuhelper/implbase2.hxx>
30 : #include <comphelper/comphelperdllapi.h>
31 :
32 : namespace comphelper
33 : {
34 :
35 :
36 : // SequenceInputStream
37 : // stream for reading data from a sequence of bytes
38 :
39 :
40 :
41 0 : class COMPHELPER_DLLPUBLIC SequenceInputStream
42 : : public ::cppu::WeakImplHelper2< ::com::sun::star::io::XInputStream, ::com::sun::star::io::XSeekable >
43 : {
44 : ::osl::Mutex m_aMutex;
45 : css::uno::Sequence<sal_Int8> m_aData;
46 : sal_Int32 m_nPos;
47 :
48 : public:
49 : SequenceInputStream(css::uno::Sequence<sal_Int8> const & rData);
50 :
51 : // com::sun::star::io::XInputStream
52 : virtual sal_Int32 SAL_CALL readBytes( ::com::sun::star::uno::Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead )
53 : throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException,
54 : ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
55 :
56 : virtual sal_Int32 SAL_CALL readSomeBytes( ::com::sun::star::uno::Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead )
57 : throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException,
58 : ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
59 :
60 : virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
61 : throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException,
62 : ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
63 :
64 : virtual sal_Int32 SAL_CALL available( )
65 : throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
66 :
67 : virtual void SAL_CALL closeInput( )
68 : throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
69 :
70 : virtual void SAL_CALL seek( sal_Int64 location ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
71 : virtual sal_Int64 SAL_CALL getPosition( ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
72 : virtual sal_Int64 SAL_CALL getLength( ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
73 :
74 : private:
75 : inline sal_Int32 avail();
76 : };
77 : typedef ::cppu::WeakImplHelper1< ::com::sun::star::io::XOutputStream > OSequenceOutputStream_Base;
78 :
79 : class OSequenceOutputStream : public OSequenceOutputStream_Base
80 : {
81 : protected:
82 : ::com::sun::star::uno::Sequence< sal_Int8 >& m_rSequence;
83 : double m_nResizeFactor;
84 : sal_Int32 m_nMinimumResize;
85 : sal_Int32 m_nMaximumResize;
86 : sal_Int32 m_nSize;
87 : // the size of the virtual stream. This is not the size of the sequence, but the number of bytes written
88 : // into the stream at a given moment.
89 :
90 : bool m_bConnected;
91 : // closeOutput has been called ?
92 :
93 : ::osl::Mutex m_aMutex;
94 :
95 : protected:
96 0 : virtual ~OSequenceOutputStream() { if (m_bConnected) closeOutput(); }
97 :
98 : public:
99 : /** constructs the object. Everything written into the stream through the XOutputStream methods will be forwarded
100 : to the sequence, reallocating it if necessary. Writing will start at offset 0 within the sequence.
101 : @param _rSeq a reference to the sequence which will be used for output.
102 : The caller is responsible for taking care of the lifetime of the stream
103 : object and the sequence. If you're in doubt about this, use <code>closeOutput</code>
104 : before destroying the sequence
105 : @param _nResizeFactor the factor which is used for resizing the sequence when necessary. In every
106 : resize step, the new sequence size will be calculated by multiplying the current
107 : size with this factor, rounded off to the next multiple of 4.
108 : @param _nMinimumResize the minmal number of bytes which is additionally allocated on resizing
109 : @param _nMaximumResize as the growth of the stream size is exponential, you may want to specify a
110 : maxmimum amount of memory which the sequence will grow by. If -1 is used,
111 : no limit is applied
112 : @see closeOutput
113 : */
114 : OSequenceOutputStream(
115 : ::com::sun::star::uno::Sequence< sal_Int8 >& _rSeq,
116 : double _nResizeFactor = 1.3,
117 : sal_Int32 _nMinimumResize = 128,
118 : sal_Int32 _nMaximumResize = -1
119 : );
120 :
121 : /// same as XOutputStream::writeBytes (as expected :)
122 : virtual void SAL_CALL writeBytes( const ::com::sun::star::uno::Sequence< sal_Int8 >& aData ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
123 : /// this is a dummy in this implementation, no buffering is used
124 : virtual void SAL_CALL flush( ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
125 : /** closes the output stream. In the case of this class, this means that the sequence used for writing is
126 : resized to the really used size and not used any further, every subsequent call to one of the XOutputStream
127 : methods will throw a <code>NotConnectedException</code>.
128 : */
129 : virtual void SAL_CALL closeOutput( ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
130 : };
131 :
132 : } // namespace comphelper
133 :
134 : #endif // INCLUDED_COMPHELPER_SEQSTREAM_HXX
135 :
136 :
137 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|