12f86921cSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 32f86921cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 42f86921cSAndrew Rist * or more contributor license agreements. See the NOTICE file 52f86921cSAndrew Rist * distributed with this work for additional information 62f86921cSAndrew Rist * regarding copyright ownership. The ASF licenses this file 72f86921cSAndrew Rist * to you under the Apache License, Version 2.0 (the 82f86921cSAndrew Rist * "License"); you may not use this file except in compliance 92f86921cSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 112f86921cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 132f86921cSAndrew Rist * Unless required by applicable law or agreed to in writing, 142f86921cSAndrew Rist * software distributed under the License is distributed on an 152f86921cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 162f86921cSAndrew Rist * KIND, either express or implied. See the License for the 172f86921cSAndrew Rist * specific language governing permissions and limitations 182f86921cSAndrew Rist * under the License. 19cdf0e10cSrcweir * 202f86921cSAndrew Rist *************************************************************/ 212f86921cSAndrew Rist 222f86921cSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25*421ed02eSdamjan #include "precompiled_file.hxx" 26cdf0e10cSrcweir #include "filinpstr.hxx" 27cdf0e10cSrcweir #ifndef _FILERROR_HXX_ 28cdf0e10cSrcweir #include "filerror.hxx" 29cdf0e10cSrcweir #endif 30cdf0e10cSrcweir #include "shell.hxx" 31cdf0e10cSrcweir #include "prov.hxx" 32cdf0e10cSrcweir 33cdf0e10cSrcweir 34cdf0e10cSrcweir using namespace fileaccess; 35cdf0e10cSrcweir using namespace com::sun::star; 36cdf0e10cSrcweir using namespace com::sun::star::ucb; 37cdf0e10cSrcweir 38cdf0e10cSrcweir 39cdf0e10cSrcweir 40cdf0e10cSrcweir XInputStream_impl::XInputStream_impl( shell* pMyShell,const rtl::OUString& aUncPath, sal_Bool bLock ) 41cdf0e10cSrcweir : m_pMyShell( pMyShell ), 42cdf0e10cSrcweir m_xProvider( pMyShell->m_pProvider ), 43cdf0e10cSrcweir m_bLock( bLock ), 44cdf0e10cSrcweir m_aFile( aUncPath ), 45cdf0e10cSrcweir m_nErrorCode( TASKHANDLER_NO_ERROR ), 46cdf0e10cSrcweir m_nMinorErrorCode( TASKHANDLER_NO_ERROR ) 47cdf0e10cSrcweir { 48cdf0e10cSrcweir sal_uInt32 nFlags = OpenFlag_Read; 49cdf0e10cSrcweir if ( !bLock ) 50cdf0e10cSrcweir nFlags |= OpenFlag_NoLock; 51cdf0e10cSrcweir 52cdf0e10cSrcweir osl::FileBase::RC err = m_aFile.open( nFlags ); 53cdf0e10cSrcweir if( err != osl::FileBase::E_None ) 54cdf0e10cSrcweir { 55cdf0e10cSrcweir m_nIsOpen = false; 56cdf0e10cSrcweir m_aFile.close(); 57cdf0e10cSrcweir 58cdf0e10cSrcweir m_nErrorCode = TASKHANDLING_OPEN_FOR_INPUTSTREAM; 59cdf0e10cSrcweir m_nMinorErrorCode = err; 60cdf0e10cSrcweir } 61cdf0e10cSrcweir else 62cdf0e10cSrcweir m_nIsOpen = true; 63cdf0e10cSrcweir } 64cdf0e10cSrcweir 65cdf0e10cSrcweir 66cdf0e10cSrcweir XInputStream_impl::~XInputStream_impl() 67cdf0e10cSrcweir { 68cdf0e10cSrcweir try 69cdf0e10cSrcweir { 70cdf0e10cSrcweir closeInput(); 71cdf0e10cSrcweir } 72cdf0e10cSrcweir catch (io::IOException const &) 73cdf0e10cSrcweir { 74cdf0e10cSrcweir OSL_ENSURE(false, "unexpected situation"); 75cdf0e10cSrcweir } 76cdf0e10cSrcweir catch (uno::RuntimeException const &) 77cdf0e10cSrcweir { 78cdf0e10cSrcweir OSL_ENSURE(false, "unexpected situation"); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir } 81cdf0e10cSrcweir 82cdf0e10cSrcweir 83cdf0e10cSrcweir sal_Int32 SAL_CALL XInputStream_impl::CtorSuccess() 84cdf0e10cSrcweir { 85cdf0e10cSrcweir return m_nErrorCode; 86cdf0e10cSrcweir }; 87cdf0e10cSrcweir 88cdf0e10cSrcweir 89cdf0e10cSrcweir 90cdf0e10cSrcweir sal_Int32 SAL_CALL XInputStream_impl::getMinorError() 91cdf0e10cSrcweir { 92cdf0e10cSrcweir return m_nMinorErrorCode; 93cdf0e10cSrcweir } 94cdf0e10cSrcweir 95cdf0e10cSrcweir 96cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////////////////// 97cdf0e10cSrcweir // XTypeProvider 98cdf0e10cSrcweir ////////////////////////////////////////////////////////////////////////////////////////// 99cdf0e10cSrcweir 100cdf0e10cSrcweir 101cdf0e10cSrcweir XTYPEPROVIDER_IMPL_3( XInputStream_impl, 102cdf0e10cSrcweir lang::XTypeProvider, 103cdf0e10cSrcweir io::XSeekable, 104cdf0e10cSrcweir io::XInputStream ) 105cdf0e10cSrcweir 106cdf0e10cSrcweir 107cdf0e10cSrcweir 108cdf0e10cSrcweir uno::Any SAL_CALL 109cdf0e10cSrcweir XInputStream_impl::queryInterface( 110cdf0e10cSrcweir const uno::Type& rType ) 111cdf0e10cSrcweir throw( uno::RuntimeException) 112cdf0e10cSrcweir { 113cdf0e10cSrcweir uno::Any aRet = cppu::queryInterface( rType, 114cdf0e10cSrcweir SAL_STATIC_CAST( io::XInputStream*,this ), 115cdf0e10cSrcweir SAL_STATIC_CAST( lang::XTypeProvider*,this ), 116cdf0e10cSrcweir SAL_STATIC_CAST( io::XSeekable*,this ) ); 117cdf0e10cSrcweir return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ); 118cdf0e10cSrcweir } 119cdf0e10cSrcweir 120cdf0e10cSrcweir 121cdf0e10cSrcweir void SAL_CALL 122cdf0e10cSrcweir XInputStream_impl::acquire( 123cdf0e10cSrcweir void ) 124cdf0e10cSrcweir throw() 125cdf0e10cSrcweir { 126cdf0e10cSrcweir OWeakObject::acquire(); 127cdf0e10cSrcweir } 128cdf0e10cSrcweir 129cdf0e10cSrcweir 130cdf0e10cSrcweir void SAL_CALL 131cdf0e10cSrcweir XInputStream_impl::release( 132cdf0e10cSrcweir void ) 133cdf0e10cSrcweir throw() 134cdf0e10cSrcweir { 135cdf0e10cSrcweir OWeakObject::release(); 136cdf0e10cSrcweir } 137cdf0e10cSrcweir 138cdf0e10cSrcweir 139cdf0e10cSrcweir 140cdf0e10cSrcweir sal_Int32 SAL_CALL 141cdf0e10cSrcweir XInputStream_impl::readBytes( 142cdf0e10cSrcweir uno::Sequence< sal_Int8 >& aData, 143cdf0e10cSrcweir sal_Int32 nBytesToRead ) 144cdf0e10cSrcweir throw( io::NotConnectedException, 145cdf0e10cSrcweir io::BufferSizeExceededException, 146cdf0e10cSrcweir io::IOException, 147cdf0e10cSrcweir uno::RuntimeException) 148cdf0e10cSrcweir { 149cdf0e10cSrcweir if( ! m_nIsOpen ) throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 150cdf0e10cSrcweir 151cdf0e10cSrcweir aData.realloc(nBytesToRead); 152cdf0e10cSrcweir //TODO! translate memory exhaustion (if it were detectable...) into 153cdf0e10cSrcweir // io::BufferSizeExceededException 154cdf0e10cSrcweir 155cdf0e10cSrcweir sal_uInt64 nrc(0); 156cdf0e10cSrcweir if(m_aFile.read( aData.getArray(),sal_uInt64(nBytesToRead),nrc ) 157cdf0e10cSrcweir != osl::FileBase::E_None) 158cdf0e10cSrcweir throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 159cdf0e10cSrcweir 160cdf0e10cSrcweir // Shrink aData in case we read less than nBytesToRead (XInputStream 161cdf0e10cSrcweir // documentation does not tell whether this is required, and I do not know 162cdf0e10cSrcweir // if any code relies on this, so be conservative---SB): 163cdf0e10cSrcweir if (sal::static_int_cast<sal_Int32>(nrc) != nBytesToRead) 164cdf0e10cSrcweir aData.realloc(sal_Int32(nrc)); 165cdf0e10cSrcweir return ( sal_Int32 ) nrc; 166cdf0e10cSrcweir } 167cdf0e10cSrcweir 168cdf0e10cSrcweir sal_Int32 SAL_CALL 169cdf0e10cSrcweir XInputStream_impl::readSomeBytes( 170cdf0e10cSrcweir uno::Sequence< sal_Int8 >& aData, 171cdf0e10cSrcweir sal_Int32 nMaxBytesToRead ) 172cdf0e10cSrcweir throw( io::NotConnectedException, 173cdf0e10cSrcweir io::BufferSizeExceededException, 174cdf0e10cSrcweir io::IOException, 175cdf0e10cSrcweir uno::RuntimeException) 176cdf0e10cSrcweir { 177cdf0e10cSrcweir return readBytes( aData,nMaxBytesToRead ); 178cdf0e10cSrcweir } 179cdf0e10cSrcweir 180cdf0e10cSrcweir 181cdf0e10cSrcweir void SAL_CALL 182cdf0e10cSrcweir XInputStream_impl::skipBytes( 183cdf0e10cSrcweir sal_Int32 nBytesToSkip ) 184cdf0e10cSrcweir throw( io::NotConnectedException, 185cdf0e10cSrcweir io::BufferSizeExceededException, 186cdf0e10cSrcweir io::IOException, 187cdf0e10cSrcweir uno::RuntimeException) 188cdf0e10cSrcweir { 189cdf0e10cSrcweir m_aFile.setPos( osl_Pos_Current, sal_uInt64( nBytesToSkip ) ); 190cdf0e10cSrcweir } 191cdf0e10cSrcweir 192cdf0e10cSrcweir 193cdf0e10cSrcweir sal_Int32 SAL_CALL 194cdf0e10cSrcweir XInputStream_impl::available( 195cdf0e10cSrcweir void ) 196cdf0e10cSrcweir throw( io::NotConnectedException, 197cdf0e10cSrcweir io::IOException, 198cdf0e10cSrcweir uno::RuntimeException) 199cdf0e10cSrcweir { 200cdf0e10cSrcweir return 0; 201cdf0e10cSrcweir } 202cdf0e10cSrcweir 203cdf0e10cSrcweir 204cdf0e10cSrcweir void SAL_CALL 205cdf0e10cSrcweir XInputStream_impl::closeInput( 206cdf0e10cSrcweir void ) 207cdf0e10cSrcweir throw( io::NotConnectedException, 208cdf0e10cSrcweir io::IOException, 209cdf0e10cSrcweir uno::RuntimeException ) 210cdf0e10cSrcweir { 211cdf0e10cSrcweir if( m_nIsOpen ) 212cdf0e10cSrcweir { 213cdf0e10cSrcweir osl::FileBase::RC err = m_aFile.close(); 214cdf0e10cSrcweir if( err != osl::FileBase::E_None ) 215cdf0e10cSrcweir throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 216cdf0e10cSrcweir m_nIsOpen = false; 217cdf0e10cSrcweir } 218cdf0e10cSrcweir } 219cdf0e10cSrcweir 220cdf0e10cSrcweir 221cdf0e10cSrcweir void SAL_CALL 222cdf0e10cSrcweir XInputStream_impl::seek( 223cdf0e10cSrcweir sal_Int64 location ) 224cdf0e10cSrcweir throw( lang::IllegalArgumentException, 225cdf0e10cSrcweir io::IOException, 226cdf0e10cSrcweir uno::RuntimeException ) 227cdf0e10cSrcweir { 228cdf0e10cSrcweir if( location < 0 ) 229cdf0e10cSrcweir throw lang::IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >(), 0 ); 230cdf0e10cSrcweir if( osl::FileBase::E_None != m_aFile.setPos( Pos_Absolut, sal_uInt64( location ) ) ) 231cdf0e10cSrcweir throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 232cdf0e10cSrcweir } 233cdf0e10cSrcweir 234cdf0e10cSrcweir 235cdf0e10cSrcweir sal_Int64 SAL_CALL 236cdf0e10cSrcweir XInputStream_impl::getPosition( 237cdf0e10cSrcweir void ) 238cdf0e10cSrcweir throw( io::IOException, 239cdf0e10cSrcweir uno::RuntimeException ) 240cdf0e10cSrcweir { 241cdf0e10cSrcweir sal_uInt64 uPos; 242cdf0e10cSrcweir if( osl::FileBase::E_None != m_aFile.getPos( uPos ) ) 243cdf0e10cSrcweir throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 244cdf0e10cSrcweir return sal_Int64( uPos ); 245cdf0e10cSrcweir } 246cdf0e10cSrcweir 247cdf0e10cSrcweir sal_Int64 SAL_CALL 248cdf0e10cSrcweir XInputStream_impl::getLength( 249cdf0e10cSrcweir void ) 250cdf0e10cSrcweir throw( io::IOException, 251cdf0e10cSrcweir uno::RuntimeException ) 252cdf0e10cSrcweir { 253cdf0e10cSrcweir sal_uInt64 uEndPos; 254cdf0e10cSrcweir if ( m_aFile.getSize(uEndPos) != osl::FileBase::E_None ) 255cdf0e10cSrcweir throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ), uno::Reference< uno::XInterface >() ); 256cdf0e10cSrcweir else 257cdf0e10cSrcweir return sal_Int64( uEndPos ); 258cdf0e10cSrcweir } 259