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 60 virtual sal_Int32 SAL_CALL readSomeBytes( ::com::sun::star::uno::Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead ); 61 62 virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ); 63 64 virtual sal_Int32 SAL_CALL available( ); 65 66 virtual void SAL_CALL closeInput( ); 67 68 virtual void SAL_CALL seek( sal_Int64 location ); 69 virtual sal_Int64 SAL_CALL getPosition( ); 70 virtual sal_Int64 SAL_CALL getLength( ); 71 72 private: 73 inline sal_Int32 avail(); 74 }; 75 typedef ::cppu::WeakImplHelper1< ::com::sun::star::io::XOutputStream > OSequenceOutputStream_Base; 76 77 class OSequenceOutputStream : public OSequenceOutputStream_Base 78 { 79 protected: 80 ::com::sun::star::uno::Sequence< sal_Int8 >& m_rSequence; 81 double m_nResizeFactor; 82 sal_Int32 m_nMinimumResize; 83 sal_Int32 m_nMaximumResize; 84 sal_Int32 m_nSize; 85 // the size of the virtual stream. This is not the size of the sequence, but the number of bytes written 86 // into the stream at a given moment. 87 88 sal_Bool m_bConnected; 89 // closeOutput has been called ? 90 91 ::osl::Mutex m_aMutex; 92 93 protected: ~OSequenceOutputStream()94 ~OSequenceOutputStream() { if (m_bConnected) closeOutput(); } 95 96 public: 97 /** constructs the object. Everything written into the stream through the XOutputStream methods will be forwarded 98 to the sequence, reallocating it if necessary. Writing will start at offset 0 within the sequence. 99 @param _rSeq a reference to the sequence which will be used for output. 100 The caller is responsible for taking care of the lifetime of the stream 101 object and the sequence. If you're in doubt about this, use <code>closeOutput</code> 102 before destroying the sequence 103 @param _nResizeFactor the factor which is used for resizing the sequence when necessary. In every 104 resize step, the new sequence size will be calculated by multiplying the current 105 size with this factor, rounded off to the next multiple of 4. 106 @param _nMinimumResize the minimal number of bytes which is additionally allocated on resizing 107 @param _nMaximumResize as the growth of the stream size is exponential, you may want to specify a 108 maxmimum amount of memory which the sequence will grow by. If -1 is used, 109 no limit is applied 110 @see closeOutput 111 */ 112 OSequenceOutputStream( 113 ::com::sun::star::uno::Sequence< sal_Int8 >& _rSeq, 114 double _nResizeFactor = 1.3, 115 sal_Int32 _nMinimumResize = 128, 116 sal_Int32 _nMaximumResize = -1 117 ); 118 119 /// same as XOutputStream::writeBytes (as expected :) 120 virtual void SAL_CALL writeBytes( const ::com::sun::star::uno::Sequence< sal_Int8 >& aData ); 121 /// this is a dummy in this implementation, no buffering is used 122 virtual void SAL_CALL flush( ); 123 /** closes the output stream. In the case of this class, this means that the sequence used for writing is 124 resized to the really used size and not used any further, every subsequent call to one of the XOutputStream 125 methods will throw a <code>NotConnectedException</code>. 126 */ 127 virtual void SAL_CALL closeOutput( ); 128 }; 129 130 } // namespace comphelper 131 132 #endif //_COMPHELPER_SEQSTREAM_HXX 133