1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: seqstream.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef _COMPHELPER_SEQSTREAM_HXX
31 #define _COMPHELPER_SEQSTREAM_HXX
33 #include <com/sun/star/uno/Sequence.hxx>
34 #include <com/sun/star/uno/Reference.hxx>
35 #include <com/sun/star/io/XInputStream.hpp>
36 #include <com/sun/star/io/XOutputStream.hpp>
37 #include <com/sun/star/io/XSeekable.hpp>
38 #include <osl/mutex.hxx>
39 #include <cppuhelper/implbase1.hxx>
40 #include <cppuhelper/implbase2.hxx>
41 #include "comphelper/comphelperdllapi.h"
46 typedef ::com::sun::star::uno::Sequence
<sal_Int8
> ByteSequence
;
48 //==================================================================
49 // SequenceInputStream
50 // stream for reading data from a sequence of bytes
51 //==================================================================
54 class COMPHELPER_DLLPUBLIC SequenceInputStream
55 : public ::cppu::WeakImplHelper2
< ::com::sun::star::io::XInputStream
, ::com::sun::star::io::XSeekable
>
57 ::osl::Mutex m_aMutex
;
62 SequenceInputStream(const ByteSequence
& rData
);
64 // com::sun::star::io::XInputStream
65 virtual sal_Int32 SAL_CALL
readBytes( ::com::sun::star::uno::Sequence
<sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
66 throw(::com::sun::star::io::NotConnectedException
, ::com::sun::star::io::BufferSizeExceededException
,
67 ::com::sun::star::io::IOException
, ::com::sun::star::uno::RuntimeException
);
69 virtual sal_Int32 SAL_CALL
readSomeBytes( ::com::sun::star::uno::Sequence
<sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
70 throw(::com::sun::star::io::NotConnectedException
, ::com::sun::star::io::BufferSizeExceededException
,
71 ::com::sun::star::io::IOException
, ::com::sun::star::uno::RuntimeException
);
73 virtual void SAL_CALL
skipBytes( sal_Int32 nBytesToSkip
)
74 throw(::com::sun::star::io::NotConnectedException
, ::com::sun::star::io::BufferSizeExceededException
,
75 ::com::sun::star::io::IOException
, ::com::sun::star::uno::RuntimeException
);
77 virtual sal_Int32 SAL_CALL
available( )
78 throw(::com::sun::star::io::NotConnectedException
, ::com::sun::star::io::IOException
, ::com::sun::star::uno::RuntimeException
);
80 virtual void SAL_CALL
closeInput( )
81 throw(::com::sun::star::io::NotConnectedException
, ::com::sun::star::io::IOException
, ::com::sun::star::uno::RuntimeException
);
83 virtual void SAL_CALL
seek( sal_Int64 location
) throw (::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::io::IOException
, ::com::sun::star::uno::RuntimeException
);
84 virtual sal_Int64 SAL_CALL
getPosition( ) throw (::com::sun::star::io::IOException
, ::com::sun::star::uno::RuntimeException
);
85 virtual sal_Int64 SAL_CALL
getLength( ) throw (::com::sun::star::io::IOException
, ::com::sun::star::uno::RuntimeException
);
88 inline sal_Int32
avail();
90 typedef ::cppu::WeakImplHelper1
< ::com::sun::star::io::XOutputStream
> OSequenceOutputStream_Base
;
92 class OSequenceOutputStream
: public OSequenceOutputStream_Base
95 ::com::sun::star::uno::Sequence
< sal_Int8
>& m_rSequence
;
96 double m_nResizeFactor
;
97 sal_Int32 m_nMinimumResize
;
98 sal_Int32 m_nMaximumResize
;
100 // the size of the virtual stream. This is not the size of the sequence, but the number of bytes written
101 // into the stream at a given moment.
103 sal_Bool m_bConnected
;
104 // closeOutput has been called ?
106 ::osl::Mutex m_aMutex
;
109 ~OSequenceOutputStream() { if (m_bConnected
) closeOutput(); }
112 /** constructs the object. Everything written into the stream through the XOutputStream methods will be forwarded
113 to the sequence, reallocating it if neccessary. Writing will start at offset 0 within the sequence.
114 @param _rSeq a reference to the sequence which will be used for output.
115 The caller is responsible for taking care of the lifetime of the stream
116 object and the sequence. If you're in doubt about this, use <code>closeOutput</code>
117 before destroying the sequence
118 @param _nResizeFactor the factor which is used for resizing the sequence when neccessary. In every
119 resize step, the new sequence size will be calculated by multiplying the current
120 size with this factor, rounded off to the next multiple of 4.
121 @param _nMinimumResize the minmal number of bytes which is additionally allocated on resizing
122 @param _nMaximumResize as the growth of the stream size is exponential, you may want to specify a
123 maxmimum amount of memory which the sequence will grow by. If -1 is used,
127 OSequenceOutputStream(
128 ::com::sun::star::uno::Sequence
< sal_Int8
>& _rSeq
,
129 double _nResizeFactor
= 1.3,
130 sal_Int32 _nMinimumResize
= 128,
131 sal_Int32 _nMaximumResize
= -1
134 /// same as XOutputStream::writeBytes (as expected :)
135 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
);
136 /// this is a dummy in this implementation, no buffering is used
137 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
);
138 /** closes the output stream. In the case of this class, this means that the sequence used for writing is
139 resized to the really used size and not used any further, every subsequent call to one of the XOutputStream
140 methods will throw a <code>NotConnectedException</code>.
142 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
);
145 } // namespace comphelper
147 #endif //_COMPHELPER_SEQSTREAM_HXX