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 25cdf0e10cSrcweir #include "precompiled_ucb.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir /************************************************************************** 28cdf0e10cSrcweir TODO 29cdf0e10cSrcweir ************************************************************************** 30cdf0e10cSrcweir 31cdf0e10cSrcweir *************************************************************************/ 32cdf0e10cSrcweir #include "ftpinpstr.hxx" 33cdf0e10cSrcweir #ifndef _RTL_ALLOC_H 34cdf0e10cSrcweir #include <rtl/alloc.h> 35cdf0e10cSrcweir #endif 36cdf0e10cSrcweir #ifndef STD_ALGORITHM 37cdf0e10cSrcweir #include <algorithm> 38cdf0e10cSrcweir #define STD_ALGORITHM 39cdf0e10cSrcweir #endif 40cdf0e10cSrcweir 41cdf0e10cSrcweir using namespace ftp; 42cdf0e10cSrcweir using namespace com::sun::star::uno; 43cdf0e10cSrcweir using namespace com::sun::star::lang; 44cdf0e10cSrcweir using namespace com::sun::star::io; 45cdf0e10cSrcweir 46cdf0e10cSrcweir 47*72cd26ddSAriel Constenla-Haile FTPInputStream::FTPInputStream( oslFileHandle tmpfl ) 48*72cd26ddSAriel Constenla-Haile : m_tmpfl(tmpfl) 49*72cd26ddSAriel Constenla-Haile , m_nLength( 0 ) 50cdf0e10cSrcweir { 51*72cd26ddSAriel Constenla-Haile if ( !m_tmpfl ) 52*72cd26ddSAriel Constenla-Haile osl_createTempFile( NULL, &m_tmpfl, NULL ); 53*72cd26ddSAriel Constenla-Haile OSL_ENSURE( m_tmpfl, "input stream without tempfile!" ); 54*72cd26ddSAriel Constenla-Haile 55*72cd26ddSAriel Constenla-Haile if ( osl_setFilePos( m_tmpfl, osl_Pos_End, 0 ) == osl_File_E_None ) 56*72cd26ddSAriel Constenla-Haile { 57*72cd26ddSAriel Constenla-Haile sal_uInt64 nFileSize = 0; 58*72cd26ddSAriel Constenla-Haile if ( osl_getFilePos( m_tmpfl, &nFileSize ) == osl_File_E_None ) 59*72cd26ddSAriel Constenla-Haile m_nLength = nFileSize; 60*72cd26ddSAriel Constenla-Haile osl_setFilePos( m_tmpfl, osl_Pos_Absolut, 0 ); 61cdf0e10cSrcweir } 62*72cd26ddSAriel Constenla-Haile } 63cdf0e10cSrcweir 64cdf0e10cSrcweir FTPInputStream::~FTPInputStream() 65cdf0e10cSrcweir { 66cdf0e10cSrcweir if ( 0 != m_tmpfl) 67*72cd26ddSAriel Constenla-Haile osl_closeFile(m_tmpfl); 68cdf0e10cSrcweir } 69cdf0e10cSrcweir 70cdf0e10cSrcweir sal_Int32 SAL_CALL FTPInputStream::readBytes(Sequence< sal_Int8 >& aData, 71cdf0e10cSrcweir sal_Int32 nBytesToRead) 72cdf0e10cSrcweir throw(NotConnectedException, 73cdf0e10cSrcweir BufferSizeExceededException, 74cdf0e10cSrcweir IOException, 75cdf0e10cSrcweir RuntimeException) 76cdf0e10cSrcweir { 77cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 78cdf0e10cSrcweir 79*72cd26ddSAriel Constenla-Haile sal_uInt64 nBeforePos( 0 ); 80*72cd26ddSAriel Constenla-Haile sal_uInt64 nBytesRequested( nBytesToRead ); 81*72cd26ddSAriel Constenla-Haile sal_uInt64 nBytesRead( 0 ); 82*72cd26ddSAriel Constenla-Haile 83*72cd26ddSAriel Constenla-Haile osl_getFilePos( m_tmpfl, &nBeforePos ); 84*72cd26ddSAriel Constenla-Haile 85*72cd26ddSAriel Constenla-Haile if ( 0 == ( nBytesRequested = std::min< sal_uInt64 >( m_nLength - nBeforePos, nBytesRequested ) ) ) 86*72cd26ddSAriel Constenla-Haile return 0; 87*72cd26ddSAriel Constenla-Haile 88cdf0e10cSrcweir if ( 0 <= nBytesToRead && aData.getLength() < nBytesToRead ) 89cdf0e10cSrcweir aData.realloc( nBytesToRead ); 90cdf0e10cSrcweir 91*72cd26ddSAriel Constenla-Haile if ( osl_readFile( m_tmpfl, aData.getArray(), nBytesRequested, &nBytesRead ) != osl_File_E_None ) 92cdf0e10cSrcweir throw IOException(); 93cdf0e10cSrcweir 94*72cd26ddSAriel Constenla-Haile return sal_Int32( nBytesRead ); 95cdf0e10cSrcweir } 96cdf0e10cSrcweir 97cdf0e10cSrcweir 98cdf0e10cSrcweir sal_Int32 SAL_CALL FTPInputStream::readSomeBytes( Sequence< sal_Int8 >& aData, 99cdf0e10cSrcweir sal_Int32 nMaxBytesToRead ) 100cdf0e10cSrcweir throw( NotConnectedException, 101cdf0e10cSrcweir BufferSizeExceededException, 102cdf0e10cSrcweir IOException, 103cdf0e10cSrcweir RuntimeException) 104cdf0e10cSrcweir { 105cdf0e10cSrcweir return readBytes(aData,nMaxBytesToRead); 106cdf0e10cSrcweir } 107cdf0e10cSrcweir 108cdf0e10cSrcweir 109cdf0e10cSrcweir 110cdf0e10cSrcweir void SAL_CALL FTPInputStream::skipBytes(sal_Int32 nBytesToSkip) 111cdf0e10cSrcweir throw(NotConnectedException, 112cdf0e10cSrcweir BufferSizeExceededException, 113cdf0e10cSrcweir IOException, 114cdf0e10cSrcweir RuntimeException) 115cdf0e10cSrcweir { 116cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 117cdf0e10cSrcweir if(!m_tmpfl) 118cdf0e10cSrcweir throw IOException(); 119cdf0e10cSrcweir 120*72cd26ddSAriel Constenla-Haile osl_setFilePos( m_tmpfl, osl_Pos_Current, nBytesToSkip ); 121cdf0e10cSrcweir } 122cdf0e10cSrcweir 123cdf0e10cSrcweir 124cdf0e10cSrcweir 125cdf0e10cSrcweir sal_Int32 SAL_CALL FTPInputStream::available(void) 126cdf0e10cSrcweir throw(NotConnectedException, 127cdf0e10cSrcweir IOException, 128cdf0e10cSrcweir RuntimeException) 129cdf0e10cSrcweir { 130cdf0e10cSrcweir return sal::static_int_cast<sal_Int32>(m_nLength - getPosition()); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir 133cdf0e10cSrcweir 134cdf0e10cSrcweir 135cdf0e10cSrcweir void SAL_CALL FTPInputStream::closeInput(void) 136cdf0e10cSrcweir throw(NotConnectedException, 137cdf0e10cSrcweir IOException, 138cdf0e10cSrcweir RuntimeException) 139cdf0e10cSrcweir { 140cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 141cdf0e10cSrcweir if(m_tmpfl) 142*72cd26ddSAriel Constenla-Haile osl_closeFile(m_tmpfl),m_tmpfl = 0; 143cdf0e10cSrcweir } 144cdf0e10cSrcweir 145cdf0e10cSrcweir 146cdf0e10cSrcweir 147cdf0e10cSrcweir void SAL_CALL FTPInputStream::seek(sal_Int64 location) 148cdf0e10cSrcweir throw( IllegalArgumentException, 149cdf0e10cSrcweir IOException, 150cdf0e10cSrcweir RuntimeException ) 151cdf0e10cSrcweir { 152cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 153cdf0e10cSrcweir if(!m_tmpfl) 154cdf0e10cSrcweir throw IOException(); 155cdf0e10cSrcweir 156*72cd26ddSAriel Constenla-Haile osl_setFilePos( m_tmpfl, osl_Pos_Absolut, location ); 157cdf0e10cSrcweir } 158cdf0e10cSrcweir 159cdf0e10cSrcweir 160cdf0e10cSrcweir 161cdf0e10cSrcweir sal_Int64 SAL_CALL 162cdf0e10cSrcweir FTPInputStream::getPosition( 163cdf0e10cSrcweir void ) 164cdf0e10cSrcweir throw( IOException, 165cdf0e10cSrcweir RuntimeException ) 166cdf0e10cSrcweir { 167cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 168cdf0e10cSrcweir if(!m_tmpfl) 169cdf0e10cSrcweir throw IOException(); 170cdf0e10cSrcweir 171*72cd26ddSAriel Constenla-Haile sal_uInt64 nFilePos = 0; 172*72cd26ddSAriel Constenla-Haile osl_getFilePos( m_tmpfl, &nFilePos ); 173*72cd26ddSAriel Constenla-Haile return nFilePos; 174cdf0e10cSrcweir } 175cdf0e10cSrcweir 176cdf0e10cSrcweir 177cdf0e10cSrcweir 178cdf0e10cSrcweir sal_Int64 SAL_CALL FTPInputStream::getLength( 179cdf0e10cSrcweir void 180cdf0e10cSrcweir ) throw( 181cdf0e10cSrcweir IOException,RuntimeException 182cdf0e10cSrcweir ) 183cdf0e10cSrcweir { 184cdf0e10cSrcweir return m_nLength; 185cdf0e10cSrcweir } 186