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 :
21 : // streams
22 : #include <com/sun/star/io/XPipe.hpp>
23 : #include <com/sun/star/io/XInputStream.hpp>
24 : #include <com/sun/star/io/XOutputStream.hpp>
25 : #include <com/sun/star/io/XConnectable.hpp>
26 :
27 : #include <com/sun/star/lang/XServiceInfo.hpp>
28 :
29 : #include <cppuhelper/factory.hxx>
30 :
31 : #include <cppuhelper/implbase3.hxx> // OWeakObject
32 :
33 : #include <osl/conditn.hxx>
34 : #include <osl/mutex.hxx>
35 :
36 : #include <limits>
37 : #include <string.h>
38 :
39 : using namespace ::rtl;
40 : using namespace ::osl;
41 : using namespace ::cppu;
42 : using namespace ::com::sun::star::uno;
43 : using namespace ::com::sun::star::io;
44 : using namespace ::com::sun::star::lang;
45 :
46 : #include "factreg.hxx"
47 : #include "streamhelper.hxx"
48 :
49 : // Implementation and service names
50 : #define IMPLEMENTATION_NAME "com.sun.star.comp.io.stm.Pipe"
51 : #define SERVICE_NAME "com.sun.star.io.Pipe"
52 :
53 : namespace io_stm{
54 :
55 : class OPipeImpl :
56 : public WeakImplHelper3< XPipe , XConnectable , XServiceInfo >
57 : {
58 : public:
59 : OPipeImpl( );
60 : ~OPipeImpl();
61 :
62 : public: // XInputStream
63 : virtual sal_Int32 SAL_CALL readBytes(Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
64 : throw( NotConnectedException,
65 : BufferSizeExceededException,
66 : RuntimeException );
67 : virtual sal_Int32 SAL_CALL readSomeBytes(Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead)
68 : throw( NotConnectedException,
69 : BufferSizeExceededException,
70 : RuntimeException );
71 : virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip)
72 : throw( NotConnectedException,
73 : BufferSizeExceededException,
74 : RuntimeException );
75 : virtual sal_Int32 SAL_CALL available(void)
76 : throw( NotConnectedException,
77 : RuntimeException );
78 : virtual void SAL_CALL closeInput(void)
79 : throw( NotConnectedException,
80 : RuntimeException );
81 :
82 : public: // XOutputStream
83 :
84 : virtual void SAL_CALL writeBytes(const Sequence< sal_Int8 >& aData)
85 : throw( NotConnectedException,
86 : BufferSizeExceededException,
87 : RuntimeException );
88 : virtual void SAL_CALL flush(void)
89 : throw( NotConnectedException,
90 : BufferSizeExceededException,
91 : RuntimeException );
92 : virtual void SAL_CALL closeOutput(void)
93 : throw( NotConnectedException,
94 : BufferSizeExceededException,
95 : RuntimeException );
96 :
97 : public: // XConnectable
98 : virtual void SAL_CALL setPredecessor(const Reference< XConnectable >& aPredecessor)
99 : throw( RuntimeException );
100 : virtual Reference< XConnectable > SAL_CALL getPredecessor(void) throw( RuntimeException );
101 : virtual void SAL_CALL setSuccessor(const Reference < XConnectable > & aSuccessor)
102 : throw( RuntimeException );
103 : virtual Reference < XConnectable > SAL_CALL getSuccessor(void) throw( RuntimeException ) ;
104 :
105 :
106 : public: // XServiceInfo
107 : OUString SAL_CALL getImplementationName() throw( );
108 : Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw( );
109 : sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw( );
110 :
111 : private:
112 :
113 : Reference < XConnectable > m_succ;
114 : Reference < XConnectable > m_pred;
115 :
116 : sal_Int32 m_nBytesToSkip;
117 :
118 : sal_Bool m_bOutputStreamClosed;
119 : sal_Bool m_bInputStreamClosed;
120 :
121 : oslCondition m_conditionBytesAvail;
122 : Mutex m_mutexAccess;
123 : I_FIFO *m_pFIFO;
124 : };
125 :
126 :
127 :
128 0 : OPipeImpl::OPipeImpl()
129 : {
130 0 : g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
131 0 : m_nBytesToSkip = 0;
132 :
133 0 : m_bOutputStreamClosed = sal_False;
134 0 : m_bInputStreamClosed = sal_False;
135 :
136 0 : m_pFIFO = new MemFIFO;
137 0 : m_conditionBytesAvail = osl_createCondition();
138 0 : }
139 :
140 0 : OPipeImpl::~OPipeImpl()
141 : {
142 0 : osl_destroyCondition( m_conditionBytesAvail );
143 0 : delete m_pFIFO;
144 0 : g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
145 0 : }
146 :
147 :
148 0 : sal_Int32 OPipeImpl::readBytes(Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
149 : throw( NotConnectedException, BufferSizeExceededException,RuntimeException )
150 : {
151 0 : while( sal_True )
152 : {
153 : { // start guarded section
154 0 : MutexGuard guard( m_mutexAccess );
155 0 : if( m_bInputStreamClosed )
156 : {
157 : throw NotConnectedException(
158 : OUString("Pipe::readBytes NotConnectedException"),
159 0 : *this );
160 : }
161 0 : sal_Int32 nOccupiedBufferLen = m_pFIFO->getSize();
162 :
163 0 : if( m_bOutputStreamClosed && nBytesToRead > nOccupiedBufferLen )
164 : {
165 0 : nBytesToRead = nOccupiedBufferLen;
166 : }
167 :
168 0 : if( nOccupiedBufferLen < nBytesToRead )
169 : {
170 : // wait outside guarded section
171 0 : osl_resetCondition( m_conditionBytesAvail );
172 : }
173 : else {
174 : // necessary bytes are available
175 0 : m_pFIFO->read( aData , nBytesToRead );
176 0 : return nBytesToRead;
177 0 : }
178 : } // end guarded section
179 :
180 : // wait for new data outside guarded section!
181 0 : osl_waitCondition( m_conditionBytesAvail , 0 );
182 : }
183 : }
184 :
185 :
186 0 : sal_Int32 OPipeImpl::readSomeBytes(Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead)
187 : throw( NotConnectedException,
188 : BufferSizeExceededException,
189 : RuntimeException )
190 : {
191 0 : while( sal_True ) {
192 : {
193 0 : MutexGuard guard( m_mutexAccess );
194 0 : if( m_bInputStreamClosed )
195 : {
196 : throw NotConnectedException(
197 : OUString("Pipe::readSomeBytes NotConnectedException"),
198 0 : *this );
199 : }
200 0 : if( m_pFIFO->getSize() )
201 : {
202 0 : sal_Int32 nSize = Min( nMaxBytesToRead , m_pFIFO->getSize() );
203 0 : aData.realloc( nSize );
204 0 : m_pFIFO->read( aData , nSize );
205 0 : return nSize;
206 : }
207 :
208 0 : if( m_bOutputStreamClosed )
209 : {
210 : // no bytes in buffer anymore
211 0 : return 0;
212 0 : }
213 : }
214 :
215 0 : osl_waitCondition( m_conditionBytesAvail , 0 );
216 : }
217 : }
218 :
219 :
220 0 : void OPipeImpl::skipBytes(sal_Int32 nBytesToSkip)
221 : throw( NotConnectedException,
222 : BufferSizeExceededException,
223 : RuntimeException )
224 : {
225 0 : MutexGuard guard( m_mutexAccess );
226 0 : if( m_bInputStreamClosed )
227 : {
228 : throw NotConnectedException(
229 : OUString("Pipe::skipBytes NotConnectedException"),
230 0 : *this );
231 : }
232 :
233 0 : if( nBytesToSkip < 0
234 : || (nBytesToSkip
235 0 : > std::numeric_limits< sal_Int32 >::max() - m_nBytesToSkip) )
236 : {
237 : throw BufferSizeExceededException(
238 : OUString("Pipe::skipBytes BufferSizeExceededException"),
239 0 : *this );
240 : }
241 0 : m_nBytesToSkip += nBytesToSkip;
242 :
243 0 : nBytesToSkip = Min( m_pFIFO->getSize() , m_nBytesToSkip );
244 0 : m_pFIFO->skip( nBytesToSkip );
245 0 : m_nBytesToSkip -= nBytesToSkip;
246 0 : }
247 :
248 :
249 0 : sal_Int32 OPipeImpl::available(void)
250 : throw( NotConnectedException,
251 : RuntimeException )
252 : {
253 0 : MutexGuard guard( m_mutexAccess );
254 0 : if( m_bInputStreamClosed )
255 : {
256 : throw NotConnectedException(
257 : OUString("Pipe::available NotConnectedException"),
258 0 : *this );
259 : }
260 0 : return m_pFIFO->getSize();
261 : }
262 :
263 0 : void OPipeImpl::closeInput(void)
264 : throw( NotConnectedException,
265 : RuntimeException)
266 : {
267 0 : MutexGuard guard( m_mutexAccess );
268 :
269 0 : m_bInputStreamClosed = sal_True;
270 :
271 0 : delete m_pFIFO;
272 0 : m_pFIFO = 0;
273 :
274 : // readBytes may throw an exception
275 0 : osl_setCondition( m_conditionBytesAvail );
276 :
277 0 : setSuccessor( Reference< XConnectable > () );
278 0 : return;
279 : }
280 :
281 :
282 0 : void OPipeImpl::writeBytes(const Sequence< sal_Int8 >& aData)
283 : throw( NotConnectedException,
284 : BufferSizeExceededException,
285 : RuntimeException)
286 : {
287 0 : MutexGuard guard( m_mutexAccess );
288 :
289 0 : if( m_bOutputStreamClosed )
290 : {
291 : throw NotConnectedException(
292 : OUString("Pipe::writeBytes NotConnectedException (outputstream)"),
293 0 : *this );
294 : }
295 :
296 0 : if( m_bInputStreamClosed )
297 : {
298 : throw NotConnectedException(
299 : OUString("Pipe::writeBytes NotConnectedException (inputstream)"),
300 0 : *this );
301 : }
302 :
303 : // check skipping
304 0 : sal_Int32 nLen = aData.getLength();
305 0 : if( m_nBytesToSkip && m_nBytesToSkip >= nLen ) {
306 : // all must be skipped - forget whole call
307 0 : m_nBytesToSkip -= nLen;
308 0 : return;
309 : }
310 :
311 : // adjust buffersize if necessary
312 :
313 : try
314 : {
315 0 : if( m_nBytesToSkip )
316 : {
317 0 : Sequence< sal_Int8 > seqCopy( nLen - m_nBytesToSkip );
318 0 : memcpy( seqCopy.getArray() , &( aData.getConstArray()[m_nBytesToSkip] ) , nLen-m_nBytesToSkip );
319 0 : m_pFIFO->write( seqCopy );
320 : }
321 : else
322 : {
323 0 : m_pFIFO->write( aData );
324 : }
325 0 : m_nBytesToSkip = 0;
326 : }
327 0 : catch ( I_FIFO_OutOfBoundsException & )
328 : {
329 : throw BufferSizeExceededException(
330 : OUString("Pipe::writeBytes BufferSizeExceededException"),
331 0 : *this );
332 : }
333 0 : catch ( I_FIFO_OutOfMemoryException & )
334 : {
335 : throw BufferSizeExceededException(
336 : OUString("Pipe::writeBytes BufferSizeExceededException"),
337 0 : *this );
338 : }
339 :
340 : // readBytes may check again if enough bytes are available
341 0 : osl_setCondition( m_conditionBytesAvail );
342 : }
343 :
344 :
345 0 : void OPipeImpl::flush(void)
346 : throw( NotConnectedException,
347 : BufferSizeExceededException,
348 : RuntimeException)
349 : {
350 : // nothing to do for a pipe
351 0 : return;
352 : }
353 :
354 0 : void OPipeImpl::closeOutput(void)
355 : throw( NotConnectedException,
356 : BufferSizeExceededException,
357 : RuntimeException)
358 : {
359 0 : MutexGuard guard( m_mutexAccess );
360 :
361 0 : m_bOutputStreamClosed = sal_True;
362 0 : osl_setCondition( m_conditionBytesAvail );
363 0 : setPredecessor( Reference < XConnectable > () );
364 0 : return;
365 : }
366 :
367 :
368 0 : void OPipeImpl::setSuccessor( const Reference < XConnectable > &r )
369 : throw( RuntimeException )
370 : {
371 : /// if the references match, nothing needs to be done
372 0 : if( m_succ != r ) {
373 : /// store the reference for later use
374 0 : m_succ = r;
375 :
376 0 : if( m_succ.is() )
377 : {
378 0 : m_succ->setPredecessor(
379 0 : Reference< XConnectable > ( (static_cast< XConnectable * >(this)) ) );
380 : }
381 : }
382 0 : }
383 :
384 0 : Reference < XConnectable > OPipeImpl::getSuccessor() throw( RuntimeException )
385 : {
386 0 : return m_succ;
387 : }
388 :
389 :
390 : // XDataSource
391 0 : void OPipeImpl::setPredecessor( const Reference < XConnectable > &r )
392 : throw( RuntimeException )
393 : {
394 0 : if( r != m_pred ) {
395 0 : m_pred = r;
396 0 : if( m_pred.is() ) {
397 0 : m_pred->setSuccessor(
398 0 : Reference < XConnectable > ( (static_cast< XConnectable * >(this)) ) );
399 : }
400 : }
401 0 : }
402 :
403 0 : Reference < XConnectable > OPipeImpl::getPredecessor() throw( RuntimeException )
404 : {
405 0 : return m_pred;
406 : }
407 :
408 :
409 :
410 :
411 : // XServiceInfo
412 0 : OUString OPipeImpl::getImplementationName() throw( )
413 : {
414 0 : return OPipeImpl_getImplementationName();
415 : }
416 :
417 : // XServiceInfo
418 0 : sal_Bool OPipeImpl::supportsService(const OUString& ServiceName) throw( )
419 : {
420 0 : Sequence< OUString > aSNL = getSupportedServiceNames();
421 0 : const OUString * pArray = aSNL.getConstArray();
422 :
423 0 : for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
424 0 : if( pArray[i] == ServiceName )
425 0 : return sal_True;
426 :
427 0 : return sal_False;
428 : }
429 :
430 : // XServiceInfo
431 0 : Sequence< OUString > OPipeImpl::getSupportedServiceNames(void) throw( )
432 : {
433 0 : return OPipeImpl_getSupportedServiceNames();
434 : }
435 :
436 :
437 :
438 :
439 :
440 : /* implementation functions
441 : *
442 : *
443 : */
444 :
445 :
446 0 : Reference < XInterface > SAL_CALL OPipeImpl_CreateInstance(
447 : SAL_UNUSED_PARAMETER const Reference < XComponentContext > & )
448 : throw(Exception)
449 : {
450 0 : OPipeImpl *p = new OPipeImpl;
451 :
452 0 : return Reference < XInterface > ( (static_cast< OWeakObject * >(p)) );
453 : }
454 :
455 :
456 0 : OUString OPipeImpl_getImplementationName()
457 : {
458 0 : return OUString( RTL_CONSTASCII_USTRINGPARAM ( IMPLEMENTATION_NAME ) );
459 : }
460 :
461 0 : Sequence<OUString> OPipeImpl_getSupportedServiceNames(void)
462 : {
463 0 : Sequence<OUString> aRet(1);
464 0 : aRet.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICE_NAME ));
465 0 : return aRet;
466 : }
467 : }
468 :
469 :
470 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|