1 --- ucb/source/ucp/gvfs/stream.hxx 2003-04-24 16:24:35.000000000 +0100
2 +++ ucb/source/ucp/gvfs/stream.hxx 2003-04-24 16:24:35.000000000 +0100
4 +#ifndef _GVFSSTREAM_HXX_
5 +#define _GVFSSTREAM_HXX_
8 +#include <sal/types.h>
10 +#ifndef _RTL_USTRING_HXX_
11 +#include <rtl/ustring.hxx>
13 +#ifndef _CPPUHELPER_WEAK_HXX_
14 +#include <cppuhelper/weak.hxx>
17 +#ifndef _COM_SUN_STAR_IO_XSTREAM_HPP_
18 +#include <com/sun/star/io/XStream.hpp>
20 +#ifndef _COM_SUN_STAR_IO_XINPUTSTREAM_HPP_
21 +#include <com/sun/star/io/XInputStream.hpp>
23 +#ifndef _COM_SUN_STAR_IO_XOUTPUTSTREAM_HPP_
24 +#include <com/sun/star/io/XOutputStream.hpp>
26 +#ifndef _COM_SUN_STAR_IO_XTRUNCATE_HPP_
27 +#include <com/sun/star/io/XTruncate.hpp>
29 +#ifndef _COM_SUN_STAR_IO_XSEEKABLE_HPP_
30 +#include <com/sun/star/io/XSeekable.hpp>
33 +#include <libgnomevfs/gnome-vfs-handle.h>
38 +class Stream : public ::com::sun::star::io::XStream,
39 + public ::com::sun::star::io::XInputStream,
40 + public ::com::sun::star::io::XOutputStream,
41 + public ::com::sun::star::io::XTruncate,
42 + public ::com::sun::star::io::XSeekable,
43 + public ::cppu::OWeakObject
46 + GnomeVFSHandle *m_handle;
47 + GnomeVFSFileInfo m_info;
48 + osl::Mutex m_aMutex;
50 + sal_Bool m_bInputStreamCalled;
51 + sal_Bool m_bOutputStreamCalled;
53 + void throwOnError( GnomeVFSResult result )
54 + throw( ::com::sun::star::io::NotConnectedException,
55 + ::com::sun::star::io::BufferSizeExceededException,
56 + ::com::sun::star::io::IOException,
57 + ::com::sun::star::uno::RuntimeException );
59 + void closeStream( void )
60 + throw( ::com::sun::star::io::NotConnectedException,
61 + ::com::sun::star::io::IOException,
62 + ::com::sun::star::uno::RuntimeException );
65 + Stream ( GnomeVFSHandle *handle,
66 + const GnomeVFSFileInfo *aInfo );
70 + virtual com::sun::star::uno::Any SAL_CALL queryInterface(const ::com::sun::star::uno::Type & type )
71 + throw( ::com::sun::star::uno::RuntimeException );
72 + virtual void SAL_CALL acquire( void )
74 + { OWeakObject::acquire(); }
75 + virtual void SAL_CALL release( void )
77 + { OWeakObject::release(); }
80 + virtual com::sun::star::uno::Reference< com::sun::star::io::XInputStream > SAL_CALL getInputStream( )
81 + throw( com::sun::star::uno::RuntimeException );
83 + virtual com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > SAL_CALL getOutputStream( )
84 + throw( com::sun::star::uno::RuntimeException );
87 + virtual sal_Int32 SAL_CALL readBytes(
88 + ::com::sun::star::uno::Sequence< sal_Int8 > & aData,
89 + sal_Int32 nBytesToRead )
90 + throw( ::com::sun::star::io::NotConnectedException,
91 + ::com::sun::star::io::BufferSizeExceededException,
92 + ::com::sun::star::io::IOException,
93 + ::com::sun::star::uno::RuntimeException );
95 + virtual sal_Int32 SAL_CALL readSomeBytes(
96 + ::com::sun::star::uno::Sequence< sal_Int8 > & aData,
97 + sal_Int32 nMaxBytesToRead )
98 + throw( ::com::sun::star::io::NotConnectedException,
99 + ::com::sun::star::io::BufferSizeExceededException,
100 + ::com::sun::star::io::IOException,
101 + ::com::sun::star::uno::RuntimeException );
103 + virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
104 + throw( ::com::sun::star::io::NotConnectedException,
105 + ::com::sun::star::io::BufferSizeExceededException,
106 + ::com::sun::star::io::IOException,
107 + ::com::sun::star::uno::RuntimeException );
109 + virtual sal_Int32 SAL_CALL available( void )
110 + throw( ::com::sun::star::io::NotConnectedException,
111 + ::com::sun::star::io::IOException,
112 + ::com::sun::star::uno::RuntimeException );
114 + virtual void SAL_CALL closeInput( void )
115 + throw( ::com::sun::star::io::NotConnectedException,
116 + ::com::sun::star::io::IOException,
117 + ::com::sun::star::uno::RuntimeException );
120 + virtual void SAL_CALL seek( sal_Int64 location )
121 + throw( ::com::sun::star::lang::IllegalArgumentException,
122 + ::com::sun::star::io::IOException,
123 + ::com::sun::star::uno::RuntimeException );
125 + virtual sal_Int64 SAL_CALL getPosition()
126 + throw( ::com::sun::star::io::IOException,
127 + ::com::sun::star::uno::RuntimeException );
129 + virtual sal_Int64 SAL_CALL getLength()
130 + throw( ::com::sun::star::io::IOException,
131 + ::com::sun::star::uno::RuntimeException );
134 + virtual void SAL_CALL writeBytes( const com::sun::star::uno::Sequence< sal_Int8 >& aData )
135 + throw( com::sun::star::io::NotConnectedException,
136 + com::sun::star::io::BufferSizeExceededException,
137 + com::sun::star::io::IOException,
138 + com::sun::star::uno::RuntimeException);
140 + virtual void SAL_CALL flush( void )
141 + throw( com::sun::star::io::NotConnectedException,
142 + com::sun::star::io::BufferSizeExceededException,
143 + com::sun::star::io::IOException,
144 + com::sun::star::uno::RuntimeException);
147 + virtual void SAL_CALL closeOutput( void )
148 + throw( com::sun::star::io::NotConnectedException,
149 + com::sun::star::io::IOException,
150 + com::sun::star::uno::RuntimeException );
153 + virtual void SAL_CALL truncate( void )
154 + throw( com::sun::star::io::IOException,
155 + com::sun::star::uno::RuntimeException );
158 +}; // namespace gvfs
159 +#endif // _GVFSSTREAM_HXX_
160 --- ucb/source/ucp/gvfs/stream.cxx 2003-04-24 16:24:35.000000000 +0100
161 +++ ucb/source/ucp/gvfs/stream.cxx 2003-04-24 16:24:35.000000000 +0100
163 +#include "stream.hxx"
165 +#ifndef _RTL_MEMORY_H_
166 +#include <rtl/memory.h>
168 +#ifndef _COM_SUN_STAR_UCB_INTERACTIVEAUGMENTEDIOEXCEPTION_HPP_
169 +#include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
172 +#include <libgnomevfs/gnome-vfs-ops.h>
174 +using namespace cppu;
175 +using namespace rtl;
176 +using namespace com::sun::star::io;
177 +using namespace com::sun::star::uno;
178 +using namespace com::sun::star::ucb;
179 +using namespace gvfs;
181 +Stream::Stream( GnomeVFSHandle *handle,
182 + const GnomeVFSFileInfo *aInfo ) :
184 + m_bInputStreamCalled( sal_False ),
185 + m_bOutputStreamCalled( sal_False )
188 + gnome_vfs_file_info_copy (&m_info, aInfo);
191 +Stream::~Stream( void )
194 + gnome_vfs_close (m_handle);
199 +Any Stream::queryInterface( const Type &type )
200 + throw( RuntimeException )
202 + Any aRet = ::cppu::queryInterface
204 + static_cast< XStream * >( this ),
205 + static_cast< XInputStream * >( this ),
206 + static_cast< XOutputStream * >( this ),
207 + static_cast< XSeekable * >( this ),
208 + static_cast< XTruncate * >( this ) );
210 + return aRet.hasValue() ? aRet : OWeakObject::queryInterface( type );
213 +// -------------------------------------------------------------------
215 +// -------------------------------------------------------------------
217 +com::sun::star::uno::Reference< com::sun::star::io::XInputStream > SAL_CALL
218 +Stream::getInputStream( )
219 + throw( com::sun::star::uno::RuntimeException )
222 + osl::MutexGuard aGuard( m_aMutex );
223 + m_bInputStreamCalled = true;
225 + return Reference< XInputStream >( this );
228 +com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > SAL_CALL
229 +Stream::getOutputStream( )
230 + throw( com::sun::star::uno::RuntimeException )
233 + osl::MutexGuard aGuard( m_aMutex );
234 + m_bOutputStreamCalled = true;
236 + return Reference< XOutputStream >( this );
239 +// -------------------------------------------------------------------
241 +// -------------------------------------------------------------------
243 +sal_Int32 SAL_CALL Stream::readBytes(
244 + Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
245 + throw( NotConnectedException,
246 + BufferSizeExceededException,
250 + GnomeVFSResult result;
251 + GnomeVFSFileSize nBytesRead = 0;
254 + throw IOException();
257 + aData.realloc( 0 );
262 + aData.realloc( nBytesToRead );
263 + } catch ( const Exception &e ) {
264 + throw BufferSizeExceededException();
268 + result = gnome_vfs_read( m_handle, aData.getArray(),
269 + nBytesToRead, &nBytesRead );
270 + } while( result == GNOME_VFS_ERROR_INTERRUPTED );
272 + if (result != GNOME_VFS_OK &&
273 + result != GNOME_VFS_ERROR_EOF)
274 + throwOnError( result );
276 + if (result == GNOME_VFS_ERROR_EOF)
279 + aData.realloc( nBytesRead );
284 +sal_Int32 SAL_CALL Stream::readSomeBytes(
285 + Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
286 + throw( NotConnectedException,
287 + BufferSizeExceededException,
291 + // Again - having 2 methods here just sucks; cf. filinpstr.cxx
292 + // This can never be an effective non-blocking API - so why bother ?
293 + return readSomeBytes( aData, nMaxBytesToRead );
296 +void SAL_CALL Stream::skipBytes( sal_Int32 nBytesToSkip )
297 + throw( NotConnectedException,
298 + BufferSizeExceededException,
302 + GnomeVFSResult result;
305 + throw IOException();
307 + result = gnome_vfs_seek( m_handle, GNOME_VFS_SEEK_CURRENT, nBytesToSkip );
309 + if ( result == GNOME_VFS_ERROR_BAD_PARAMETERS ||
310 + result == GNOME_VFS_ERROR_NOT_SUPPORTED )
311 + g_warning ("FIXME: just read them in ...");
313 + throwOnError( result );
316 +sal_Int32 SAL_CALL Stream::available( )
317 + throw( NotConnectedException,
321 + return 0; // cf. filinpstr.cxx
324 +void SAL_CALL Stream::closeInput( void )
325 + throw( NotConnectedException,
329 + osl::MutexGuard aGuard( m_aMutex );
330 + m_bInputStreamCalled = false;
332 + if( ! m_bOutputStreamCalled )
336 +// -------------------------------------------------------------------
338 +// -------------------------------------------------------------------
340 +void SAL_CALL Stream::seek( sal_Int64 location )
341 + throw( ::com::sun::star::lang::IllegalArgumentException,
345 + GnomeVFSResult result;
348 + throw IOException();
350 + if ( location < 0 )
351 + throw ::com::sun::star::lang::IllegalArgumentException();
354 + result = gnome_vfs_seek( m_handle, GNOME_VFS_SEEK_START, location );
356 + if (result == GNOME_VFS_ERROR_EOF)
357 + throw ::com::sun::star::lang::IllegalArgumentException();
359 + throwOnError( result );
362 +sal_Int64 SAL_CALL Stream::getPosition()
363 + throw( IOException,
366 + GnomeVFSFileSize nBytesIn = 0;
369 + throw IOException();
371 + throwOnError( gnome_vfs_tell( m_handle, &nBytesIn ) );
376 +sal_Int64 SAL_CALL Stream::getLength()
377 + throw( IOException, RuntimeException )
379 + // FIXME: so this sucks; it may be stale but ...
380 + if (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE)
381 + return m_info.size;
383 + g_warning ("FIXME: No valid length");
388 +// -------------------------------------------------------------------
390 +// -------------------------------------------------------------------
392 +void SAL_CALL Stream::truncate( void )
393 + throw( com::sun::star::io::IOException,
394 + com::sun::star::uno::RuntimeException )
397 + throw IOException();
399 + throwOnError( gnome_vfs_truncate_handle( m_handle, 0 ) );
402 +// -------------------------------------------------------------------
404 +// -------------------------------------------------------------------
406 +void SAL_CALL Stream::writeBytes( const com::sun::star::uno::Sequence< sal_Int8 >& aData )
407 + throw( com::sun::star::io::NotConnectedException,
408 + com::sun::star::io::BufferSizeExceededException,
409 + com::sun::star::io::IOException,
410 + com::sun::star::uno::RuntimeException)
412 + GnomeVFSResult result = GNOME_VFS_OK;
413 + GnomeVFSFileSize toWrite = aData.getLength();
414 + const sal_Int8 *p = aData.getConstArray();
417 + throw IOException();
419 + while( toWrite > 0) {
420 + GnomeVFSFileSize bytesWritten = 0;
422 + result = gnome_vfs_write( m_handle, p, toWrite, &bytesWritten );
423 + if( result == GNOME_VFS_ERROR_INTERRUPTED )
425 + throwOnError( result );
426 + g_assert( bytesWritten <= toWrite );
427 + toWrite -= bytesWritten;
432 +void SAL_CALL Stream::flush( void )
433 + throw( NotConnectedException, BufferSizeExceededException,
434 + IOException, RuntimeException )
438 +void SAL_CALL Stream::closeOutput( void )
439 + throw( com::sun::star::io::NotConnectedException,
440 + com::sun::star::io::IOException,
441 + com::sun::star::uno::RuntimeException )
443 + osl::MutexGuard aGuard( m_aMutex );
444 + m_bOutputStreamCalled = false;
446 + if( ! m_bInputStreamCalled )
450 +// -------------------------------------------------------------------
452 +// -------------------------------------------------------------------
454 +void Stream::closeStream( void )
455 + throw( ::com::sun::star::io::NotConnectedException,
456 + ::com::sun::star::io::IOException,
457 + ::com::sun::star::uno::RuntimeException )
460 + gnome_vfs_close (m_handle);
463 + throw IOException();
466 +void Stream::throwOnError( GnomeVFSResult result )
467 + throw( NotConnectedException,
468 + BufferSizeExceededException,
472 + if( result != GNOME_VFS_OK ) {
473 + ::rtl::OUString aMsg = ::rtl::OUString::createFromAscii
474 + ( gnome_vfs_result_to_string( result ) );
476 + g_warning( "Input Stream exceptional result '%s' (%d)",
477 + gnome_vfs_result_to_string( result ), result );
479 + throw IOException( aMsg, static_cast< cppu::OWeakObject * >( this ) );