xref: /trunk/main/ucb/source/ucp/ftp/ftpinpstr.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 {
73     osl::MutexGuard aGuard(m_aMutex);
74 
75     sal_uInt64 nBeforePos( 0 );
76     sal_uInt64 nBytesRequested( nBytesToRead );
77     sal_uInt64 nBytesRead( 0 );
78 
79     osl_getFilePos( m_tmpfl, &nBeforePos );
80 
81     if ( 0 == ( nBytesRequested = std::min< sal_uInt64 >( m_nLength - nBeforePos, nBytesRequested ) ) )
82         return 0;
83 
84     if ( 0 <= nBytesToRead && aData.getLength() < nBytesToRead )
85         aData.realloc( nBytesToRead );
86 
87     if ( osl_readFile( m_tmpfl, aData.getArray(), nBytesRequested, &nBytesRead ) != osl_File_E_None )
88         throw IOException();
89 
90     return sal_Int32( nBytesRead );
91 }
92 
93 
readSomeBytes(Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)94 sal_Int32 SAL_CALL FTPInputStream::readSomeBytes( Sequence< sal_Int8 >& aData,
95                                                   sal_Int32 nMaxBytesToRead )
96 {
97     return readBytes(aData,nMaxBytesToRead);
98 }
99 
100 
101 
skipBytes(sal_Int32 nBytesToSkip)102 void SAL_CALL FTPInputStream::skipBytes(sal_Int32 nBytesToSkip)
103 {
104     osl::MutexGuard aGuard(m_aMutex);
105     if(!m_tmpfl)
106         throw IOException();
107 
108     osl_setFilePos( m_tmpfl, osl_Pos_Current, nBytesToSkip );
109 }
110 
111 
112 
available(void)113 sal_Int32 SAL_CALL FTPInputStream::available(void)
114 {
115     return sal::static_int_cast<sal_Int32>(m_nLength - getPosition());
116 }
117 
118 
119 
closeInput(void)120 void SAL_CALL FTPInputStream::closeInput(void)
121 {
122     osl::MutexGuard aGuard(m_aMutex);
123     if(m_tmpfl)
124         osl_closeFile(m_tmpfl),m_tmpfl = 0;
125 }
126 
127 
128 
seek(sal_Int64 location)129 void SAL_CALL FTPInputStream::seek(sal_Int64 location)
130 {
131     osl::MutexGuard aGuard(m_aMutex);
132     if(!m_tmpfl)
133         throw IOException();
134 
135     osl_setFilePos( m_tmpfl, osl_Pos_Absolut, location );
136 }
137 
138 
139 
140 sal_Int64 SAL_CALL
getPosition(void)141 FTPInputStream::getPosition(
142     void )
143 {
144     osl::MutexGuard aGuard(m_aMutex);
145     if(!m_tmpfl)
146         throw IOException();
147 
148     sal_uInt64 nFilePos = 0;
149     osl_getFilePos( m_tmpfl, &nFilePos );
150     return nFilePos;
151 }
152 
153 
154 
getLength(void)155 sal_Int64 SAL_CALL FTPInputStream::getLength(
156     void
157 )
158 {
159     return m_nLength;
160 }
161