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