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