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