1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_ucb.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir /************************************************************************** 32*cdf0e10cSrcweir TODO 33*cdf0e10cSrcweir ************************************************************************** 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir *************************************************************************/ 36*cdf0e10cSrcweir #include "ftpinpstr.hxx" 37*cdf0e10cSrcweir #ifndef _RTL_ALLOC_H 38*cdf0e10cSrcweir #include <rtl/alloc.h> 39*cdf0e10cSrcweir #endif 40*cdf0e10cSrcweir #ifndef STD_ALGORITHM 41*cdf0e10cSrcweir #include <algorithm> 42*cdf0e10cSrcweir #define STD_ALGORITHM 43*cdf0e10cSrcweir #endif 44*cdf0e10cSrcweir #include <stdio.h> 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir using namespace ftp; 47*cdf0e10cSrcweir using namespace com::sun::star::uno; 48*cdf0e10cSrcweir using namespace com::sun::star::lang; 49*cdf0e10cSrcweir using namespace com::sun::star::io; 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir FTPInputStream::FTPInputStream(FILE* tmpfl) 53*cdf0e10cSrcweir : m_tmpfl(tmpfl ? tmpfl : tmpfile()) 54*cdf0e10cSrcweir { 55*cdf0e10cSrcweir fseek(m_tmpfl,0,SEEK_END); 56*cdf0e10cSrcweir // fpos_t pos; 57*cdf0e10cSrcweir // fgetpos(m_tmpfl,&pos); 58*cdf0e10cSrcweir long pos = ftell(m_tmpfl); 59*cdf0e10cSrcweir rewind(m_tmpfl); 60*cdf0e10cSrcweir m_nLength = sal_Int64(pos); 61*cdf0e10cSrcweir } 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir FTPInputStream::~FTPInputStream() 66*cdf0e10cSrcweir { 67*cdf0e10cSrcweir if ( 0 != m_tmpfl) 68*cdf0e10cSrcweir fclose(m_tmpfl); 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir Any SAL_CALL FTPInputStream::queryInterface( 73*cdf0e10cSrcweir const Type& rType 74*cdf0e10cSrcweir ) 75*cdf0e10cSrcweir throw( 76*cdf0e10cSrcweir RuntimeException 77*cdf0e10cSrcweir ) 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir Any aRet = ::cppu::queryInterface(rType, 80*cdf0e10cSrcweir SAL_STATIC_CAST( XInputStream*,this ), 81*cdf0e10cSrcweir SAL_STATIC_CAST( XSeekable*,this ) ); 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ); 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir void SAL_CALL FTPInputStream::acquire( void ) throw() { 89*cdf0e10cSrcweir OWeakObject::acquire(); 90*cdf0e10cSrcweir } 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir void SAL_CALL FTPInputStream::release( void ) throw() { 95*cdf0e10cSrcweir OWeakObject::release(); 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir sal_Int32 SAL_CALL FTPInputStream::readBytes(Sequence< sal_Int8 >& aData, 100*cdf0e10cSrcweir sal_Int32 nBytesToRead) 101*cdf0e10cSrcweir throw(NotConnectedException, 102*cdf0e10cSrcweir BufferSizeExceededException, 103*cdf0e10cSrcweir IOException, 104*cdf0e10cSrcweir RuntimeException) 105*cdf0e10cSrcweir { 106*cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir if(0 <= nBytesToRead && aData.getLength() < nBytesToRead) 109*cdf0e10cSrcweir aData.realloc(nBytesToRead); 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir // fpos_t bpos,epos; 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir // fgetpos(m_tmpfl,&bpos); 114*cdf0e10cSrcweir // fread(aData.getArray(),nBytesToRead,1,m_tmpfl); 115*cdf0e10cSrcweir // fgetpos(m_tmpfl,&epos); 116*cdf0e10cSrcweir long bpos,epos; 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir bpos = ftell(m_tmpfl); 119*cdf0e10cSrcweir if (fread(aData.getArray(),nBytesToRead,1,m_tmpfl) != 1) 120*cdf0e10cSrcweir throw IOException(); 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir epos = ftell(m_tmpfl); 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir return sal_Int32(epos-bpos); 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir sal_Int32 SAL_CALL FTPInputStream::readSomeBytes( Sequence< sal_Int8 >& aData, 129*cdf0e10cSrcweir sal_Int32 nMaxBytesToRead ) 130*cdf0e10cSrcweir throw( NotConnectedException, 131*cdf0e10cSrcweir BufferSizeExceededException, 132*cdf0e10cSrcweir IOException, 133*cdf0e10cSrcweir RuntimeException) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir return readBytes(aData,nMaxBytesToRead); 136*cdf0e10cSrcweir } 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir void SAL_CALL FTPInputStream::skipBytes(sal_Int32 nBytesToSkip) 141*cdf0e10cSrcweir throw(NotConnectedException, 142*cdf0e10cSrcweir BufferSizeExceededException, 143*cdf0e10cSrcweir IOException, 144*cdf0e10cSrcweir RuntimeException) 145*cdf0e10cSrcweir { 146*cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 147*cdf0e10cSrcweir if(!m_tmpfl) 148*cdf0e10cSrcweir throw IOException(); 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir fseek(m_tmpfl,long(nBytesToSkip),SEEK_CUR); 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir sal_Int32 SAL_CALL FTPInputStream::available(void) 156*cdf0e10cSrcweir throw(NotConnectedException, 157*cdf0e10cSrcweir IOException, 158*cdf0e10cSrcweir RuntimeException) 159*cdf0e10cSrcweir { 160*cdf0e10cSrcweir return sal::static_int_cast<sal_Int32>(m_nLength - getPosition()); 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir void SAL_CALL FTPInputStream::closeInput(void) 166*cdf0e10cSrcweir throw(NotConnectedException, 167*cdf0e10cSrcweir IOException, 168*cdf0e10cSrcweir RuntimeException) 169*cdf0e10cSrcweir { 170*cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 171*cdf0e10cSrcweir if(m_tmpfl) 172*cdf0e10cSrcweir fclose(m_tmpfl),m_tmpfl = 0; 173*cdf0e10cSrcweir } 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir void SAL_CALL FTPInputStream::seek(sal_Int64 location) 178*cdf0e10cSrcweir throw( IllegalArgumentException, 179*cdf0e10cSrcweir IOException, 180*cdf0e10cSrcweir RuntimeException ) 181*cdf0e10cSrcweir { 182*cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 183*cdf0e10cSrcweir if(!m_tmpfl) 184*cdf0e10cSrcweir throw IOException(); 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir fseek(m_tmpfl,long(location),SEEK_SET); 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir sal_Int64 SAL_CALL 192*cdf0e10cSrcweir FTPInputStream::getPosition( 193*cdf0e10cSrcweir void ) 194*cdf0e10cSrcweir throw( IOException, 195*cdf0e10cSrcweir RuntimeException ) 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 198*cdf0e10cSrcweir if(!m_tmpfl) 199*cdf0e10cSrcweir throw IOException(); 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir // fpos_t pos; 202*cdf0e10cSrcweir // fgetpos(m_tmpfl,&pos); 203*cdf0e10cSrcweir long pos; 204*cdf0e10cSrcweir pos = ftell(m_tmpfl); 205*cdf0e10cSrcweir return sal_Int64(pos); 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir sal_Int64 SAL_CALL FTPInputStream::getLength( 211*cdf0e10cSrcweir void 212*cdf0e10cSrcweir ) throw( 213*cdf0e10cSrcweir IOException,RuntimeException 214*cdf0e10cSrcweir ) 215*cdf0e10cSrcweir { 216*cdf0e10cSrcweir return m_nLength; 217*cdf0e10cSrcweir } 218