update dev300-m58
[ooovba.git] / ucb / source / ucp / webdav / NeonInputStream.cxx
blobfcc65934dba173d1f8fcab596fe98c221bfac461
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: NeonInputStream.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_ucb.hxx"
33 #include "NeonInputStream.hxx"
34 #include "DAVResourceAccess.hxx"
36 #include <rtl/memory.h>
37 #include <com/sun/star/ucb/CommandFailedException.hpp>
39 #include <comphelper/processfactory.hxx>
40 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <cstdio>
44 using namespace cppu;
45 using namespace com::sun::star::io;
46 using namespace com::sun::star;
47 using namespace webdav_ucp;
49 // -------------------------------------------------------------------
50 // Constructor
51 // -------------------------------------------------------------------
52 NeonInputStream::NeonInputStream()
53 : m_nLen( 0 ),
54 m_nPos( 0 ),
55 m_bDirty( sal_False )
59 // -------------------------------------------------------------------
60 // Destructor
61 // -------------------------------------------------------------------
62 NeonInputStream::~NeonInputStream( void )
66 // -------------------------------------------------------------------
67 // AddToStream
68 // Allows the caller to add some data to the "end" of the stream
69 // -------------------------------------------------------------------
70 void NeonInputStream::AddToStream( const char * inBuf, sal_Int32 inLen )
72 OSL_ENSURE( !m_bDirty, "Cannot AddToStream() when it was already written to it." );
74 m_aInputBuffer.realloc( sal::static_int_cast<sal_Int32>(m_nLen) + inLen );
75 rtl_copyMemory( m_aInputBuffer.getArray() + m_nLen, inBuf, inLen );
76 m_nLen += inLen;
79 // -------------------------------------------------------------------
80 // Associate a URL with this stream
81 // -------------------------------------------------------------------
82 void NeonInputStream::SetURL( const rtl::OUString &rURL )
84 osl::MutexGuard aGuard( m_aLock );
86 m_aURL = rURL;
89 // -------------------------------------------------------------------
90 // queryInterface
91 // -------------------------------------------------------------------
92 uno::Any NeonInputStream::queryInterface( const uno::Type &type )
93 throw( uno::RuntimeException )
95 uno::Any aRet = ::cppu::queryInterface( type,
96 static_cast< XStream * >( this ),
97 static_cast< XInputStream * >( this ),
98 static_cast< XOutputStream * >( this ),
99 static_cast< XSeekable * >( this ),
100 static_cast< XTruncate * >( this ) );
101 return aRet.hasValue() ? aRet : OWeakObject::queryInterface( type );
104 // -------------------------------------------------------------------
105 // getInputStream
106 // -------------------------------------------------------------------
107 com::sun::star::uno::Reference< com::sun::star::io::XInputStream > SAL_CALL
108 NeonInputStream::getInputStream( void )
109 throw( com::sun::star::uno::RuntimeException )
111 return uno::Reference< XInputStream >( this );
114 // -------------------------------------------------------------------
115 // getOutputStream
116 // -------------------------------------------------------------------
117 com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > SAL_CALL
118 NeonInputStream::getOutputStream( void )
119 throw( com::sun::star::uno::RuntimeException )
121 return uno::Reference< XOutputStream >( this );
124 // -------------------------------------------------------------------
125 // readBytes
126 // "Reads" the specified number of bytes from the stream
127 // -------------------------------------------------------------------
128 sal_Int32 SAL_CALL NeonInputStream::readBytes(
129 ::com::sun::star::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
130 throw( ::com::sun::star::io::NotConnectedException,
131 ::com::sun::star::io::BufferSizeExceededException,
132 ::com::sun::star::io::IOException,
133 ::com::sun::star::uno::RuntimeException )
135 // Work out how much we're actually going to write
136 sal_Int32 theBytes2Read = nBytesToRead;
137 sal_Int32 theBytesLeft = sal::static_int_cast<sal_Int32>(m_nLen - m_nPos);
138 if ( theBytes2Read > theBytesLeft )
139 theBytes2Read = theBytesLeft;
141 // Realloc buffer.
142 aData.realloc( theBytes2Read );
144 // Write the data
145 rtl_copyMemory(
146 aData.getArray(), m_aInputBuffer.getConstArray() + m_nPos, theBytes2Read );
148 // Update our stream position for next time
149 m_nPos += theBytes2Read;
151 return theBytes2Read;
154 // -------------------------------------------------------------------
155 // readSomeBytes
156 // -------------------------------------------------------------------
157 sal_Int32 SAL_CALL NeonInputStream::readSomeBytes(
158 ::com::sun::star::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
159 throw( ::com::sun::star::io::NotConnectedException,
160 ::com::sun::star::io::BufferSizeExceededException,
161 ::com::sun::star::io::IOException,
162 ::com::sun::star::uno::RuntimeException )
164 // Warning: What should this be doing ?
165 return readBytes( aData, nMaxBytesToRead );
168 // -------------------------------------------------------------------
169 // skipBytes
170 // Moves the current stream position forward
171 // -------------------------------------------------------------------
172 void SAL_CALL NeonInputStream::skipBytes( sal_Int32 nBytesToSkip )
173 throw( ::com::sun::star::io::NotConnectedException,
174 ::com::sun::star::io::BufferSizeExceededException,
175 ::com::sun::star::io::IOException,
176 ::com::sun::star::uno::RuntimeException )
178 m_nPos += nBytesToSkip;
179 if ( m_nPos >= m_nLen )
180 m_nPos = m_nLen;
183 // -------------------------------------------------------------------
184 // available
185 // Returns the number of unread bytes currently remaining on the stream
186 // -------------------------------------------------------------------
187 sal_Int32 SAL_CALL NeonInputStream::available( )
188 throw( ::com::sun::star::io::NotConnectedException,
189 ::com::sun::star::io::IOException,
190 ::com::sun::star::uno::RuntimeException )
192 return sal::static_int_cast<sal_Int32>(m_nLen - m_nPos);
195 // -------------------------------------------------------------------
196 // closeInput
197 // -------------------------------------------------------------------
198 void SAL_CALL NeonInputStream::closeInput( void )
199 throw( ::com::sun::star::io::NotConnectedException,
200 ::com::sun::star::io::IOException,
201 ::com::sun::star::uno::RuntimeException )
205 // -------------------------------------------------------------------
206 // seek
207 // -------------------------------------------------------------------
208 void SAL_CALL NeonInputStream::seek( sal_Int64 location )
209 throw( ::com::sun::star::lang::IllegalArgumentException,
210 ::com::sun::star::io::IOException,
211 ::com::sun::star::uno::RuntimeException )
213 if ( location < 0 )
214 throw ::com::sun::star::lang::IllegalArgumentException();
216 if ( location <= m_nLen )
217 m_nPos = location;
218 else
219 throw ::com::sun::star::lang::IllegalArgumentException();
222 // -------------------------------------------------------------------
223 // getPosition
224 // -------------------------------------------------------------------
225 sal_Int64 SAL_CALL NeonInputStream::getPosition()
226 throw( ::com::sun::star::io::IOException,
227 ::com::sun::star::uno::RuntimeException )
229 return m_nPos;
232 // -------------------------------------------------------------------
233 // getLength
234 // -------------------------------------------------------------------
235 sal_Int64 SAL_CALL NeonInputStream::getLength()
236 throw( ::com::sun::star::io::IOException,
237 ::com::sun::star::uno::RuntimeException )
239 return m_nLen;
242 // -------------------------------------------------------------------
243 // writeBytes
244 // -------------------------------------------------------------------
245 void SAL_CALL NeonInputStream::writeBytes( const com::sun::star::uno::Sequence< sal_Int8 >& aData )
246 throw( com::sun::star::io::NotConnectedException,
247 com::sun::star::io::BufferSizeExceededException,
248 com::sun::star::io::IOException,
249 com::sun::star::uno::RuntimeException)
251 #if OSL_DEBUG_LEVEL > 0
252 fprintf( stderr, "WebDAV: writeBytes()\n" );
253 #endif
255 sal_Int32 nDataLen = aData.getLength();
256 OSL_ASSERT( nDataLen >= 0 );
258 // Anything to do?
259 if ( nDataLen == 0 )
260 return;
262 // Update the length of the stream & size of the buffer
263 if ( m_nLen < m_nPos + nDataLen )
265 m_nLen = m_nPos + nDataLen;
266 if ( m_aInputBuffer.getLength() < m_nLen )
267 m_aInputBuffer.realloc( sal::static_int_cast<sal_Int32>( m_nLen ) );
270 rtl_copyMemory( m_aInputBuffer.getArray() + m_nPos, aData.getConstArray(), nDataLen );
271 m_nPos += nDataLen;
273 m_bDirty = sal_True;
276 // -------------------------------------------------------------------
277 // flush
278 // -------------------------------------------------------------------
279 void SAL_CALL NeonInputStream::flush( void )
280 throw( NotConnectedException, BufferSizeExceededException,
281 IOException, uno::RuntimeException )
283 if ( m_bDirty )
285 #if OSL_DEBUG_LEVEL > 0
286 fprintf( stderr, "WebDAV: flush(), saving the changed file.\n" );
287 #endif
288 // FIXME It's really hacky to create the new session
289 // But so far it seems I have no other chance...
290 uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY );
291 rtl::Reference< DAVSessionFactory > rDAVFactory( new DAVSessionFactory() );
293 DAVResourceAccess aResourceAccess( xFactory, rDAVFactory, m_aURL );
295 try {
296 aResourceAccess.PUT( reinterpret_cast<const char*>( m_aInputBuffer.getConstArray() ), m_nLen,
297 DAVResourceAccess::createCommandEnvironment() );
299 catch ( DAVException & e )
301 throw ucb::CommandFailedException(
302 e.getData(),
303 uno::Reference< uno::XInterface >(),
304 uno::makeAny( e.getData() ) );
307 m_bDirty = sal_False;
311 // -------------------------------------------------------------------
312 // closeOutput
313 // -------------------------------------------------------------------
314 void SAL_CALL NeonInputStream::closeOutput( void )
315 throw( com::sun::star::io::NotConnectedException,
316 com::sun::star::io::IOException,
317 com::sun::star::uno::RuntimeException )
319 if ( m_bDirty )
321 #if OSL_DEBUG_LEVEL > 0
322 fprintf( stderr, "WebDAV: TODO write on closeOutput(), the stream is dirty!\n" );
323 #endif
327 // -------------------------------------------------------------------
328 // truncate
329 // -------------------------------------------------------------------
330 void SAL_CALL NeonInputStream::truncate( void )
331 throw( com::sun::star::io::IOException,
332 com::sun::star::uno::RuntimeException )
334 #if OSL_DEBUG_LEVEL > 0
335 fprintf( stderr, "WebDAV: truncate()\n" );
336 #endif
338 if ( m_nLen > 0 )
340 m_nLen = m_nPos = 0;
341 m_bDirty = sal_True;