xref: /trunk/main/ucb/source/ucp/webdav/CurlInputStream.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_webdav.hxx"
26 #include "CurlInputStream.hxx"
27 #include <rtl/memory.h>
28 
29 using namespace cppu;
30 using namespace rtl;
31 using namespace com::sun::star::io;
32 using namespace com::sun::star::uno;
33 using namespace http_dav_ucp;
34 
35 
36 // -------------------------------------------------------------------
37 // Constructor
38 // -------------------------------------------------------------------
CurlInputStream(void)39 CurlInputStream::CurlInputStream( void )
40 : mLen( 0 ),
41   mPos( 0 ),
42   mCapacity( 0 )
43 {
44 }
45 
46 // -------------------------------------------------------------------
47 // Destructor
48 // -------------------------------------------------------------------
~CurlInputStream(void)49 CurlInputStream::~CurlInputStream( void )
50 {
51 }
52 
53 // -------------------------------------------------------------------
54 // AddToStream
55 // Allows the caller to add some data to the "end" of the stream
56 // -------------------------------------------------------------------
AddToStream(const char * inBuf,sal_Int32 inLen)57 void CurlInputStream::AddToStream( const char * inBuf, sal_Int32 inLen )
58 {
59     if ( mLen + inLen > mCapacity )
60     {
61         if ( 2*mCapacity >= ( mLen + inLen ) )
62             mCapacity *= 2;
63         else
64             mCapacity = mLen + inLen;
65         mInputBuffer.realloc( mCapacity );
66     }
67     rtl_copyMemory( mInputBuffer.getArray() + mLen, inBuf, inLen );
68     mLen += inLen;
69 }
70 
71 // -------------------------------------------------------------------
72 // queryInterface
73 // -------------------------------------------------------------------
queryInterface(const Type & type)74 Any CurlInputStream::queryInterface( const Type &type )
75 {
76     Any aRet = ::cppu::queryInterface( type,
77                                        static_cast< XInputStream * >( this ),
78                                        static_cast< XSeekable * >( this ) );
79     return aRet.hasValue() ? aRet : OWeakObject::queryInterface( type );
80 }
81 
82 // -------------------------------------------------------------------
83 // readBytes
84 // "Reads" the specified number of bytes from the stream
85 // -------------------------------------------------------------------
readBytes(::com::sun::star::uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)86 sal_Int32 SAL_CALL CurlInputStream::readBytes(
87   ::com::sun::star::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
88 {
89     // Work out how much we're actually going to write
90     sal_Int32 theBytes2Read = nBytesToRead;
91     sal_Int32 theBytesLeft  = sal::static_int_cast<sal_Int32>(mLen - mPos);
92     if ( theBytes2Read > theBytesLeft )
93         theBytes2Read = theBytesLeft;
94 
95     // Realloc buffer.
96     aData.realloc( theBytes2Read );
97 
98     // Write the data
99     rtl_copyMemory(
100         aData.getArray(), mInputBuffer.getConstArray() + mPos, theBytes2Read );
101 
102     // Update our stream position for next time
103     mPos += theBytes2Read;
104 
105     return theBytes2Read;
106 }
107 
108 // -------------------------------------------------------------------
109 // readSomeBytes
110 // -------------------------------------------------------------------
readSomeBytes(::com::sun::star::uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)111 sal_Int32 SAL_CALL CurlInputStream::readSomeBytes(
112  ::com::sun::star::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
113 {
114     // Warning: What should this be doing ?
115     return readBytes( aData, nMaxBytesToRead );
116 }
117 
118 // -------------------------------------------------------------------
119 // skipBytes
120 // Moves the current stream position forward
121 // -------------------------------------------------------------------
skipBytes(sal_Int32 nBytesToSkip)122 void SAL_CALL CurlInputStream::skipBytes( sal_Int32 nBytesToSkip )
123 {
124     mPos += nBytesToSkip;
125     if ( mPos >= mLen )
126         mPos = mLen;
127 }
128 
129 // -------------------------------------------------------------------
130 // available
131 // Returns the number of unread bytes currently remaining on the stream
132 // -------------------------------------------------------------------
available()133 sal_Int32 SAL_CALL CurlInputStream::available(  )
134 {
135     return sal::static_int_cast<sal_Int32>(mLen - mPos);
136 }
137 
138 // -------------------------------------------------------------------
139 // closeInput
140 // -------------------------------------------------------------------
closeInput(void)141 void SAL_CALL CurlInputStream::closeInput( void )
142 {
143 }
144 
145 // -------------------------------------------------------------------
146 // seek
147 // -------------------------------------------------------------------
seek(sal_Int64 location)148 void SAL_CALL CurlInputStream::seek( sal_Int64 location )
149 {
150     if ( location < 0 )
151         throw ::com::sun::star::lang::IllegalArgumentException();
152 
153     if ( location <= mLen )
154         mPos = location;
155     else
156         throw ::com::sun::star::lang::IllegalArgumentException();
157 }
158 
159 // -------------------------------------------------------------------
160 // getPosition
161 // -------------------------------------------------------------------
getPosition()162 sal_Int64 SAL_CALL CurlInputStream::getPosition()
163 {
164     return mPos;
165 }
166 
167 // -------------------------------------------------------------------
168 // getLength
169 // -------------------------------------------------------------------
getLength()170 sal_Int64 SAL_CALL CurlInputStream::getLength()
171 {
172     return mLen;
173 }
174