1*89dcb3daSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*89dcb3daSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*89dcb3daSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*89dcb3daSAndrew Rist * distributed with this work for additional information 6*89dcb3daSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*89dcb3daSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*89dcb3daSAndrew Rist * "License"); you may not use this file except in compliance 9*89dcb3daSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*89dcb3daSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*89dcb3daSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*89dcb3daSAndrew Rist * software distributed under the License is distributed on an 15*89dcb3daSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*89dcb3daSAndrew Rist * KIND, either express or implied. See the License for the 17*89dcb3daSAndrew Rist * specific language governing permissions and limitations 18*89dcb3daSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*89dcb3daSAndrew Rist *************************************************************/ 21*89dcb3daSAndrew Rist 22*89dcb3daSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir 25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 26cdf0e10cSrcweir #include "precompiled_xmlhelp.hxx" 27cdf0e10cSrcweir #include <rtl/memory.h> 28cdf0e10cSrcweir #include "bufferedinputstream.hxx" 29cdf0e10cSrcweir 30cdf0e10cSrcweir 31cdf0e10cSrcweir using namespace cppu; 32cdf0e10cSrcweir using namespace com::sun::star::uno; 33cdf0e10cSrcweir using namespace com::sun::star::lang; 34cdf0e10cSrcweir using namespace com::sun::star::io; 35cdf0e10cSrcweir using namespace chelp; 36cdf0e10cSrcweir 37cdf0e10cSrcweir 38cdf0e10cSrcweir Reference<XInputStream> chelp::turnToSeekable(const Reference<XInputStream>& xInputStream) 39cdf0e10cSrcweir { 40cdf0e10cSrcweir if( ! xInputStream.is() ) 41cdf0e10cSrcweir return xInputStream; 42cdf0e10cSrcweir 43cdf0e10cSrcweir Reference<XSeekable> xSeekable(xInputStream,UNO_QUERY); 44cdf0e10cSrcweir 45cdf0e10cSrcweir if( xSeekable.is() ) 46cdf0e10cSrcweir return xInputStream; 47cdf0e10cSrcweir 48cdf0e10cSrcweir return new BufferedInputStream(xInputStream); 49cdf0e10cSrcweir } 50cdf0e10cSrcweir 51cdf0e10cSrcweir 52cdf0e10cSrcweir 53cdf0e10cSrcweir BufferedInputStream::BufferedInputStream(const Reference<XInputStream>& xInputStream) 54cdf0e10cSrcweir : m_nBufferLocation(0), 55cdf0e10cSrcweir m_nBufferSize(0), 56cdf0e10cSrcweir m_pBuffer(new sal_Int8[1]) // Initialize with one to avoid gcc compiler warnings 57cdf0e10cSrcweir { 58cdf0e10cSrcweir try 59cdf0e10cSrcweir { 60cdf0e10cSrcweir sal_Int32 num; 61cdf0e10cSrcweir sal_Int8 *tmp; 62cdf0e10cSrcweir Sequence< sal_Int8 > aData(4096); 63cdf0e10cSrcweir do{ 64cdf0e10cSrcweir num = xInputStream->readBytes(aData,4096); 65cdf0e10cSrcweir if( num > 0 ) 66cdf0e10cSrcweir { 67cdf0e10cSrcweir tmp = m_pBuffer; 68cdf0e10cSrcweir m_pBuffer = new sal_Int8[m_nBufferSize+num]; 69cdf0e10cSrcweir rtl_copyMemory((void *)(m_pBuffer), 70cdf0e10cSrcweir (void *)(tmp), 71cdf0e10cSrcweir sal_uInt32(m_nBufferSize)); 72cdf0e10cSrcweir rtl_copyMemory((void *)(m_pBuffer+m_nBufferSize), 73cdf0e10cSrcweir (void *)(aData.getArray()), 74cdf0e10cSrcweir sal_uInt32(num)); 75cdf0e10cSrcweir m_nBufferSize += num; 76cdf0e10cSrcweir delete[] tmp; 77cdf0e10cSrcweir } 78cdf0e10cSrcweir } while( num == 4096 ); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir catch( const NotConnectedException&) 81cdf0e10cSrcweir { 82cdf0e10cSrcweir } 83cdf0e10cSrcweir catch( const BufferSizeExceededException&) 84cdf0e10cSrcweir { 85cdf0e10cSrcweir } 86cdf0e10cSrcweir catch( const IOException&) 87cdf0e10cSrcweir { 88cdf0e10cSrcweir } 89cdf0e10cSrcweir catch( const RuntimeException&) 90cdf0e10cSrcweir { 91cdf0e10cSrcweir } 92cdf0e10cSrcweir xInputStream->closeInput(); 93cdf0e10cSrcweir } 94cdf0e10cSrcweir 95cdf0e10cSrcweir 96cdf0e10cSrcweir BufferedInputStream::~BufferedInputStream() 97cdf0e10cSrcweir { 98cdf0e10cSrcweir delete[] m_pBuffer; 99cdf0e10cSrcweir } 100cdf0e10cSrcweir 101cdf0e10cSrcweir 102cdf0e10cSrcweir Any SAL_CALL BufferedInputStream::queryInterface( const Type& rType ) throw( RuntimeException ) 103cdf0e10cSrcweir { 104cdf0e10cSrcweir Any aRet = ::cppu::queryInterface( rType, 105cdf0e10cSrcweir SAL_STATIC_CAST( XInputStream*,this ), 106cdf0e10cSrcweir SAL_STATIC_CAST( XSeekable*,this ) ); 107cdf0e10cSrcweir 108cdf0e10cSrcweir return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ); 109cdf0e10cSrcweir } 110cdf0e10cSrcweir 111cdf0e10cSrcweir 112cdf0e10cSrcweir void SAL_CALL BufferedInputStream::acquire( void ) throw() 113cdf0e10cSrcweir { 114cdf0e10cSrcweir OWeakObject::acquire(); 115cdf0e10cSrcweir } 116cdf0e10cSrcweir 117cdf0e10cSrcweir 118cdf0e10cSrcweir void SAL_CALL BufferedInputStream::release( void ) throw() 119cdf0e10cSrcweir { 120cdf0e10cSrcweir OWeakObject::release(); 121cdf0e10cSrcweir } 122cdf0e10cSrcweir 123cdf0e10cSrcweir 124cdf0e10cSrcweir 125cdf0e10cSrcweir sal_Int32 SAL_CALL BufferedInputStream::readBytes( Sequence< sal_Int8 >& aData,sal_Int32 nBytesToRead ) 126cdf0e10cSrcweir throw( NotConnectedException, 127cdf0e10cSrcweir BufferSizeExceededException, 128cdf0e10cSrcweir IOException, 129cdf0e10cSrcweir RuntimeException) 130cdf0e10cSrcweir { 131cdf0e10cSrcweir osl::MutexGuard aGuard( m_aMutex ); 132cdf0e10cSrcweir 133cdf0e10cSrcweir if( 0 > nBytesToRead ) 134cdf0e10cSrcweir throw BufferSizeExceededException(); 135cdf0e10cSrcweir 136cdf0e10cSrcweir if( m_nBufferLocation + nBytesToRead > m_nBufferSize ) 137cdf0e10cSrcweir nBytesToRead = m_nBufferSize - m_nBufferLocation; 138cdf0e10cSrcweir 139cdf0e10cSrcweir if( aData.getLength() < nBytesToRead ) 140cdf0e10cSrcweir aData.realloc(nBytesToRead); 141cdf0e10cSrcweir 142cdf0e10cSrcweir rtl_copyMemory((void*)(aData.getArray()), 143cdf0e10cSrcweir (void*)(m_pBuffer+m_nBufferLocation), 144cdf0e10cSrcweir nBytesToRead); 145cdf0e10cSrcweir 146cdf0e10cSrcweir return nBytesToRead; 147cdf0e10cSrcweir } 148cdf0e10cSrcweir 149cdf0e10cSrcweir 150cdf0e10cSrcweir sal_Int32 SAL_CALL BufferedInputStream::readSomeBytes( 151cdf0e10cSrcweir Sequence< sal_Int8 >& aData,sal_Int32 nMaxBytesToRead ) 152cdf0e10cSrcweir throw( NotConnectedException, 153cdf0e10cSrcweir BufferSizeExceededException, 154cdf0e10cSrcweir IOException, 155cdf0e10cSrcweir RuntimeException) 156cdf0e10cSrcweir { 157cdf0e10cSrcweir return readBytes(aData,nMaxBytesToRead); 158cdf0e10cSrcweir } 159cdf0e10cSrcweir 160cdf0e10cSrcweir 161cdf0e10cSrcweir 162cdf0e10cSrcweir void SAL_CALL BufferedInputStream::skipBytes( sal_Int32 nBytesToSkip ) 163cdf0e10cSrcweir throw( NotConnectedException, 164cdf0e10cSrcweir BufferSizeExceededException, 165cdf0e10cSrcweir IOException, 166cdf0e10cSrcweir RuntimeException ) 167cdf0e10cSrcweir { 168cdf0e10cSrcweir try 169cdf0e10cSrcweir { 170cdf0e10cSrcweir seek(m_nBufferLocation+nBytesToSkip); 171cdf0e10cSrcweir } 172cdf0e10cSrcweir catch( const IllegalArgumentException& ) 173cdf0e10cSrcweir { 174cdf0e10cSrcweir throw BufferSizeExceededException(); 175cdf0e10cSrcweir } 176cdf0e10cSrcweir } 177cdf0e10cSrcweir 178cdf0e10cSrcweir 179cdf0e10cSrcweir 180cdf0e10cSrcweir sal_Int32 SAL_CALL BufferedInputStream::available( void ) 181cdf0e10cSrcweir throw( NotConnectedException, 182cdf0e10cSrcweir IOException, 183cdf0e10cSrcweir RuntimeException ) 184cdf0e10cSrcweir { 185cdf0e10cSrcweir osl::MutexGuard aGuard( m_aMutex ); 186cdf0e10cSrcweir return m_nBufferSize-m_nBufferLocation; 187cdf0e10cSrcweir } 188cdf0e10cSrcweir 189cdf0e10cSrcweir 190cdf0e10cSrcweir 191cdf0e10cSrcweir void SAL_CALL BufferedInputStream::closeInput( void ) 192cdf0e10cSrcweir throw( NotConnectedException, 193cdf0e10cSrcweir IOException, 194cdf0e10cSrcweir RuntimeException ) 195cdf0e10cSrcweir { 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir 199cdf0e10cSrcweir void SAL_CALL BufferedInputStream::seek( sal_Int64 location ) 200cdf0e10cSrcweir throw( IllegalArgumentException, 201cdf0e10cSrcweir IOException, 202cdf0e10cSrcweir RuntimeException ) 203cdf0e10cSrcweir { 204cdf0e10cSrcweir if( 0 <= location && location < m_nBufferSize ) 205cdf0e10cSrcweir { 206cdf0e10cSrcweir osl::MutexGuard aGuard( m_aMutex ); 207cdf0e10cSrcweir m_nBufferLocation = sal::static_int_cast<sal_Int32>( location ); 208cdf0e10cSrcweir } 209cdf0e10cSrcweir else 210cdf0e10cSrcweir throw IllegalArgumentException(); 211cdf0e10cSrcweir } 212cdf0e10cSrcweir 213cdf0e10cSrcweir 214cdf0e10cSrcweir 215cdf0e10cSrcweir sal_Int64 SAL_CALL BufferedInputStream::getPosition( void ) 216cdf0e10cSrcweir throw( IOException, 217cdf0e10cSrcweir RuntimeException ) 218cdf0e10cSrcweir { 219cdf0e10cSrcweir osl::MutexGuard aGuard( m_aMutex ); 220cdf0e10cSrcweir return m_nBufferLocation; 221cdf0e10cSrcweir } 222cdf0e10cSrcweir 223cdf0e10cSrcweir 224cdf0e10cSrcweir 225cdf0e10cSrcweir sal_Int64 SAL_CALL BufferedInputStream::getLength( void ) throw( IOException,RuntimeException ) 226cdf0e10cSrcweir { 227cdf0e10cSrcweir osl::MutexGuard aGuard( m_aMutex ); 228cdf0e10cSrcweir return m_nBufferSize; 229cdf0e10cSrcweir } 230