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 						throw( RuntimeException )
76 {
77 	Any aRet = ::cppu::queryInterface( type,
78 									   static_cast< XInputStream * >( this ),
79 									   static_cast< XSeekable * >( this ) );
80 	return aRet.hasValue() ? aRet : OWeakObject::queryInterface( type );
81 }
82 
83 // -------------------------------------------------------------------
84 // readBytes
85 // "Reads" the specified number of bytes from the stream
86 // -------------------------------------------------------------------
readBytes(::com::sun::star::uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)87 sal_Int32 SAL_CALL CurlInputStream::readBytes(
88   ::com::sun::star::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
89 		throw( ::com::sun::star::io::NotConnectedException,
90 			   ::com::sun::star::io::BufferSizeExceededException,
91 			   ::com::sun::star::io::IOException,
92 			   ::com::sun::star::uno::RuntimeException )
93 {
94 	// Work out how much we're actually going to write
95 	sal_Int32 theBytes2Read = nBytesToRead;
96 	sal_Int32 theBytesLeft  = sal::static_int_cast<sal_Int32>(mLen - mPos);
97 	if ( theBytes2Read > theBytesLeft )
98 		theBytes2Read = theBytesLeft;
99 
100 	// Realloc buffer.
101 	aData.realloc( theBytes2Read );
102 
103 	// Write the data
104 	rtl_copyMemory(
105 		aData.getArray(), mInputBuffer.getConstArray() + mPos, theBytes2Read );
106 
107 	// Update our stream position for next time
108 	mPos += theBytes2Read;
109 
110     return theBytes2Read;
111 }
112 
113 // -------------------------------------------------------------------
114 // readSomeBytes
115 // -------------------------------------------------------------------
readSomeBytes(::com::sun::star::uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)116 sal_Int32 SAL_CALL CurlInputStream::readSomeBytes(
117  ::com::sun::star::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
118         throw( ::com::sun::star::io::NotConnectedException,
119                ::com::sun::star::io::BufferSizeExceededException,
120                ::com::sun::star::io::IOException,
121                ::com::sun::star::uno::RuntimeException )
122 {
123 	// Warning: What should this be doing ?
124 	return readBytes( aData, nMaxBytesToRead );
125 }
126 
127 // -------------------------------------------------------------------
128 // skipBytes
129 // Moves the current stream position forward
130 // -------------------------------------------------------------------
skipBytes(sal_Int32 nBytesToSkip)131 void SAL_CALL CurlInputStream::skipBytes( sal_Int32 nBytesToSkip )
132         throw( ::com::sun::star::io::NotConnectedException,
133                ::com::sun::star::io::BufferSizeExceededException,
134                ::com::sun::star::io::IOException,
135                ::com::sun::star::uno::RuntimeException )
136 {
137 	mPos += nBytesToSkip;
138 	if ( mPos >= mLen )
139 		mPos = mLen;
140 }
141 
142 // -------------------------------------------------------------------
143 // available
144 // Returns the number of unread bytes currently remaining on the stream
145 // -------------------------------------------------------------------
available()146 sal_Int32 SAL_CALL CurlInputStream::available(  )
147         throw( ::com::sun::star::io::NotConnectedException,
148                ::com::sun::star::io::IOException,
149                ::com::sun::star::uno::RuntimeException )
150 {
151 	return sal::static_int_cast<sal_Int32>(mLen - mPos);
152 }
153 
154 // -------------------------------------------------------------------
155 // closeInput
156 // -------------------------------------------------------------------
closeInput(void)157 void SAL_CALL CurlInputStream::closeInput( void )
158  		throw( ::com::sun::star::io::NotConnectedException,
159        		   ::com::sun::star::io::IOException,
160        		   ::com::sun::star::uno::RuntimeException )
161 {
162 }
163 
164 // -------------------------------------------------------------------
165 // seek
166 // -------------------------------------------------------------------
seek(sal_Int64 location)167 void SAL_CALL CurlInputStream::seek( sal_Int64 location )
168 		throw( ::com::sun::star::lang::IllegalArgumentException,
169 			   ::com::sun::star::io::IOException,
170 			   ::com::sun::star::uno::RuntimeException )
171 {
172 	if ( location < 0 )
173 		throw ::com::sun::star::lang::IllegalArgumentException();
174 
175     if ( location <= mLen )
176 		mPos = location;
177 	else
178         throw ::com::sun::star::lang::IllegalArgumentException();
179 }
180 
181 // -------------------------------------------------------------------
182 // getPosition
183 // -------------------------------------------------------------------
getPosition()184 sal_Int64 SAL_CALL CurlInputStream::getPosition()
185 		throw( ::com::sun::star::io::IOException,
186 			   ::com::sun::star::uno::RuntimeException )
187 {
188 	return mPos;
189 }
190 
191 // -------------------------------------------------------------------
192 // getLength
193 // -------------------------------------------------------------------
getLength()194 sal_Int64 SAL_CALL CurlInputStream::getLength()
195 		throw( ::com::sun::star::io::IOException,
196 			   ::com::sun::star::uno::RuntimeException )
197 {
198 	return mLen;
199 }
200