xref: /AOO41X/main/comphelper/inc/comphelper/seqstream.hxx (revision 9877b273795ec465a3ce9c15d738eb34c0455705)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _COMPHELPER_SEQSTREAM_HXX
24 #define _COMPHELPER_SEQSTREAM_HXX
25 
26 #include <com/sun/star/uno/Sequence.hxx>
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <com/sun/star/io/XInputStream.hpp>
29 #include <com/sun/star/io/XOutputStream.hpp>
30 #include <com/sun/star/io/XSeekable.hpp>
31 #include <osl/mutex.hxx>
32 #include <cppuhelper/implbase1.hxx>
33 #include <cppuhelper/implbase2.hxx>
34 #include "comphelper/comphelperdllapi.h"
35 
36 namespace comphelper
37 {
38 
39     typedef ::com::sun::star::uno::Sequence<sal_Int8> ByteSequence;
40 
41 //==================================================================
42 // SequenceInputStream
43 // stream for reading data from a sequence of bytes
44 //==================================================================
45 
46 
47 class COMPHELPER_DLLPUBLIC SequenceInputStream
48 : public ::cppu::WeakImplHelper2< ::com::sun::star::io::XInputStream, ::com::sun::star::io::XSeekable >
49 {
50     ::osl::Mutex    m_aMutex;
51     ByteSequence    m_aData;
52     sal_Int32       m_nPos;
53 
54 public:
55     SequenceInputStream(const ByteSequence& rData);
56 
57 // com::sun::star::io::XInputStream
58     virtual sal_Int32 SAL_CALL readBytes( ::com::sun::star::uno::Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead )
59         throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException,
60               ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
61 
62     virtual sal_Int32 SAL_CALL readSomeBytes( ::com::sun::star::uno::Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead )
63         throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException,
64               ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
65 
66     virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
67         throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException,
68               ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
69 
70     virtual sal_Int32 SAL_CALL available(  )
71         throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
72 
73     virtual void SAL_CALL closeInput(  )
74         throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
75 
76     virtual void SAL_CALL seek( sal_Int64 location ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
77     virtual sal_Int64 SAL_CALL getPosition(  ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
78     virtual sal_Int64 SAL_CALL getLength(  ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
79 
80 private:
81     inline sal_Int32 avail();
82 };
83 typedef ::cppu::WeakImplHelper1< ::com::sun::star::io::XOutputStream > OSequenceOutputStream_Base;
84 
85 class OSequenceOutputStream : public OSequenceOutputStream_Base
86 {
87 protected:
88     ::com::sun::star::uno::Sequence< sal_Int8 >&    m_rSequence;
89     double                                          m_nResizeFactor;
90     sal_Int32                                       m_nMinimumResize;
91     sal_Int32                                       m_nMaximumResize;
92     sal_Int32                                       m_nSize;
93         // the size of the virtual stream. This is not the size of the sequence, but the number of bytes written
94         // into the stream at a given moment.
95 
96     sal_Bool                                        m_bConnected;
97         // closeOutput has been called ?
98 
99     ::osl::Mutex                                    m_aMutex;
100 
101 protected:
~OSequenceOutputStream()102     ~OSequenceOutputStream() { if (m_bConnected) closeOutput(); }
103 
104 public:
105     /** constructs the object. Everything written into the stream through the XOutputStream methods will be forwarded
106         to the sequence, reallocating it if neccessary. Writing will start at offset 0 within the sequence.
107         @param      _rSeq               a reference to the sequence which will be used for output.
108                                         The caller is responsible for taking care of the lifetime of the stream
109                                         object and the sequence. If you're in doubt about this, use <code>closeOutput</code>
110                                         before destroying the sequence
111         @param      _nResizeFactor      the factor which is used for resizing the sequence when neccessary. In every
112                                         resize step, the new sequence size will be calculated by multiplying the current
113                                         size with this factor, rounded off to the next multiple of 4.
114         @param      _nMinimumResize     the minmal number of bytes which is additionally allocated on resizing
115         @param      _nMaximumResize     as the growth of the stream size is exponential, you may want to specify a
116                                         maxmimum amount of memory which the sequence will grow by. If -1 is used,
117                                         no limit is applied
118         @see        closeOutput
119     */
120     OSequenceOutputStream(
121         ::com::sun::star::uno::Sequence< sal_Int8 >& _rSeq,
122         double _nResizeFactor = 1.3,
123         sal_Int32 _nMinimumResize = 128,
124         sal_Int32 _nMaximumResize = -1
125         );
126 
127     /// same as XOutputStream::writeBytes (as expected :)
128     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);
129     /// this is a dummy in this implementation, no buffering is used
130     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);
131     /** closes the output stream. In the case of this class, this means that the sequence used for writing is
132         resized to the really used size and not used any further, every subsequent call to one of the XOutputStream
133         methods will throw a <code>NotConnectedException</code>.
134     */
135     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);
136 };
137 
138 } // namespace comphelper
139 
140 #endif //_COMPHELPER_SEQSTREAM_HXX
141 
142 
143