1*2f86921cSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*2f86921cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*2f86921cSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*2f86921cSAndrew Rist * distributed with this work for additional information 6*2f86921cSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*2f86921cSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*2f86921cSAndrew Rist * "License"); you may not use this file except in compliance 9*2f86921cSAndrew Rist * with the License. You may obtain a copy of the License at 10*2f86921cSAndrew Rist * 11*2f86921cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*2f86921cSAndrew Rist * 13*2f86921cSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*2f86921cSAndrew Rist * software distributed under the License is distributed on an 15*2f86921cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*2f86921cSAndrew Rist * KIND, either express or implied. See the License for the 17*2f86921cSAndrew Rist * specific language governing permissions and limitations 18*2f86921cSAndrew Rist * under the License. 19*2f86921cSAndrew Rist * 20*2f86921cSAndrew Rist *************************************************************/ 21*2f86921cSAndrew Rist 22*2f86921cSAndrew 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 #include <stdio.h> 41cdf0e10cSrcweir 42cdf0e10cSrcweir using namespace ftp; 43cdf0e10cSrcweir using namespace com::sun::star::uno; 44cdf0e10cSrcweir using namespace com::sun::star::lang; 45cdf0e10cSrcweir using namespace com::sun::star::io; 46cdf0e10cSrcweir 47cdf0e10cSrcweir 48cdf0e10cSrcweir FTPInputStream::FTPInputStream(FILE* tmpfl) 49cdf0e10cSrcweir : m_tmpfl(tmpfl ? tmpfl : tmpfile()) 50cdf0e10cSrcweir { 51cdf0e10cSrcweir fseek(m_tmpfl,0,SEEK_END); 52cdf0e10cSrcweir // fpos_t pos; 53cdf0e10cSrcweir // fgetpos(m_tmpfl,&pos); 54cdf0e10cSrcweir long pos = ftell(m_tmpfl); 55cdf0e10cSrcweir rewind(m_tmpfl); 56cdf0e10cSrcweir m_nLength = sal_Int64(pos); 57cdf0e10cSrcweir } 58cdf0e10cSrcweir 59cdf0e10cSrcweir 60cdf0e10cSrcweir 61cdf0e10cSrcweir FTPInputStream::~FTPInputStream() 62cdf0e10cSrcweir { 63cdf0e10cSrcweir if ( 0 != m_tmpfl) 64cdf0e10cSrcweir fclose(m_tmpfl); 65cdf0e10cSrcweir } 66cdf0e10cSrcweir 67cdf0e10cSrcweir 68cdf0e10cSrcweir Any SAL_CALL FTPInputStream::queryInterface( 69cdf0e10cSrcweir const Type& rType 70cdf0e10cSrcweir ) 71cdf0e10cSrcweir throw( 72cdf0e10cSrcweir RuntimeException 73cdf0e10cSrcweir ) 74cdf0e10cSrcweir { 75cdf0e10cSrcweir Any aRet = ::cppu::queryInterface(rType, 76cdf0e10cSrcweir SAL_STATIC_CAST( XInputStream*,this ), 77cdf0e10cSrcweir SAL_STATIC_CAST( XSeekable*,this ) ); 78cdf0e10cSrcweir 79cdf0e10cSrcweir return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ); 80cdf0e10cSrcweir } 81cdf0e10cSrcweir 82cdf0e10cSrcweir 83cdf0e10cSrcweir 84cdf0e10cSrcweir void SAL_CALL FTPInputStream::acquire( void ) throw() { 85cdf0e10cSrcweir OWeakObject::acquire(); 86cdf0e10cSrcweir } 87cdf0e10cSrcweir 88cdf0e10cSrcweir 89cdf0e10cSrcweir 90cdf0e10cSrcweir void SAL_CALL FTPInputStream::release( void ) throw() { 91cdf0e10cSrcweir OWeakObject::release(); 92cdf0e10cSrcweir } 93cdf0e10cSrcweir 94cdf0e10cSrcweir 95cdf0e10cSrcweir sal_Int32 SAL_CALL FTPInputStream::readBytes(Sequence< sal_Int8 >& aData, 96cdf0e10cSrcweir sal_Int32 nBytesToRead) 97cdf0e10cSrcweir throw(NotConnectedException, 98cdf0e10cSrcweir BufferSizeExceededException, 99cdf0e10cSrcweir IOException, 100cdf0e10cSrcweir RuntimeException) 101cdf0e10cSrcweir { 102cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 103cdf0e10cSrcweir 104cdf0e10cSrcweir if(0 <= nBytesToRead && aData.getLength() < nBytesToRead) 105cdf0e10cSrcweir aData.realloc(nBytesToRead); 106cdf0e10cSrcweir 107cdf0e10cSrcweir // fpos_t bpos,epos; 108cdf0e10cSrcweir 109cdf0e10cSrcweir // fgetpos(m_tmpfl,&bpos); 110cdf0e10cSrcweir // fread(aData.getArray(),nBytesToRead,1,m_tmpfl); 111cdf0e10cSrcweir // fgetpos(m_tmpfl,&epos); 112cdf0e10cSrcweir long bpos,epos; 113cdf0e10cSrcweir 114cdf0e10cSrcweir bpos = ftell(m_tmpfl); 115cdf0e10cSrcweir if (fread(aData.getArray(),nBytesToRead,1,m_tmpfl) != 1) 116cdf0e10cSrcweir throw IOException(); 117cdf0e10cSrcweir 118cdf0e10cSrcweir epos = ftell(m_tmpfl); 119cdf0e10cSrcweir 120cdf0e10cSrcweir return sal_Int32(epos-bpos); 121cdf0e10cSrcweir } 122cdf0e10cSrcweir 123cdf0e10cSrcweir 124cdf0e10cSrcweir sal_Int32 SAL_CALL FTPInputStream::readSomeBytes( Sequence< sal_Int8 >& aData, 125cdf0e10cSrcweir sal_Int32 nMaxBytesToRead ) 126cdf0e10cSrcweir throw( NotConnectedException, 127cdf0e10cSrcweir BufferSizeExceededException, 128cdf0e10cSrcweir IOException, 129cdf0e10cSrcweir RuntimeException) 130cdf0e10cSrcweir { 131cdf0e10cSrcweir return readBytes(aData,nMaxBytesToRead); 132cdf0e10cSrcweir } 133cdf0e10cSrcweir 134cdf0e10cSrcweir 135cdf0e10cSrcweir 136cdf0e10cSrcweir void SAL_CALL FTPInputStream::skipBytes(sal_Int32 nBytesToSkip) 137cdf0e10cSrcweir throw(NotConnectedException, 138cdf0e10cSrcweir BufferSizeExceededException, 139cdf0e10cSrcweir IOException, 140cdf0e10cSrcweir RuntimeException) 141cdf0e10cSrcweir { 142cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 143cdf0e10cSrcweir if(!m_tmpfl) 144cdf0e10cSrcweir throw IOException(); 145cdf0e10cSrcweir 146cdf0e10cSrcweir fseek(m_tmpfl,long(nBytesToSkip),SEEK_CUR); 147cdf0e10cSrcweir } 148cdf0e10cSrcweir 149cdf0e10cSrcweir 150cdf0e10cSrcweir 151cdf0e10cSrcweir sal_Int32 SAL_CALL FTPInputStream::available(void) 152cdf0e10cSrcweir throw(NotConnectedException, 153cdf0e10cSrcweir IOException, 154cdf0e10cSrcweir RuntimeException) 155cdf0e10cSrcweir { 156cdf0e10cSrcweir return sal::static_int_cast<sal_Int32>(m_nLength - getPosition()); 157cdf0e10cSrcweir } 158cdf0e10cSrcweir 159cdf0e10cSrcweir 160cdf0e10cSrcweir 161cdf0e10cSrcweir void SAL_CALL FTPInputStream::closeInput(void) 162cdf0e10cSrcweir throw(NotConnectedException, 163cdf0e10cSrcweir IOException, 164cdf0e10cSrcweir RuntimeException) 165cdf0e10cSrcweir { 166cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 167cdf0e10cSrcweir if(m_tmpfl) 168cdf0e10cSrcweir fclose(m_tmpfl),m_tmpfl = 0; 169cdf0e10cSrcweir } 170cdf0e10cSrcweir 171cdf0e10cSrcweir 172cdf0e10cSrcweir 173cdf0e10cSrcweir void SAL_CALL FTPInputStream::seek(sal_Int64 location) 174cdf0e10cSrcweir throw( IllegalArgumentException, 175cdf0e10cSrcweir IOException, 176cdf0e10cSrcweir RuntimeException ) 177cdf0e10cSrcweir { 178cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 179cdf0e10cSrcweir if(!m_tmpfl) 180cdf0e10cSrcweir throw IOException(); 181cdf0e10cSrcweir 182cdf0e10cSrcweir fseek(m_tmpfl,long(location),SEEK_SET); 183cdf0e10cSrcweir } 184cdf0e10cSrcweir 185cdf0e10cSrcweir 186cdf0e10cSrcweir 187cdf0e10cSrcweir sal_Int64 SAL_CALL 188cdf0e10cSrcweir FTPInputStream::getPosition( 189cdf0e10cSrcweir void ) 190cdf0e10cSrcweir throw( IOException, 191cdf0e10cSrcweir RuntimeException ) 192cdf0e10cSrcweir { 193cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 194cdf0e10cSrcweir if(!m_tmpfl) 195cdf0e10cSrcweir throw IOException(); 196cdf0e10cSrcweir 197cdf0e10cSrcweir // fpos_t pos; 198cdf0e10cSrcweir // fgetpos(m_tmpfl,&pos); 199cdf0e10cSrcweir long pos; 200cdf0e10cSrcweir pos = ftell(m_tmpfl); 201cdf0e10cSrcweir return sal_Int64(pos); 202cdf0e10cSrcweir } 203cdf0e10cSrcweir 204cdf0e10cSrcweir 205cdf0e10cSrcweir 206cdf0e10cSrcweir sal_Int64 SAL_CALL FTPInputStream::getLength( 207cdf0e10cSrcweir void 208cdf0e10cSrcweir ) throw( 209cdf0e10cSrcweir IOException,RuntimeException 210cdf0e10cSrcweir ) 211cdf0e10cSrcweir { 212cdf0e10cSrcweir return m_nLength; 213cdf0e10cSrcweir } 214