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 <sal/config.h>
22 #include <com/sun/star/io/BufferSizeExceededException.hpp>
23 #include <com/sun/star/io/NotConnectedException.hpp>
24 #include <com/sun/star/io/XPipe.hpp>
25 #include <com/sun/star/io/XConnectable.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <cppuhelper/implbase.hxx>
30 #include <cppuhelper/supportsservice.hxx>
32 #include <osl/conditn.hxx>
33 #include <osl/mutex.hxx>
39 using namespace ::osl
;
40 using namespace ::cppu
;
41 using namespace ::com::sun::star::uno
;
42 using namespace ::com::sun::star::io
;
43 using namespace ::com::sun::star::lang
;
45 #include "streamhelper.hxx"
47 namespace com::sun::star::uno
{ class XComponentContext
; }
54 public WeakImplHelper
< XPipe
, XConnectable
, XServiceInfo
>
59 public: // XInputStream
60 virtual sal_Int32 SAL_CALL
readBytes(Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
) override
;
61 virtual sal_Int32 SAL_CALL
readSomeBytes(Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
) override
;
62 virtual void SAL_CALL
skipBytes(sal_Int32 nBytesToSkip
) override
;
63 virtual sal_Int32 SAL_CALL
available() override
;
64 virtual void SAL_CALL
closeInput() override
;
66 public: // XOutputStream
68 virtual void SAL_CALL
writeBytes(const Sequence
< sal_Int8
>& aData
) override
;
69 virtual void SAL_CALL
flush() override
;
70 virtual void SAL_CALL
closeOutput() override
;
72 public: // XConnectable
73 virtual void SAL_CALL
setPredecessor(const Reference
< XConnectable
>& aPredecessor
) override
;
74 virtual Reference
< XConnectable
> SAL_CALL
getPredecessor() override
;
75 virtual void SAL_CALL
setSuccessor(const Reference
< XConnectable
> & aSuccessor
) override
;
76 virtual Reference
< XConnectable
> SAL_CALL
getSuccessor() override
;
79 public: // XServiceInfo
80 OUString SAL_CALL
getImplementationName() override
;
81 Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
82 sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) override
;
86 Reference
< XConnectable
> m_succ
;
87 Reference
< XConnectable
> m_pred
;
89 sal_Int32 m_nBytesToSkip
;
91 bool m_bOutputStreamClosed
;
92 bool m_bInputStreamClosed
;
94 osl::Condition m_conditionBytesAvail
;
96 std::unique_ptr
<MemFIFO
> m_pFIFO
;
101 OPipeImpl::OPipeImpl()
103 , m_bOutputStreamClosed(false )
104 , m_bInputStreamClosed( false )
105 , m_pFIFO( new MemFIFO
)
111 sal_Int32
OPipeImpl::readBytes(Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
115 { // start guarded section
116 MutexGuard
guard( m_mutexAccess
);
117 if( m_bInputStreamClosed
)
119 throw NotConnectedException(
120 "Pipe::readBytes NotConnectedException",
123 sal_Int32 nOccupiedBufferLen
= m_pFIFO
->getSize();
125 if( m_bOutputStreamClosed
&& nBytesToRead
> nOccupiedBufferLen
)
127 nBytesToRead
= nOccupiedBufferLen
;
130 if( nOccupiedBufferLen
< nBytesToRead
)
132 // wait outside guarded section
133 m_conditionBytesAvail
.reset();
136 // necessary bytes are available
137 m_pFIFO
->read( aData
, nBytesToRead
);
140 } // end guarded section
142 // wait for new data outside guarded section!
143 m_conditionBytesAvail
.wait();
148 sal_Int32
OPipeImpl::readSomeBytes(Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
152 MutexGuard
guard( m_mutexAccess
);
153 if( m_bInputStreamClosed
)
155 throw NotConnectedException(
156 "Pipe::readSomeBytes NotConnectedException",
159 if( m_pFIFO
->getSize() )
161 sal_Int32 nSize
= std::min( nMaxBytesToRead
, m_pFIFO
->getSize() );
162 aData
.realloc( nSize
);
163 m_pFIFO
->read( aData
, nSize
);
167 if( m_bOutputStreamClosed
)
169 // no bytes in buffer anymore
174 m_conditionBytesAvail
.wait();
179 void OPipeImpl::skipBytes(sal_Int32 nBytesToSkip
)
181 MutexGuard
guard( m_mutexAccess
);
182 if( m_bInputStreamClosed
)
184 throw NotConnectedException(
185 "Pipe::skipBytes NotConnectedException",
191 > std::numeric_limits
< sal_Int32
>::max() - m_nBytesToSkip
) )
193 throw BufferSizeExceededException(
194 "Pipe::skipBytes BufferSizeExceededException",
197 m_nBytesToSkip
+= nBytesToSkip
;
199 nBytesToSkip
= std::min( m_pFIFO
->getSize() , m_nBytesToSkip
);
200 m_pFIFO
->skip( nBytesToSkip
);
201 m_nBytesToSkip
-= nBytesToSkip
;
205 sal_Int32
OPipeImpl::available()
207 MutexGuard
guard( m_mutexAccess
);
208 if( m_bInputStreamClosed
)
210 throw NotConnectedException(
211 "Pipe::available NotConnectedException",
214 return m_pFIFO
->getSize();
217 void OPipeImpl::closeInput()
219 MutexGuard
guard( m_mutexAccess
);
221 m_bInputStreamClosed
= true;
225 // readBytes may throw an exception
226 m_conditionBytesAvail
.set();
228 setSuccessor( Reference
< XConnectable
> () );
232 void OPipeImpl::writeBytes(const Sequence
< sal_Int8
>& aData
)
234 MutexGuard
guard( m_mutexAccess
);
236 if( m_bOutputStreamClosed
)
238 throw NotConnectedException(
239 "Pipe::writeBytes NotConnectedException (outputstream)",
243 if( m_bInputStreamClosed
)
245 throw NotConnectedException(
246 "Pipe::writeBytes NotConnectedException (inputstream)",
251 sal_Int32 nLen
= aData
.getLength();
252 if( m_nBytesToSkip
&& m_nBytesToSkip
>= nLen
) {
253 // all must be skipped - forget whole call
254 m_nBytesToSkip
-= nLen
;
258 // adjust buffersize if necessary
261 Sequence
< sal_Int8
> seqCopy( nLen
- m_nBytesToSkip
);
262 memcpy( seqCopy
.getArray() , &( aData
.getConstArray()[m_nBytesToSkip
] ) , nLen
-m_nBytesToSkip
);
263 m_pFIFO
->write( seqCopy
);
267 m_pFIFO
->write( aData
);
271 // readBytes may check again if enough bytes are available
272 m_conditionBytesAvail
.set();
276 void OPipeImpl::flush()
278 // nothing to do for a pipe
281 void OPipeImpl::closeOutput()
283 MutexGuard
guard( m_mutexAccess
);
285 m_bOutputStreamClosed
= true;
286 m_conditionBytesAvail
.set();
287 setPredecessor( Reference
< XConnectable
> () );
291 void OPipeImpl::setSuccessor( const Reference
< XConnectable
> &r
)
293 /// if the references match, nothing needs to be done
295 /// store the reference for later use
300 m_succ
->setPredecessor(
301 Reference
< XConnectable
> ( static_cast< XConnectable
* >(this) ) );
306 Reference
< XConnectable
> OPipeImpl::getSuccessor()
313 void OPipeImpl::setPredecessor( const Reference
< XConnectable
> &r
)
318 m_pred
->setSuccessor(
319 Reference
< XConnectable
> ( static_cast< XConnectable
* >(this) ) );
324 Reference
< XConnectable
> OPipeImpl::getPredecessor()
331 OUString
OPipeImpl::getImplementationName()
333 return "com.sun.star.comp.io.stm.Pipe";
337 sal_Bool
OPipeImpl::supportsService(const OUString
& ServiceName
)
339 return cppu::supportsService(this, ServiceName
);
343 Sequence
< OUString
> OPipeImpl::getSupportedServiceNames()
345 return { "com.sun.star.io.Pipe" };
350 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
351 io_OPipeImpl_get_implementation(
352 css::uno::XComponentContext
* , css::uno::Sequence
<css::uno::Any
> const&)
354 return cppu::acquire(new io_stm::OPipeImpl());
357 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */