Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "gvfs_stream.hxx"
30 : : #include <rtl/memory.h>
31 : : #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
32 : :
33 : : #include <libgnomevfs/gnome-vfs-ops.h>
34 : :
35 : : using namespace cppu;
36 : : using namespace com::sun::star::io;
37 : : using namespace com::sun::star::uno;
38 : : using namespace com::sun::star::ucb;
39 : : using namespace gvfs;
40 : :
41 : : using ::rtl::OUString;
42 : :
43 : 0 : Stream::Stream( GnomeVFSHandle *handle,
44 : : const GnomeVFSFileInfo *aInfo ) :
45 : : m_eof (sal_False),
46 : : m_bInputStreamCalled( sal_False ),
47 : 0 : m_bOutputStreamCalled( sal_False )
48 : : {
49 : 0 : m_handle = handle;
50 : 0 : gnome_vfs_file_info_copy (&m_info, aInfo);
51 : 0 : }
52 : :
53 : 0 : Stream::~Stream( void )
54 : : {
55 : 0 : if (m_handle) {
56 : 0 : gnome_vfs_close (m_handle);
57 : 0 : m_handle = NULL;
58 : : }
59 : 0 : }
60 : :
61 : 0 : Any Stream::queryInterface( const Type &type )
62 : : throw( RuntimeException )
63 : : {
64 : : Any aRet = ::cppu::queryInterface
65 : : ( type,
66 : : static_cast< XStream * >( this ),
67 : : static_cast< XInputStream * >( this ),
68 : : static_cast< XOutputStream * >( this ),
69 : : static_cast< XSeekable * >( this ),
70 : 0 : static_cast< XTruncate * >( this ) );
71 : :
72 : 0 : return aRet.hasValue() ? aRet : OWeakObject::queryInterface( type );
73 : : }
74 : :
75 : : // -------------------------------------------------------------------
76 : : // XStream
77 : : // -------------------------------------------------------------------
78 : :
79 : : com::sun::star::uno::Reference< com::sun::star::io::XInputStream > SAL_CALL
80 : 0 : Stream::getInputStream( )
81 : : throw( com::sun::star::uno::RuntimeException )
82 : : {
83 : : {
84 : 0 : osl::MutexGuard aGuard( m_aMutex );
85 : 0 : m_bInputStreamCalled = true;
86 : : }
87 : 0 : return Reference< XInputStream >( this );
88 : : }
89 : :
90 : : com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > SAL_CALL
91 : 0 : Stream::getOutputStream( )
92 : : throw( com::sun::star::uno::RuntimeException )
93 : : {
94 : : {
95 : 0 : osl::MutexGuard aGuard( m_aMutex );
96 : 0 : m_bOutputStreamCalled = true;
97 : : }
98 : 0 : return Reference< XOutputStream >( this );
99 : : }
100 : :
101 : : // -------------------------------------------------------------------
102 : : // XInputStream
103 : : // -------------------------------------------------------------------
104 : :
105 : 0 : sal_Int32 SAL_CALL Stream::readBytes(
106 : : Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
107 : : throw( NotConnectedException,
108 : : BufferSizeExceededException,
109 : : IOException,
110 : : RuntimeException )
111 : : {
112 : : GnomeVFSResult result;
113 : 0 : GnomeVFSFileSize nBytesRead = 0;
114 : :
115 : 0 : if( ! m_handle )
116 : 0 : throw IOException();
117 : :
118 : 0 : if( m_eof ) {
119 : 0 : aData.realloc( 0 );
120 : 0 : return 0;
121 : : }
122 : :
123 : : try {
124 : 0 : aData.realloc( nBytesToRead );
125 : 0 : } catch ( const Exception &e ) {
126 : 0 : throw BufferSizeExceededException();
127 : : }
128 : :
129 : 0 : do {
130 : 0 : result = gnome_vfs_read( m_handle, aData.getArray(),
131 : 0 : nBytesToRead, &nBytesRead );
132 : : } while( result == GNOME_VFS_ERROR_INTERRUPTED );
133 : :
134 : 0 : if (result != GNOME_VFS_OK &&
135 : : result != GNOME_VFS_ERROR_EOF)
136 : 0 : throwOnError( result );
137 : :
138 : 0 : if (result == GNOME_VFS_ERROR_EOF)
139 : 0 : m_eof = sal_True;
140 : :
141 : 0 : aData.realloc( sal::static_int_cast<sal_uInt32>(nBytesRead) );
142 : :
143 : 0 : return sal::static_int_cast<sal_Int32>(nBytesRead);
144 : : }
145 : :
146 : 0 : sal_Int32 SAL_CALL Stream::readSomeBytes(
147 : : Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
148 : : throw( NotConnectedException,
149 : : BufferSizeExceededException,
150 : : IOException,
151 : : RuntimeException )
152 : : {
153 : : // Again - having 2 methods here just sucks; cf. filinpstr.cxx
154 : : // This can never be an effective non-blocking API - so why bother ?
155 : 0 : return readBytes( aData, nMaxBytesToRead );
156 : : }
157 : :
158 : 0 : void SAL_CALL Stream::skipBytes( sal_Int32 nBytesToSkip )
159 : : throw( NotConnectedException,
160 : : BufferSizeExceededException,
161 : : IOException,
162 : : RuntimeException )
163 : : {
164 : : GnomeVFSResult result;
165 : :
166 : 0 : if( ! m_handle )
167 : 0 : throw IOException();
168 : :
169 : 0 : result = gnome_vfs_seek( m_handle, GNOME_VFS_SEEK_CURRENT, nBytesToSkip );
170 : :
171 : 0 : if ( result == GNOME_VFS_ERROR_BAD_PARAMETERS ||
172 : : result == GNOME_VFS_ERROR_NOT_SUPPORTED )
173 : 0 : g_warning ("FIXME: just read them in ...");
174 : :
175 : 0 : throwOnError( result );
176 : 0 : }
177 : :
178 : 0 : sal_Int32 SAL_CALL Stream::available( )
179 : : throw( NotConnectedException,
180 : : IOException,
181 : : RuntimeException )
182 : : {
183 : 0 : return 0; // cf. filinpstr.cxx
184 : : }
185 : :
186 : 0 : void SAL_CALL Stream::closeInput( void )
187 : : throw( NotConnectedException,
188 : : IOException,
189 : : RuntimeException )
190 : : {
191 : 0 : osl::MutexGuard aGuard( m_aMutex );
192 : 0 : m_bInputStreamCalled = false;
193 : :
194 : 0 : if( ! m_bOutputStreamCalled )
195 : 0 : closeStream();
196 : 0 : }
197 : :
198 : : // -------------------------------------------------------------------
199 : : // XSeekable
200 : : // -------------------------------------------------------------------
201 : :
202 : 0 : void SAL_CALL Stream::seek( sal_Int64 location )
203 : : throw( ::com::sun::star::lang::IllegalArgumentException,
204 : : IOException,
205 : : RuntimeException )
206 : : {
207 : : GnomeVFSResult result;
208 : :
209 : 0 : if( ! m_handle )
210 : 0 : throw IOException();
211 : :
212 : 0 : if ( location < 0 )
213 : 0 : throw ::com::sun::star::lang::IllegalArgumentException();
214 : :
215 : 0 : m_eof = sal_False;
216 : 0 : result = gnome_vfs_seek( m_handle, GNOME_VFS_SEEK_START, location );
217 : :
218 : 0 : if (result == GNOME_VFS_ERROR_EOF)
219 : 0 : throw ::com::sun::star::lang::IllegalArgumentException();
220 : :
221 : 0 : throwOnError( result );
222 : 0 : }
223 : :
224 : 0 : sal_Int64 SAL_CALL Stream::getPosition()
225 : : throw( IOException,
226 : : RuntimeException )
227 : : {
228 : 0 : GnomeVFSFileSize nBytesIn = 0;
229 : :
230 : 0 : if( ! m_handle )
231 : 0 : throw IOException();
232 : :
233 : 0 : throwOnError( gnome_vfs_tell( m_handle, &nBytesIn ) );
234 : :
235 : 0 : return nBytesIn;
236 : : }
237 : :
238 : 0 : sal_Int64 SAL_CALL Stream::getLength()
239 : : throw( IOException, RuntimeException )
240 : : {
241 : : // FIXME: so this sucks; it may be stale but ...
242 : 0 : if (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE)
243 : 0 : return m_info.size;
244 : : else {
245 : 0 : g_warning ("FIXME: No valid length");
246 : 0 : return 0;
247 : : }
248 : : }
249 : :
250 : : // -------------------------------------------------------------------
251 : : // XTruncate
252 : : // -------------------------------------------------------------------
253 : :
254 : 0 : void SAL_CALL Stream::truncate( void )
255 : : throw( com::sun::star::io::IOException,
256 : : com::sun::star::uno::RuntimeException )
257 : : {
258 : 0 : if( ! m_handle )
259 : 0 : throw IOException();
260 : :
261 : 0 : throwOnError( gnome_vfs_truncate_handle( m_handle, 0 ) );
262 : 0 : }
263 : :
264 : : // -------------------------------------------------------------------
265 : : // XOutputStream
266 : : // -------------------------------------------------------------------
267 : :
268 : 0 : void SAL_CALL Stream::writeBytes( const com::sun::star::uno::Sequence< sal_Int8 >& aData )
269 : : throw( com::sun::star::io::NotConnectedException,
270 : : com::sun::star::io::BufferSizeExceededException,
271 : : com::sun::star::io::IOException,
272 : : com::sun::star::uno::RuntimeException)
273 : : {
274 : 0 : GnomeVFSResult result = GNOME_VFS_OK;
275 : 0 : GnomeVFSFileSize toWrite = aData.getLength();
276 : 0 : const sal_Int8 *p = aData.getConstArray();
277 : :
278 : 0 : if( ! m_handle )
279 : 0 : throw IOException();
280 : :
281 : 0 : while( toWrite > 0) {
282 : 0 : GnomeVFSFileSize bytesWritten = 0;
283 : :
284 : 0 : result = gnome_vfs_write( m_handle, p, toWrite, &bytesWritten );
285 : 0 : if( result == GNOME_VFS_ERROR_INTERRUPTED )
286 : 0 : continue;
287 : 0 : throwOnError( result );
288 : 0 : g_assert( bytesWritten <= toWrite );
289 : 0 : toWrite -= bytesWritten;
290 : 0 : p += bytesWritten;
291 : : }
292 : 0 : }
293 : :
294 : 0 : void SAL_CALL Stream::flush( void )
295 : : throw( NotConnectedException, BufferSizeExceededException,
296 : : IOException, RuntimeException )
297 : : {
298 : 0 : }
299 : :
300 : 0 : void SAL_CALL Stream::closeOutput( void )
301 : : throw( com::sun::star::io::NotConnectedException,
302 : : com::sun::star::io::IOException,
303 : : com::sun::star::uno::RuntimeException )
304 : : {
305 : 0 : osl::MutexGuard aGuard( m_aMutex );
306 : 0 : m_bOutputStreamCalled = false;
307 : :
308 : 0 : if( ! m_bInputStreamCalled )
309 : 0 : closeStream();
310 : 0 : }
311 : :
312 : : // -------------------------------------------------------------------
313 : : // Misc.
314 : : // -------------------------------------------------------------------
315 : :
316 : 0 : void Stream::closeStream( void )
317 : : throw( ::com::sun::star::io::NotConnectedException,
318 : : ::com::sun::star::io::IOException,
319 : : ::com::sun::star::uno::RuntimeException )
320 : : {
321 : 0 : if (m_handle) {
322 : 0 : gnome_vfs_close (m_handle);
323 : 0 : m_handle = NULL;
324 : : } else
325 : 0 : throw IOException();
326 : 0 : }
327 : :
328 : 0 : void Stream::throwOnError( GnomeVFSResult result )
329 : : throw( NotConnectedException,
330 : : BufferSizeExceededException,
331 : : IOException,
332 : : RuntimeException )
333 : : {
334 : 0 : if( result != GNOME_VFS_OK ) {
335 : : ::rtl::OUString aMsg = ::rtl::OUString::createFromAscii
336 : 0 : ( gnome_vfs_result_to_string( result ) );
337 : :
338 : 0 : g_warning( "Input Stream exceptional result '%s' (%d)",
339 : 0 : gnome_vfs_result_to_string( result ), result );
340 : :
341 : 0 : throw IOException( aMsg, static_cast< cppu::OWeakObject * >( this ) );
342 : : }
343 : 0 : }
344 : :
345 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|