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