1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "gvfs_stream.hxx"
21 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
23 #include <libgnomevfs/gnome-vfs-ops.h>
26 using namespace com::sun::star::io
;
27 using namespace com::sun::star::uno
;
28 using namespace com::sun::star::ucb
;
31 using ::rtl::OUString
;
33 Stream::Stream( GnomeVFSHandle
*handle
,
34 const GnomeVFSFileInfo
*aInfo
) :
36 m_bInputStreamCalled( sal_False
),
37 m_bOutputStreamCalled( sal_False
)
40 gnome_vfs_file_info_copy (&m_info
, aInfo
);
43 Stream::~Stream( void )
46 gnome_vfs_close (m_handle
);
51 Any
Stream::queryInterface( const Type
&type
)
52 throw( RuntimeException
)
54 Any aRet
= ::cppu::queryInterface
56 static_cast< XStream
* >( this ),
57 static_cast< XInputStream
* >( this ),
58 static_cast< XOutputStream
* >( this ),
59 static_cast< XSeekable
* >( this ),
60 static_cast< XTruncate
* >( this ) );
62 return aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( type
);
65 // -------------------------------------------------------------------
67 // -------------------------------------------------------------------
69 com::sun::star::uno::Reference
< com::sun::star::io::XInputStream
> SAL_CALL
70 Stream::getInputStream( )
71 throw( com::sun::star::uno::RuntimeException
)
74 osl::MutexGuard
aGuard( m_aMutex
);
75 m_bInputStreamCalled
= true;
77 return Reference
< XInputStream
>( this );
80 com::sun::star::uno::Reference
< com::sun::star::io::XOutputStream
> SAL_CALL
81 Stream::getOutputStream( )
82 throw( com::sun::star::uno::RuntimeException
)
85 osl::MutexGuard
aGuard( m_aMutex
);
86 m_bOutputStreamCalled
= true;
88 return Reference
< XOutputStream
>( this );
91 // -------------------------------------------------------------------
93 // -------------------------------------------------------------------
95 sal_Int32 SAL_CALL
Stream::readBytes(
96 Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
97 throw( NotConnectedException
,
98 BufferSizeExceededException
,
102 GnomeVFSResult result
;
103 GnomeVFSFileSize nBytesRead
= 0;
114 aData
.realloc( nBytesToRead
);
115 } catch ( const Exception
&e
) {
116 throw BufferSizeExceededException();
120 result
= gnome_vfs_read( m_handle
, aData
.getArray(),
121 nBytesToRead
, &nBytesRead
);
122 } while( result
== GNOME_VFS_ERROR_INTERRUPTED
);
124 if (result
!= GNOME_VFS_OK
&&
125 result
!= GNOME_VFS_ERROR_EOF
)
126 throwOnError( result
);
128 if (result
== GNOME_VFS_ERROR_EOF
)
131 aData
.realloc( sal::static_int_cast
<sal_uInt32
>(nBytesRead
) );
133 return sal::static_int_cast
<sal_Int32
>(nBytesRead
);
136 sal_Int32 SAL_CALL
Stream::readSomeBytes(
137 Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
138 throw( NotConnectedException
,
139 BufferSizeExceededException
,
143 // Again - having 2 methods here just sucks; cf. filinpstr.cxx
144 // This can never be an effective non-blocking API - so why bother ?
145 return readBytes( aData
, nMaxBytesToRead
);
148 void SAL_CALL
Stream::skipBytes( sal_Int32 nBytesToSkip
)
149 throw( NotConnectedException
,
150 BufferSizeExceededException
,
154 GnomeVFSResult result
;
159 result
= gnome_vfs_seek( m_handle
, GNOME_VFS_SEEK_CURRENT
, nBytesToSkip
);
161 if ( result
== GNOME_VFS_ERROR_BAD_PARAMETERS
||
162 result
== GNOME_VFS_ERROR_NOT_SUPPORTED
)
163 g_warning ("FIXME: just read them in ...");
165 throwOnError( result
);
168 sal_Int32 SAL_CALL
Stream::available( )
169 throw( NotConnectedException
,
173 return 0; // cf. filinpstr.cxx
176 void SAL_CALL
Stream::closeInput( void )
177 throw( NotConnectedException
,
181 osl::MutexGuard
aGuard( m_aMutex
);
182 m_bInputStreamCalled
= false;
184 if( ! m_bOutputStreamCalled
)
188 // -------------------------------------------------------------------
190 // -------------------------------------------------------------------
192 void SAL_CALL
Stream::seek( sal_Int64 location
)
193 throw( ::com::sun::star::lang::IllegalArgumentException
,
197 GnomeVFSResult result
;
203 throw ::com::sun::star::lang::IllegalArgumentException();
206 result
= gnome_vfs_seek( m_handle
, GNOME_VFS_SEEK_START
, location
);
208 if (result
== GNOME_VFS_ERROR_EOF
)
209 throw ::com::sun::star::lang::IllegalArgumentException();
211 throwOnError( result
);
214 sal_Int64 SAL_CALL
Stream::getPosition()
218 GnomeVFSFileSize nBytesIn
= 0;
223 throwOnError( gnome_vfs_tell( m_handle
, &nBytesIn
) );
228 sal_Int64 SAL_CALL
Stream::getLength()
229 throw( IOException
, RuntimeException
)
231 // FIXME: so this sucks; it may be stale but ...
232 if (m_info
.valid_fields
& GNOME_VFS_FILE_INFO_FIELDS_SIZE
)
235 g_warning ("FIXME: No valid length");
240 // -------------------------------------------------------------------
242 // -------------------------------------------------------------------
244 void SAL_CALL
Stream::truncate( void )
245 throw( com::sun::star::io::IOException
,
246 com::sun::star::uno::RuntimeException
)
251 throwOnError( gnome_vfs_truncate_handle( m_handle
, 0 ) );
254 // -------------------------------------------------------------------
256 // -------------------------------------------------------------------
258 void SAL_CALL
Stream::writeBytes( const com::sun::star::uno::Sequence
< sal_Int8
>& aData
)
259 throw( com::sun::star::io::NotConnectedException
,
260 com::sun::star::io::BufferSizeExceededException
,
261 com::sun::star::io::IOException
,
262 com::sun::star::uno::RuntimeException
)
264 GnomeVFSResult result
= GNOME_VFS_OK
;
265 GnomeVFSFileSize toWrite
= aData
.getLength();
266 const sal_Int8
*p
= aData
.getConstArray();
271 while( toWrite
> 0) {
272 GnomeVFSFileSize bytesWritten
= 0;
274 result
= gnome_vfs_write( m_handle
, p
, toWrite
, &bytesWritten
);
275 if( result
== GNOME_VFS_ERROR_INTERRUPTED
)
277 throwOnError( result
);
278 g_assert( bytesWritten
<= toWrite
);
279 toWrite
-= bytesWritten
;
284 void SAL_CALL
Stream::flush( void )
285 throw( NotConnectedException
, BufferSizeExceededException
,
286 IOException
, RuntimeException
)
290 void SAL_CALL
Stream::closeOutput( void )
291 throw( com::sun::star::io::NotConnectedException
,
292 com::sun::star::io::IOException
,
293 com::sun::star::uno::RuntimeException
)
295 osl::MutexGuard
aGuard( m_aMutex
);
296 m_bOutputStreamCalled
= false;
298 if( ! m_bInputStreamCalled
)
302 // -------------------------------------------------------------------
304 // -------------------------------------------------------------------
306 void Stream::closeStream( void )
307 throw( ::com::sun::star::io::NotConnectedException
,
308 ::com::sun::star::io::IOException
,
309 ::com::sun::star::uno::RuntimeException
)
312 gnome_vfs_close (m_handle
);
318 void Stream::throwOnError( GnomeVFSResult result
)
319 throw( NotConnectedException
,
320 BufferSizeExceededException
,
324 if( result
!= GNOME_VFS_OK
) {
325 ::rtl::OUString aMsg
= ::rtl::OUString::createFromAscii
326 ( gnome_vfs_result_to_string( result
) );
328 g_warning( "Input Stream exceptional result '%s' (%d)",
329 gnome_vfs_result_to_string( result
), result
);
331 throw IOException( aMsg
, static_cast< cppu::OWeakObject
* >( this ) );
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */