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 _COMPHELPER_SEQSTREAM_HXX
20 : #define _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 : typedef ::com::sun::star::uno::Sequence<sal_Int8> ByteSequence;
36 :
37 : //==================================================================
38 : // SequenceInputStream
39 : // stream for reading data from a sequence of bytes
40 : //==================================================================
41 :
42 :
43 2720 : class COMPHELPER_DLLPUBLIC SequenceInputStream
44 : : public ::cppu::WeakImplHelper2< ::com::sun::star::io::XInputStream, ::com::sun::star::io::XSeekable >
45 : {
46 : ::osl::Mutex m_aMutex;
47 : ByteSequence m_aData;
48 : sal_Int32 m_nPos;
49 :
50 : public:
51 : SequenceInputStream(const ByteSequence& rData);
52 :
53 : // com::sun::star::io::XInputStream
54 : virtual sal_Int32 SAL_CALL readBytes( ::com::sun::star::uno::Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead )
55 : throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException,
56 : ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
57 :
58 : virtual sal_Int32 SAL_CALL readSomeBytes( ::com::sun::star::uno::Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead )
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 void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
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 sal_Int32 SAL_CALL available( )
67 : throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
68 :
69 : virtual void SAL_CALL closeInput( )
70 : throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
71 :
72 : virtual void SAL_CALL seek( sal_Int64 location ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
73 : virtual sal_Int64 SAL_CALL getPosition( ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
74 : virtual sal_Int64 SAL_CALL getLength( ) throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
75 :
76 : private:
77 : inline sal_Int32 avail();
78 : };
79 : typedef ::cppu::WeakImplHelper1< ::com::sun::star::io::XOutputStream > OSequenceOutputStream_Base;
80 :
81 : class OSequenceOutputStream : public OSequenceOutputStream_Base
82 : {
83 : protected:
84 : ::com::sun::star::uno::Sequence< sal_Int8 >& m_rSequence;
85 : double m_nResizeFactor;
86 : sal_Int32 m_nMinimumResize;
87 : sal_Int32 m_nMaximumResize;
88 : sal_Int32 m_nSize;
89 : // the size of the virtual stream. This is not the size of the sequence, but the number of bytes written
90 : // into the stream at a given moment.
91 :
92 : sal_Bool m_bConnected;
93 : // closeOutput has been called ?
94 :
95 : ::osl::Mutex m_aMutex;
96 :
97 : protected:
98 0 : ~OSequenceOutputStream() { if (m_bConnected) closeOutput(); }
99 :
100 : public:
101 : /** constructs the object. Everything written into the stream through the XOutputStream methods will be forwarded
102 : to the sequence, reallocating it if neccessary. Writing will start at offset 0 within the sequence.
103 : @param _rSeq a reference to the sequence which will be used for output.
104 : The caller is responsible for taking care of the lifetime of the stream
105 : object and the sequence. If you're in doubt about this, use <code>closeOutput</code>
106 : before destroying the sequence
107 : @param _nResizeFactor the factor which is used for resizing the sequence when neccessary. In every
108 : resize step, the new sequence size will be calculated by multiplying the current
109 : size with this factor, rounded off to the next multiple of 4.
110 : @param _nMinimumResize the minmal number of bytes which is additionally allocated on resizing
111 : @param _nMaximumResize as the growth of the stream size is exponential, you may want to specify a
112 : maxmimum amount of memory which the sequence will grow by. If -1 is used,
113 : no limit is applied
114 : @see closeOutput
115 : */
116 : OSequenceOutputStream(
117 : ::com::sun::star::uno::Sequence< sal_Int8 >& _rSeq,
118 : double _nResizeFactor = 1.3,
119 : sal_Int32 _nMinimumResize = 128,
120 : sal_Int32 _nMaximumResize = -1
121 : );
122 :
123 : /// same as XOutputStream::writeBytes (as expected :)
124 : 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);
125 : /// this is a dummy in this implementation, no buffering is used
126 : 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);
127 : /** closes the output stream. In the case of this class, this means that the sequence used for writing is
128 : resized to the really used size and not used any further, every subsequent call to one of the XOutputStream
129 : methods will throw a <code>NotConnectedException</code>.
130 : */
131 : 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);
132 : };
133 :
134 : } // namespace comphelper
135 :
136 : #endif //_COMPHELPER_SEQSTREAM_HXX
137 :
138 :
139 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|