Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "gvfs_stream.hxx"
21 : #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
22 :
23 : #include <libgnomevfs/gnome-vfs-ops.h>
24 :
25 : using namespace cppu;
26 : using namespace com::sun::star::io;
27 : using namespace com::sun::star::uno;
28 : using namespace com::sun::star::ucb;
29 : using namespace gvfs;
30 :
31 : using ::rtl::OUString;
32 :
33 0 : Stream::Stream( GnomeVFSHandle *handle,
34 : const GnomeVFSFileInfo *aInfo ) :
35 : m_eof (sal_False),
36 : m_bInputStreamCalled( sal_False ),
37 0 : m_bOutputStreamCalled( sal_False )
38 : {
39 0 : m_handle = handle;
40 0 : gnome_vfs_file_info_copy (&m_info, aInfo);
41 0 : }
42 :
43 0 : Stream::~Stream( void )
44 : {
45 0 : if (m_handle) {
46 0 : gnome_vfs_close (m_handle);
47 0 : m_handle = NULL;
48 : }
49 0 : }
50 :
51 0 : Any Stream::queryInterface( const Type &type )
52 : throw( RuntimeException )
53 : {
54 : Any aRet = ::cppu::queryInterface
55 : ( type,
56 : static_cast< XStream * >( this ),
57 : static_cast< XInputStream * >( this ),
58 : static_cast< XOutputStream * >( this ),
59 : static_cast< XSeekable * >( this ),
60 0 : static_cast< XTruncate * >( this ) );
61 :
62 0 : return aRet.hasValue() ? aRet : OWeakObject::queryInterface( type );
63 : }
64 :
65 : // -------------------------------------------------------------------
66 : // XStream
67 : // -------------------------------------------------------------------
68 :
69 : com::sun::star::uno::Reference< com::sun::star::io::XInputStream > SAL_CALL
70 0 : Stream::getInputStream( )
71 : throw( com::sun::star::uno::RuntimeException )
72 : {
73 : {
74 0 : osl::MutexGuard aGuard( m_aMutex );
75 0 : m_bInputStreamCalled = true;
76 : }
77 0 : return Reference< XInputStream >( this );
78 : }
79 :
80 : com::sun::star::uno::Reference< com::sun::star::io::XOutputStream > SAL_CALL
81 0 : Stream::getOutputStream( )
82 : throw( com::sun::star::uno::RuntimeException )
83 : {
84 : {
85 0 : osl::MutexGuard aGuard( m_aMutex );
86 0 : m_bOutputStreamCalled = true;
87 : }
88 0 : return Reference< XOutputStream >( this );
89 : }
90 :
91 : // -------------------------------------------------------------------
92 : // XInputStream
93 : // -------------------------------------------------------------------
94 :
95 0 : sal_Int32 SAL_CALL Stream::readBytes(
96 : Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
97 : throw( NotConnectedException,
98 : BufferSizeExceededException,
99 : IOException,
100 : RuntimeException )
101 : {
102 : GnomeVFSResult result;
103 0 : GnomeVFSFileSize nBytesRead = 0;
104 :
105 0 : if( ! m_handle )
106 0 : throw IOException();
107 :
108 0 : if( m_eof ) {
109 0 : aData.realloc( 0 );
110 0 : return 0;
111 : }
112 :
113 : try {
114 0 : aData.realloc( nBytesToRead );
115 0 : } catch ( const Exception &e ) {
116 0 : throw BufferSizeExceededException();
117 : }
118 :
119 0 : do {
120 0 : result = gnome_vfs_read( m_handle, aData.getArray(),
121 0 : nBytesToRead, &nBytesRead );
122 : } while( result == GNOME_VFS_ERROR_INTERRUPTED );
123 :
124 0 : if (result != GNOME_VFS_OK &&
125 : result != GNOME_VFS_ERROR_EOF)
126 0 : throwOnError( result );
127 :
128 0 : if (result == GNOME_VFS_ERROR_EOF)
129 0 : m_eof = sal_True;
130 :
131 0 : aData.realloc( sal::static_int_cast<sal_uInt32>(nBytesRead) );
132 :
133 0 : return sal::static_int_cast<sal_Int32>(nBytesRead);
134 : }
135 :
136 0 : sal_Int32 SAL_CALL Stream::readSomeBytes(
137 : Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
138 : throw( NotConnectedException,
139 : BufferSizeExceededException,
140 : IOException,
141 : RuntimeException )
142 : {
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 0 : return readBytes( aData, nMaxBytesToRead );
146 : }
147 :
148 0 : void SAL_CALL Stream::skipBytes( sal_Int32 nBytesToSkip )
149 : throw( NotConnectedException,
150 : BufferSizeExceededException,
151 : IOException,
152 : RuntimeException )
153 : {
154 : GnomeVFSResult result;
155 :
156 0 : if( ! m_handle )
157 0 : throw IOException();
158 :
159 0 : result = gnome_vfs_seek( m_handle, GNOME_VFS_SEEK_CURRENT, nBytesToSkip );
160 :
161 0 : if ( result == GNOME_VFS_ERROR_BAD_PARAMETERS ||
162 : result == GNOME_VFS_ERROR_NOT_SUPPORTED )
163 0 : g_warning ("FIXME: just read them in ...");
164 :
165 0 : throwOnError( result );
166 0 : }
167 :
168 0 : sal_Int32 SAL_CALL Stream::available( )
169 : throw( NotConnectedException,
170 : IOException,
171 : RuntimeException )
172 : {
173 0 : return 0; // cf. filinpstr.cxx
174 : }
175 :
176 0 : void SAL_CALL Stream::closeInput( void )
177 : throw( NotConnectedException,
178 : IOException,
179 : RuntimeException )
180 : {
181 0 : osl::MutexGuard aGuard( m_aMutex );
182 0 : m_bInputStreamCalled = false;
183 :
184 0 : if( ! m_bOutputStreamCalled )
185 0 : closeStream();
186 0 : }
187 :
188 : // -------------------------------------------------------------------
189 : // XSeekable
190 : // -------------------------------------------------------------------
191 :
192 0 : void SAL_CALL Stream::seek( sal_Int64 location )
193 : throw( ::com::sun::star::lang::IllegalArgumentException,
194 : IOException,
195 : RuntimeException )
196 : {
197 : GnomeVFSResult result;
198 :
199 0 : if( ! m_handle )
200 0 : throw IOException();
201 :
202 0 : if ( location < 0 )
203 0 : throw ::com::sun::star::lang::IllegalArgumentException();
204 :
205 0 : m_eof = sal_False;
206 0 : result = gnome_vfs_seek( m_handle, GNOME_VFS_SEEK_START, location );
207 :
208 0 : if (result == GNOME_VFS_ERROR_EOF)
209 0 : throw ::com::sun::star::lang::IllegalArgumentException();
210 :
211 0 : throwOnError( result );
212 0 : }
213 :
214 0 : sal_Int64 SAL_CALL Stream::getPosition()
215 : throw( IOException,
216 : RuntimeException )
217 : {
218 0 : GnomeVFSFileSize nBytesIn = 0;
219 :
220 0 : if( ! m_handle )
221 0 : throw IOException();
222 :
223 0 : throwOnError( gnome_vfs_tell( m_handle, &nBytesIn ) );
224 :
225 0 : return nBytesIn;
226 : }
227 :
228 0 : sal_Int64 SAL_CALL Stream::getLength()
229 : throw( IOException, RuntimeException )
230 : {
231 : // FIXME: so this sucks; it may be stale but ...
232 0 : if (m_info.valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE)
233 0 : return m_info.size;
234 : else {
235 0 : g_warning ("FIXME: No valid length");
236 0 : return 0;
237 : }
238 : }
239 :
240 : // -------------------------------------------------------------------
241 : // XTruncate
242 : // -------------------------------------------------------------------
243 :
244 0 : void SAL_CALL Stream::truncate( void )
245 : throw( com::sun::star::io::IOException,
246 : com::sun::star::uno::RuntimeException )
247 : {
248 0 : if( ! m_handle )
249 0 : throw IOException();
250 :
251 0 : throwOnError( gnome_vfs_truncate_handle( m_handle, 0 ) );
252 0 : }
253 :
254 : // -------------------------------------------------------------------
255 : // XOutputStream
256 : // -------------------------------------------------------------------
257 :
258 0 : 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)
263 : {
264 0 : GnomeVFSResult result = GNOME_VFS_OK;
265 0 : GnomeVFSFileSize toWrite = aData.getLength();
266 0 : const sal_Int8 *p = aData.getConstArray();
267 :
268 0 : if( ! m_handle )
269 0 : throw IOException();
270 :
271 0 : while( toWrite > 0) {
272 0 : GnomeVFSFileSize bytesWritten = 0;
273 :
274 0 : result = gnome_vfs_write( m_handle, p, toWrite, &bytesWritten );
275 0 : if( result == GNOME_VFS_ERROR_INTERRUPTED )
276 0 : continue;
277 0 : throwOnError( result );
278 0 : g_assert( bytesWritten <= toWrite );
279 0 : toWrite -= bytesWritten;
280 0 : p += bytesWritten;
281 : }
282 0 : }
283 :
284 0 : void SAL_CALL Stream::flush( void )
285 : throw( NotConnectedException, BufferSizeExceededException,
286 : IOException, RuntimeException )
287 : {
288 0 : }
289 :
290 0 : 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 )
294 : {
295 0 : osl::MutexGuard aGuard( m_aMutex );
296 0 : m_bOutputStreamCalled = false;
297 :
298 0 : if( ! m_bInputStreamCalled )
299 0 : closeStream();
300 0 : }
301 :
302 : // -------------------------------------------------------------------
303 : // Misc.
304 : // -------------------------------------------------------------------
305 :
306 0 : void Stream::closeStream( void )
307 : throw( ::com::sun::star::io::NotConnectedException,
308 : ::com::sun::star::io::IOException,
309 : ::com::sun::star::uno::RuntimeException )
310 : {
311 0 : if (m_handle) {
312 0 : gnome_vfs_close (m_handle);
313 0 : m_handle = NULL;
314 : } else
315 0 : throw IOException();
316 0 : }
317 :
318 0 : void Stream::throwOnError( GnomeVFSResult result )
319 : throw( NotConnectedException,
320 : BufferSizeExceededException,
321 : IOException,
322 : RuntimeException )
323 : {
324 0 : if( result != GNOME_VFS_OK ) {
325 : ::rtl::OUString aMsg = ::rtl::OUString::createFromAscii
326 0 : ( gnome_vfs_result_to_string( result ) );
327 :
328 0 : g_warning( "Input Stream exceptional result '%s' (%d)",
329 0 : gnome_vfs_result_to_string( result ), result );
330 :
331 0 : throw IOException( aMsg, static_cast< cppu::OWeakObject * >( this ) );
332 : }
333 0 : }
334 :
335 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|