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