1*b0724fc6SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*b0724fc6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*b0724fc6SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*b0724fc6SAndrew Rist * distributed with this work for additional information 6*b0724fc6SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*b0724fc6SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*b0724fc6SAndrew Rist * "License"); you may not use this file except in compliance 9*b0724fc6SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*b0724fc6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*b0724fc6SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*b0724fc6SAndrew Rist * software distributed under the License is distributed on an 15*b0724fc6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b0724fc6SAndrew Rist * KIND, either express or implied. See the License for the 17*b0724fc6SAndrew Rist * specific language governing permissions and limitations 18*b0724fc6SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*b0724fc6SAndrew Rist *************************************************************/ 21*b0724fc6SAndrew Rist 22*b0724fc6SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_toolkit.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir 28cdf0e10cSrcweir #include <toolkit/helper/unomemorystream.hxx> 29cdf0e10cSrcweir #include <algorithm> 30cdf0e10cSrcweir 31cdf0e10cSrcweir // ---------------------------------------------------- 32cdf0e10cSrcweir // class UnoMemoryStream 33cdf0e10cSrcweir // ---------------------------------------------------- 34cdf0e10cSrcweir UnoMemoryStream::UnoMemoryStream( sal_uInt32 nInitSize, sal_uInt32 nInitResize ) 35cdf0e10cSrcweir : SvMemoryStream( nInitSize, nInitResize ) 36cdf0e10cSrcweir { 37cdf0e10cSrcweir } 38cdf0e10cSrcweir 39cdf0e10cSrcweir // ::com::sun::star::uno::XInterface 40cdf0e10cSrcweir ::com::sun::star::uno::Any UnoMemoryStream::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException) 41cdf0e10cSrcweir { 42cdf0e10cSrcweir ::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType, 43cdf0e10cSrcweir SAL_STATIC_CAST( ::com::sun::star::io::XInputStream*, this ) ); 44cdf0e10cSrcweir return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType )); 45cdf0e10cSrcweir } 46cdf0e10cSrcweir 47cdf0e10cSrcweir 48cdf0e10cSrcweir // ::com::sun::star::io::XInputStream 49cdf0e10cSrcweir sal_Int32 UnoMemoryStream::readBytes( ::com::sun::star::uno::Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException) 50cdf0e10cSrcweir { 51cdf0e10cSrcweir ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); 52cdf0e10cSrcweir 53cdf0e10cSrcweir sal_Int32 nRead = available(); 54cdf0e10cSrcweir if ( nRead > nBytesToRead ) 55cdf0e10cSrcweir nRead = nBytesToRead; 56cdf0e10cSrcweir 57cdf0e10cSrcweir rData = ::com::sun::star::uno::Sequence< sal_Int8 >( nRead ); 58cdf0e10cSrcweir Read( rData.getArray(), nRead ); 59cdf0e10cSrcweir 60cdf0e10cSrcweir return nRead; 61cdf0e10cSrcweir } 62cdf0e10cSrcweir 63cdf0e10cSrcweir sal_Int32 UnoMemoryStream::readSomeBytes( ::com::sun::star::uno::Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException) 64cdf0e10cSrcweir { 65cdf0e10cSrcweir ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); 66cdf0e10cSrcweir 67cdf0e10cSrcweir sal_Int32 nAvailable = available(); 68cdf0e10cSrcweir if( nAvailable ) 69cdf0e10cSrcweir { 70cdf0e10cSrcweir return readBytes( rData, std::min( nMaxBytesToRead , nAvailable ) ); 71cdf0e10cSrcweir } 72cdf0e10cSrcweir else 73cdf0e10cSrcweir { 74cdf0e10cSrcweir // Not the most effective method, but it sticks to the specification 75cdf0e10cSrcweir return readBytes( rData, 1 ); 76cdf0e10cSrcweir } 77cdf0e10cSrcweir } 78cdf0e10cSrcweir 79cdf0e10cSrcweir void UnoMemoryStream::skipBytes( sal_Int32 nBytesToSkip ) throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::BufferSizeExceededException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException) 80cdf0e10cSrcweir { 81cdf0e10cSrcweir ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); 82cdf0e10cSrcweir 83cdf0e10cSrcweir SeekRel( nBytesToSkip ); 84cdf0e10cSrcweir } 85cdf0e10cSrcweir 86cdf0e10cSrcweir sal_Int32 UnoMemoryStream::available() throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException) 87cdf0e10cSrcweir { 88cdf0e10cSrcweir ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); 89cdf0e10cSrcweir 90cdf0e10cSrcweir sal_uInt32 nStreamPos = Tell(); 91cdf0e10cSrcweir sal_uInt32 nEnd = Seek( STREAM_SEEK_TO_END ); 92cdf0e10cSrcweir Seek( nStreamPos ); 93cdf0e10cSrcweir return nEnd - nStreamPos; 94cdf0e10cSrcweir } 95cdf0e10cSrcweir 96cdf0e10cSrcweir void UnoMemoryStream::closeInput() throw(::com::sun::star::io::NotConnectedException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException) 97cdf0e10cSrcweir { 98cdf0e10cSrcweir // nothing to do 99cdf0e10cSrcweir } 100cdf0e10cSrcweir 101cdf0e10cSrcweir 102cdf0e10cSrcweir 103cdf0e10cSrcweir 104cdf0e10cSrcweir 105