tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / io / source / stm / odata.cxx
blobf55be79dc4c023d646c3d7aa30874250cea58969
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <string.h>
21 #include <unordered_map>
22 #include <vector>
24 #include <cppuhelper/weak.hxx>
25 #include <cppuhelper/implbase.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <osl/endian.h>
29 #include <com/sun/star/io/NotConnectedException.hpp>
30 #include <com/sun/star/io/XObjectInputStream.hpp>
31 #include <com/sun/star/io/XObjectOutputStream.hpp>
32 #include <com/sun/star/io/XActiveDataSource.hpp>
33 #include <com/sun/star/io/XActiveDataSink.hpp>
34 #include <com/sun/star/io/XMarkableStream.hpp>
35 #include <com/sun/star/io/XConnectable.hpp>
36 #include <com/sun/star/io/UnexpectedEOFException.hpp>
37 #include <com/sun/star/io/WrongFormatException.hpp>
38 #include <com/sun/star/lang/XServiceInfo.hpp>
39 #include <com/sun/star/uno/XComponentContext.hpp>
41 using namespace ::cppu;
42 using namespace ::com::sun::star::io;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::lang;
46 namespace io_stm {
48 namespace {
50 class ODataInputStream :
51 public WeakImplHelper <
52 XDataInputStream,
53 XActiveDataSink,
54 XConnectable,
55 XServiceInfo
58 public:
59 ODataInputStream( )
60 : m_bValidStream( false )
64 public: // XInputStream
65 virtual sal_Int32 SAL_CALL readBytes(Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead) override;
66 virtual sal_Int32 SAL_CALL readSomeBytes(Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) override;
67 virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override;
68 virtual sal_Int32 SAL_CALL available() override;
69 virtual void SAL_CALL closeInput() override;
71 public: // XDataInputStream
72 virtual sal_Int8 SAL_CALL readBoolean() override;
73 virtual sal_Int8 SAL_CALL readByte() override;
74 virtual sal_Unicode SAL_CALL readChar() override;
75 virtual sal_Int16 SAL_CALL readShort() override;
76 virtual sal_Int32 SAL_CALL readLong() override;
77 virtual sal_Int64 SAL_CALL readHyper() override;
78 virtual float SAL_CALL readFloat() override;
79 virtual double SAL_CALL readDouble() override;
80 virtual OUString SAL_CALL readUTF() override;
83 public: // XActiveDataSink
84 virtual void SAL_CALL setInputStream(const Reference< XInputStream > & aStream) override;
85 virtual Reference< XInputStream > SAL_CALL getInputStream() override;
87 public: // XConnectable
88 virtual void SAL_CALL setPredecessor(const Reference < XConnectable >& aPredecessor) override;
89 virtual Reference < XConnectable > SAL_CALL getPredecessor() override;
90 virtual void SAL_CALL setSuccessor(const Reference < XConnectable >& aSuccessor) override;
91 virtual Reference < XConnectable > SAL_CALL getSuccessor() override ;
94 public: // XServiceInfo
95 OUString SAL_CALL getImplementationName() override;
96 Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
97 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
99 protected:
101 Reference < XConnectable > m_pred;
102 Reference < XConnectable > m_succ;
103 Reference < XInputStream > m_input;
104 bool m_bValidStream;
109 // XInputStream
110 sal_Int32 ODataInputStream::readBytes(Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
112 if( !m_bValidStream )
114 throw NotConnectedException( );
116 sal_Int32 nRead = m_input->readBytes( aData , nBytesToRead );
117 return nRead;
120 sal_Int32 ODataInputStream::readSomeBytes(Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead)
122 if( !m_bValidStream )
123 throw NotConnectedException( );
124 sal_Int32 nRead = m_input->readSomeBytes( aData , nMaxBytesToRead );
125 return nRead;
127 void ODataInputStream::skipBytes(sal_Int32 nBytesToSkip)
129 if( !m_bValidStream )
130 throw NotConnectedException( );
131 m_input->skipBytes( nBytesToSkip );
135 sal_Int32 ODataInputStream::available()
137 if( !m_bValidStream )
138 throw NotConnectedException( );
139 sal_Int32 nAvail = m_input->available( );
140 return nAvail;
143 void ODataInputStream::closeInput()
145 if( !m_bValidStream )
146 throw NotConnectedException( );
147 m_input->closeInput( );
148 setInputStream( Reference< XInputStream > () );
149 setPredecessor( Reference < XConnectable >() );
150 setSuccessor( Reference < XConnectable >() );
151 m_bValidStream = false;
155 //== XDataInputStream ===========================================
157 // XDataInputStream
158 sal_Int8 ODataInputStream::readBoolean()
160 return readByte();
163 sal_Int8 ODataInputStream::readByte()
165 Sequence<sal_Int8> aTmp(1);
166 if( 1 != readBytes( aTmp, 1 ) )
168 throw UnexpectedEOFException();
170 return aTmp.getConstArray()[0];
173 sal_Unicode ODataInputStream::readChar()
175 Sequence<sal_Int8> aTmp(2);
176 if( 2 != readBytes( aTmp, 2 ) )
178 throw UnexpectedEOFException();
181 const sal_uInt8 * pBytes = reinterpret_cast<const sal_uInt8 *>(aTmp.getConstArray());
182 return (static_cast<sal_Unicode>(pBytes[0]) << 8) + pBytes[1];
185 sal_Int16 ODataInputStream::readShort()
187 Sequence<sal_Int8> aTmp(2);
188 if( 2 != readBytes( aTmp, 2 ) )
190 throw UnexpectedEOFException();
193 const sal_uInt8 * pBytes = reinterpret_cast<const sal_uInt8 *>(aTmp.getConstArray());
194 return (static_cast<sal_Int16>(pBytes[0]) << 8) + pBytes[1];
198 sal_Int32 ODataInputStream::readLong()
200 Sequence<sal_Int8> aTmp(4);
201 if( 4 != readBytes( aTmp, 4 ) )
203 throw UnexpectedEOFException( );
206 const sal_uInt8 * pBytes = reinterpret_cast<const sal_uInt8 *>(aTmp.getConstArray());
207 return (static_cast<sal_Int32>(pBytes[0]) << 24) + (static_cast<sal_Int32>(pBytes[1]) << 16) + (static_cast<sal_Int32>(pBytes[2]) << 8) + pBytes[3];
211 sal_Int64 ODataInputStream::readHyper()
213 Sequence<sal_Int8> aTmp(8);
214 if( 8 != readBytes( aTmp, 8 ) )
216 throw UnexpectedEOFException( );
219 const sal_uInt8 * pBytes = reinterpret_cast<const sal_uInt8 *>(aTmp.getConstArray());
220 return
221 (static_cast<sal_Int64>(pBytes[0]) << 56) +
222 (static_cast<sal_Int64>(pBytes[1]) << 48) +
223 (static_cast<sal_Int64>(pBytes[2]) << 40) +
224 (static_cast<sal_Int64>(pBytes[3]) << 32) +
225 (static_cast<sal_Int64>(pBytes[4]) << 24) +
226 (static_cast<sal_Int64>(pBytes[5]) << 16) +
227 (static_cast<sal_Int64>(pBytes[6]) << 8) +
228 pBytes[7];
231 float ODataInputStream::readFloat()
233 union { float f; sal_uInt32 n; } a;
234 a.n = readLong();
235 return a.f;
238 double ODataInputStream::readDouble()
240 union { double d; struct { sal_uInt32 n1; sal_uInt32 n2; } ad; } a;
241 #if defined OSL_LITENDIAN
242 a.ad.n2 = readLong();
243 a.ad.n1 = readLong();
244 #else
245 a.ad.n1 = readLong();
246 a.ad.n2 = readLong();
247 #endif
248 return a.d;
251 OUString ODataInputStream::readUTF()
253 sal_uInt16 nShortLen = static_cast<sal_uInt16>(readShort());
254 sal_Int32 nUTFLen;
256 if( (sal_uInt16(0xffff)) == nShortLen )
258 // is interpreted as a sign, that string is longer than 64k
259 // incompatible to older XDataInputStream-routines, when strings are exactly 64k
260 nUTFLen = readLong();
262 else
264 nUTFLen = static_cast<sal_Int32>(nShortLen);
267 Sequence<sal_Unicode> aBuffer( nUTFLen );
268 sal_Unicode * pStr = aBuffer.getArray();
270 sal_Int32 nCount = 0;
271 sal_Int32 nStrLen = 0;
272 while( nCount < nUTFLen )
274 sal_uInt8 c = static_cast<sal_uInt8>(readByte());
275 sal_uInt8 char2, char3;
276 switch( c >> 4 )
278 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
279 // 0xxxxxxx
280 nCount++;
281 pStr[nStrLen++] = c;
282 break;
284 case 12: case 13:
285 // 110x xxxx 10xx xxxx
286 nCount += 2;
287 if( nCount > nUTFLen )
289 throw WrongFormatException( );
292 char2 = static_cast<sal_uInt8>(readByte());
293 if( (char2 & 0xC0) != 0x80 )
295 throw WrongFormatException( );
298 pStr[nStrLen++] = (sal_Unicode(c & 0x1F) << 6) | (char2 & 0x3F);
299 break;
301 case 14:
302 // 1110 xxxx 10xx xxxx 10xx xxxx
303 nCount += 3;
304 if( nCount > nUTFLen )
306 throw WrongFormatException( );
309 char2 = static_cast<sal_uInt8>(readByte());
310 char3 = static_cast<sal_uInt8>(readByte());
312 if( ((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80) ) {
313 throw WrongFormatException( );
315 pStr[nStrLen++] = (sal_Unicode(c & 0x0F) << 12) |
316 (sal_Unicode(char2 & 0x3F) << 6) |
317 (char3 & 0x3F);
318 break;
320 default:
321 // 10xx xxxx, 1111 xxxx
322 throw WrongFormatException();
323 //throw new UTFDataFormatException();
326 return OUString( pStr, nStrLen );
330 // XActiveDataSource
331 void ODataInputStream::setInputStream(const Reference< XInputStream > & aStream)
334 if( m_input != aStream ) {
335 m_input = aStream;
337 Reference < XConnectable > pred( m_input , UNO_QUERY );
338 setPredecessor( pred );
341 m_bValidStream = m_input.is();
344 Reference< XInputStream > ODataInputStream::getInputStream()
346 return m_input;
350 // XDataSink
351 void ODataInputStream::setSuccessor( const Reference < XConnectable > &r )
353 /// if the references match, nothing needs to be done
354 if( m_succ != r ) {
355 /// store the reference for later use
356 m_succ = r;
358 if( m_succ.is() ) {
359 /// set this instance as the sink !
360 m_succ->setPredecessor( Reference< XConnectable > (
361 static_cast< XConnectable * >(this) ) );
366 Reference < XConnectable > ODataInputStream::getSuccessor()
368 return m_succ;
372 // XDataSource
373 void ODataInputStream::setPredecessor( const Reference < XConnectable > &r )
375 if( r != m_pred ) {
376 m_pred = r;
377 if( m_pred.is() ) {
378 m_pred->setSuccessor( Reference< XConnectable > (
379 static_cast< XConnectable * >(this) ) );
383 Reference < XConnectable > ODataInputStream::getPredecessor()
385 return m_pred;
388 // XServiceInfo
389 OUString ODataInputStream::getImplementationName()
391 return u"com.sun.star.comp.io.stm.DataInputStream"_ustr;
394 // XServiceInfo
395 sal_Bool ODataInputStream::supportsService(const OUString& ServiceName)
397 return cppu::supportsService(this, ServiceName);
400 // XServiceInfo
401 Sequence< OUString > ODataInputStream::getSupportedServiceNames()
403 return { u"com.sun.star.io.DataInputStream"_ustr };
406 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
407 io_ODataInputStream_get_implementation(
408 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
410 return cppu::acquire(new ODataInputStream());
413 namespace {
415 class ODataOutputStream :
416 public WeakImplHelper <
417 XDataOutputStream,
418 XActiveDataSource,
419 XConnectable,
420 XServiceInfo >
422 public:
423 ODataOutputStream()
424 : m_bValidStream( false )
428 public: // XOutputStream
429 virtual void SAL_CALL writeBytes(const Sequence< sal_Int8 >& aData) override;
430 virtual void SAL_CALL flush() override;
431 virtual void SAL_CALL closeOutput() override;
433 public: // XDataOutputStream
434 virtual void SAL_CALL writeBoolean(sal_Bool Value) override;
435 virtual void SAL_CALL writeByte(sal_Int8 Value) override;
436 virtual void SAL_CALL writeChar(sal_Unicode Value) override;
437 virtual void SAL_CALL writeShort(sal_Int16 Value) override;
438 virtual void SAL_CALL writeLong(sal_Int32 Value) override;
439 virtual void SAL_CALL writeHyper(sal_Int64 Value) override;
440 virtual void SAL_CALL writeFloat(float Value) override;
441 virtual void SAL_CALL writeDouble(double Value) override;
442 virtual void SAL_CALL writeUTF(const OUString& Value) override;
444 public: // XActiveDataSource
445 virtual void SAL_CALL setOutputStream(const Reference< XOutputStream > & aStream) override;
446 virtual Reference < XOutputStream > SAL_CALL getOutputStream() override;
448 public: // XConnectable
449 virtual void SAL_CALL setPredecessor(const Reference < XConnectable >& aPredecessor) override;
450 virtual Reference < XConnectable > SAL_CALL getPredecessor() override;
451 virtual void SAL_CALL setSuccessor(const Reference < XConnectable >& aSuccessor) override;
452 virtual Reference < XConnectable > SAL_CALL getSuccessor() override;
454 public: // XServiceInfo
455 OUString SAL_CALL getImplementationName() override;
456 Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
457 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
459 protected:
460 Reference < XConnectable > m_succ;
461 Reference < XConnectable > m_pred;
462 Reference< XOutputStream > m_output;
463 bool m_bValidStream;
468 // XOutputStream
469 void ODataOutputStream::writeBytes(const Sequence< sal_Int8 >& aData)
471 if( !m_bValidStream )
472 throw NotConnectedException( );
473 m_output->writeBytes( aData );
476 void ODataOutputStream::flush()
478 if( !m_bValidStream )
479 throw NotConnectedException();
480 m_output->flush();
484 void ODataOutputStream::closeOutput()
486 if( !m_bValidStream )
487 throw NotConnectedException();
488 m_output->closeOutput();
489 setOutputStream( Reference< XOutputStream > () );
490 setPredecessor( Reference < XConnectable >() );
491 setSuccessor( Reference < XConnectable >() );
494 // XDataOutputStream
495 void ODataOutputStream::writeBoolean(sal_Bool Value)
497 if( Value )
499 writeByte( 1 );
501 else
503 writeByte( 0 );
508 void ODataOutputStream::writeByte(sal_Int8 Value)
510 writeBytes( { Value } );
513 void ODataOutputStream::writeChar(sal_Unicode Value)
515 writeBytes( { sal_Int8(Value >> 8),
516 sal_Int8(Value) } );
520 void ODataOutputStream::writeShort(sal_Int16 Value)
522 writeBytes( { sal_Int8(Value >> 8),
523 sal_Int8(Value) } );
526 void ODataOutputStream::writeLong(sal_Int32 Value)
528 writeBytes( { sal_Int8(Value >> 24),
529 sal_Int8(Value >> 16),
530 sal_Int8(Value >> 8),
531 sal_Int8(Value) } );
534 void ODataOutputStream::writeHyper(sal_Int64 Value)
536 writeBytes( { sal_Int8(Value >> 56),
537 sal_Int8(Value >> 48),
538 sal_Int8(Value >> 40),
539 sal_Int8(Value >> 32),
540 sal_Int8(Value >> 24),
541 sal_Int8(Value >> 16),
542 sal_Int8(Value >> 8),
543 sal_Int8(Value) } );
547 void ODataOutputStream::writeFloat(float Value)
549 union { float f; sal_uInt32 n; } a;
550 a.f = Value;
551 writeLong( a.n );
554 void ODataOutputStream::writeDouble(double Value)
556 union { double d; struct { sal_uInt32 n1; sal_uInt32 n2; } ad; } a;
557 a.d = Value;
558 #if defined OSL_LITENDIAN
559 writeLong( a.ad.n2 );
560 writeLong( a.ad.n1 );
561 #else
562 writeLong( a.ad.n1 );
563 writeLong( a.ad.n2 );
564 #endif
567 void ODataOutputStream::writeUTF(const OUString& Value)
569 sal_Int32 nStrLen = Value.getLength();
570 const sal_Unicode * pStr = Value.getStr();
571 sal_Int32 nUTFLen = 0;
572 sal_Int32 i;
574 for( i = 0 ; i < nStrLen ; i++ )
576 sal_uInt16 c = pStr[i];
577 if( (c >= 0x0001) && (c <= 0x007F) )
579 nUTFLen++;
581 else if( c > 0x07FF )
583 nUTFLen += 3;
585 else
587 nUTFLen += 2;
592 // compatibility mode for older implementations, where it was not possible
593 // to write blocks bigger than 64 k. Note that there is a tradeoff. Blocks,
594 // that are exactly 64k long can not be read by older routines when written
595 // with these routines and the other way round !!!!!
596 if( nUTFLen >= 0xFFFF ) {
597 writeShort( sal_Int16(-1) );
598 writeLong( nUTFLen );
600 else {
601 writeShort( static_cast<sal_uInt16>(nUTFLen) );
603 for( i = 0 ; i < nStrLen ; i++ )
605 sal_uInt16 c = pStr[i];
606 if( (c >= 0x0001) && (c <= 0x007F) )
608 writeByte(sal_Int8(c));
610 else if( c > 0x07FF )
612 writeByte(sal_Int8(0xE0 | ((c >> 12) & 0x0F)));
613 writeByte(sal_Int8(0x80 | ((c >> 6) & 0x3F)));
614 writeByte(sal_Int8(0x80 | ((c >> 0) & 0x3F)));
616 else
618 writeByte(sal_Int8(0xC0 | ((c >> 6) & 0x1F)));
619 writeByte(sal_Int8(0x80 | ((c >> 0) & 0x3F)));
624 // XActiveDataSource
625 void ODataOutputStream::setOutputStream(const Reference< XOutputStream > & aStream)
627 if( m_output != aStream ) {
628 m_output = aStream;
629 m_bValidStream = m_output.is();
631 Reference < XConnectable > succ( m_output , UNO_QUERY );
632 setSuccessor( succ );
636 Reference< XOutputStream > ODataOutputStream::getOutputStream()
638 return m_output;
642 // XDataSink
643 void ODataOutputStream::setSuccessor( const Reference < XConnectable > &r )
645 /// if the references match, nothing needs to be done
646 if( m_succ != r )
648 /// store the reference for later use
649 m_succ = r;
651 if( m_succ.is() )
653 /// set this instance as the sink !
654 m_succ->setPredecessor( Reference < XConnectable > (
655 static_cast< XConnectable * >(this) ));
659 Reference < XConnectable > ODataOutputStream::getSuccessor()
661 return m_succ;
665 // XDataSource
666 void ODataOutputStream::setPredecessor( const Reference < XConnectable > &r )
668 if( r != m_pred ) {
669 m_pred = r;
670 if( m_pred.is() ) {
671 m_pred->setSuccessor( Reference< XConnectable > (
672 static_cast< XConnectable * >(this) ));
676 Reference < XConnectable > ODataOutputStream::getPredecessor()
678 return m_pred;
682 // XServiceInfo
683 OUString ODataOutputStream::getImplementationName()
685 return u"com.sun.star.comp.io.stm.DataOutputStream"_ustr;
688 // XServiceInfo
689 sal_Bool ODataOutputStream::supportsService(const OUString& ServiceName)
691 return cppu::supportsService(this, ServiceName);
694 // XServiceInfo
695 Sequence< OUString > ODataOutputStream::getSupportedServiceNames()
697 return { u"com.sun.star.io.DataOutputStream"_ustr };
700 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
701 io_ODataOutputStream_get_implementation(
702 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
704 return cppu::acquire(new ODataOutputStream());
707 namespace {
709 struct equalObjectContainer_Impl
711 bool operator()(const Reference< XInterface > & s1,
712 const Reference< XInterface > & s2) const
714 return s1 == s2;
719 struct hashObjectContainer_Impl
721 size_t operator()(const Reference< XInterface > & xRef) const
723 return reinterpret_cast<size_t>(xRef.get());
729 typedef std::unordered_map
731 Reference< XInterface >,
732 sal_Int32,
733 hashObjectContainer_Impl,
734 equalObjectContainer_Impl
735 > ObjectContainer_Impl;
737 namespace {
739 class OObjectOutputStream:
740 public ImplInheritanceHelper<
741 ODataOutputStream, /* parent */
742 XObjectOutputStream, XMarkableStream >
744 public:
745 OObjectOutputStream()
746 : m_nMaxId(0) ,
747 m_bValidMarkable(false)
751 public:
752 // XOutputStream
753 virtual void SAL_CALL writeBytes(const Sequence< sal_Int8 >& aData) override
754 { ODataOutputStream::writeBytes( aData ); }
756 virtual void SAL_CALL flush() override
757 { ODataOutputStream::flush(); }
759 virtual void SAL_CALL closeOutput() override
760 { ODataOutputStream::closeOutput(); }
762 public:
763 // XDataOutputStream
764 virtual void SAL_CALL writeBoolean(sal_Bool Value) override
765 { ODataOutputStream::writeBoolean( Value ); }
766 virtual void SAL_CALL writeByte(sal_Int8 Value) override
767 { ODataOutputStream::writeByte( Value ); }
768 virtual void SAL_CALL writeChar(sal_Unicode Value) override
769 { ODataOutputStream::writeChar( Value ); }
770 virtual void SAL_CALL writeShort(sal_Int16 Value) override
771 { ODataOutputStream::writeShort( Value ); }
772 virtual void SAL_CALL writeLong(sal_Int32 Value) override
773 { ODataOutputStream::writeLong( Value ); }
774 virtual void SAL_CALL writeHyper(sal_Int64 Value) override
775 { ODataOutputStream::writeHyper( Value ); }
776 virtual void SAL_CALL writeFloat(float Value) override
777 { ODataOutputStream::writeFloat( Value ); }
778 virtual void SAL_CALL writeDouble(double Value) override
779 { ODataOutputStream::writeDouble( Value ); }
780 virtual void SAL_CALL writeUTF(const OUString& Value) override
781 { ODataOutputStream::writeUTF( Value );}
783 // XObjectOutputStream
784 virtual void SAL_CALL writeObject( const Reference< XPersistObject > & r ) override;
786 public: // XMarkableStream
787 virtual sal_Int32 SAL_CALL createMark() override;
788 virtual void SAL_CALL deleteMark(sal_Int32 Mark) override;
789 virtual void SAL_CALL jumpToMark(sal_Int32 nMark) override;
790 virtual void SAL_CALL jumpToFurthest() override;
791 virtual sal_Int32 SAL_CALL offsetToMark(sal_Int32 nMark) override;
793 public: // XServiceInfo
794 OUString SAL_CALL getImplementationName() override;
795 Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
796 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
798 private:
799 void connectToMarkable();
800 private:
801 ObjectContainer_Impl m_mapObject;
802 sal_Int32 m_nMaxId;
803 Reference< XMarkableStream > m_rMarkable;
804 bool m_bValidMarkable;
809 void OObjectOutputStream::writeObject( const Reference< XPersistObject > & xPObj )
812 connectToMarkable();
813 bool bWriteObj = false;
814 // create Mark to write length of info
815 sal_uInt32 nInfoLenMark = m_rMarkable->createMark();
817 // length of the info data (is later rewritten)
818 OObjectOutputStream::writeShort( 0 );
820 // write the object identifier
821 if( xPObj.is() )
823 Reference< XInterface > rX( xPObj , UNO_QUERY );
825 ObjectContainer_Impl::const_iterator aIt
826 = m_mapObject.find( rX );
827 if( aIt == m_mapObject.end() )
829 // insert new object in hash table
830 m_mapObject[ rX ] = ++m_nMaxId;
831 ODataOutputStream::writeLong( m_nMaxId );
832 ODataOutputStream::writeUTF( xPObj->getServiceName() );
833 bWriteObj = true;
835 else
837 ODataOutputStream::writeLong( (*aIt).second );
838 ODataOutputStream::writeUTF( OUString() );
841 else
843 ODataOutputStream::writeLong( 0 );
844 ODataOutputStream::writeUTF( OUString() );
847 sal_uInt32 nObjLenMark = m_rMarkable->createMark();
848 ODataOutputStream::writeLong( 0 );
850 sal_Int32 nInfoLen = m_rMarkable->offsetToMark( nInfoLenMark );
851 m_rMarkable->jumpToMark( nInfoLenMark );
852 // write length of the info data
853 ODataOutputStream::writeShort( static_cast<sal_Int16>(nInfoLen) );
854 // jump to the end of the stream
855 m_rMarkable->jumpToFurthest();
857 if( bWriteObj )
858 xPObj->write( Reference< XObjectOutputStream > (
859 static_cast< XObjectOutputStream * >(this) ) );
861 sal_Int32 nObjLen = m_rMarkable->offsetToMark( nObjLenMark ) -4;
862 m_rMarkable->jumpToMark( nObjLenMark );
863 // write length of the info data
864 ODataOutputStream::writeLong( nObjLen );
865 // jump to the end of the stream
866 m_rMarkable->jumpToFurthest();
868 m_rMarkable->deleteMark( nObjLenMark );
869 m_rMarkable->deleteMark( nInfoLenMark );
873 void OObjectOutputStream::connectToMarkable()
875 if( m_bValidMarkable )
876 return;
878 if( ! m_bValidStream )
879 throw NotConnectedException();
881 // find the markable stream !
882 Reference< XInterface > rTry(m_output);
883 while( true ) {
884 if( ! rTry.is() )
886 throw NotConnectedException();
888 Reference < XMarkableStream > markable( rTry , UNO_QUERY );
889 if( markable.is() )
891 m_rMarkable = std::move(markable);
892 break;
894 Reference < XActiveDataSource > source( rTry , UNO_QUERY );
895 rTry = source;
897 m_bValidMarkable = true;
901 sal_Int32 OObjectOutputStream::createMark()
903 connectToMarkable(); // throws an exception, if a markable is not connected !
905 return m_rMarkable->createMark();
908 void OObjectOutputStream::deleteMark(sal_Int32 Mark)
910 if( ! m_bValidMarkable )
912 throw NotConnectedException();
914 m_rMarkable->deleteMark( Mark );
917 void OObjectOutputStream::jumpToMark(sal_Int32 nMark)
919 if( ! m_bValidMarkable )
921 throw NotConnectedException();
923 m_rMarkable->jumpToMark( nMark );
927 void OObjectOutputStream::jumpToFurthest()
929 connectToMarkable();
930 m_rMarkable->jumpToFurthest();
933 sal_Int32 OObjectOutputStream::offsetToMark(sal_Int32 nMark)
935 if( ! m_bValidMarkable )
937 throw NotConnectedException();
939 return m_rMarkable->offsetToMark( nMark );
942 // XServiceInfo
943 OUString OObjectOutputStream::getImplementationName()
945 return u"com.sun.star.comp.io.stm.ObjectOutputStream"_ustr;
948 // XServiceInfo
949 sal_Bool OObjectOutputStream::supportsService(const OUString& ServiceName)
951 return cppu::supportsService(this, ServiceName);
954 // XServiceInfo
955 Sequence< OUString > OObjectOutputStream::getSupportedServiceNames()
957 return { u"com.sun.star.io.ObjectOutputStream"_ustr };
960 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
961 io_OObjectOutputStream_get_implementation(
962 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
964 return cppu::acquire(new OObjectOutputStream());
967 namespace {
969 class OObjectInputStream:
970 public ImplInheritanceHelper<
971 ODataInputStream, /* parent */
972 XObjectInputStream, XMarkableStream >
974 public:
975 explicit OObjectInputStream( const Reference < XComponentContext > &r)
976 : m_rSMgr( r->getServiceManager() )
977 , m_rCxt( r )
978 , m_bValidMarkable(false)
982 public: // XInputStream
983 virtual sal_Int32 SAL_CALL readBytes(Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead) override
984 { return ODataInputStream::readBytes( aData , nBytesToRead ); }
986 virtual sal_Int32 SAL_CALL readSomeBytes(Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) override
987 { return ODataInputStream::readSomeBytes( aData, nMaxBytesToRead ); }
989 virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
990 { ODataInputStream::skipBytes( nBytesToSkip ); }
992 virtual sal_Int32 SAL_CALL available() override
993 { return ODataInputStream::available(); }
995 virtual void SAL_CALL closeInput() override
996 { ODataInputStream::closeInput(); }
998 public: // XDataInputStream
999 virtual sal_Int8 SAL_CALL readBoolean() override
1000 { return ODataInputStream::readBoolean(); }
1001 virtual sal_Int8 SAL_CALL readByte() override
1002 { return ODataInputStream::readByte(); }
1003 virtual sal_Unicode SAL_CALL readChar() override
1004 { return ODataInputStream::readChar(); }
1005 virtual sal_Int16 SAL_CALL readShort() override
1006 { return ODataInputStream::readShort(); }
1007 virtual sal_Int32 SAL_CALL readLong() override
1008 { return ODataInputStream::readLong(); }
1009 virtual sal_Int64 SAL_CALL readHyper() override
1010 { return ODataInputStream::readHyper(); }
1011 virtual float SAL_CALL readFloat() override
1012 { return ODataInputStream::readFloat(); }
1013 virtual double SAL_CALL readDouble() override
1014 { return ODataInputStream::readDouble(); }
1015 virtual OUString SAL_CALL readUTF() override
1016 { return ODataInputStream::readUTF(); }
1018 public: // XObjectInputStream
1019 virtual Reference< XPersistObject > SAL_CALL readObject( ) override;
1021 public: // XMarkableStream
1022 virtual sal_Int32 SAL_CALL createMark() override;
1023 virtual void SAL_CALL deleteMark(sal_Int32 Mark) override;
1024 virtual void SAL_CALL jumpToMark(sal_Int32 nMark) override;
1025 virtual void SAL_CALL jumpToFurthest() override;
1026 virtual sal_Int32 SAL_CALL offsetToMark(sal_Int32 nMark) override;
1028 public: // XServiceInfo
1029 OUString SAL_CALL getImplementationName() override;
1030 Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
1031 sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
1033 private:
1034 void connectToMarkable();
1035 private:
1036 Reference < XMultiComponentFactory > m_rSMgr;
1037 Reference < XComponentContext > m_rCxt;
1038 bool m_bValidMarkable;
1039 Reference < XMarkableStream > m_rMarkable;
1040 std::vector < Reference< XPersistObject > > m_aPersistVector;
1046 Reference< XPersistObject > OObjectInputStream::readObject()
1048 // check if chain contains a XMarkableStream
1049 connectToMarkable();
1051 Reference< XPersistObject > xLoadedObj;
1053 // create Mark to skip newer versions
1054 sal_uInt32 nMark = m_rMarkable->createMark();
1055 // length of the data
1056 sal_Int32 nLen = static_cast<sal_uInt16>(ODataInputStream::readShort());
1057 if( nLen < 0xc )
1059 throw WrongFormatException();
1062 // read the object identifier
1063 sal_uInt32 nId = readLong();
1065 // the name of the persist model
1066 // MM ???
1067 OUString aName = readUTF();
1069 // Read the length of the object
1070 sal_Int32 nObjLen = readLong();
1071 if( 0 == nId && 0 != nObjLen )
1073 throw WrongFormatException();
1076 // skip data of new version
1077 skipBytes( nLen - m_rMarkable->offsetToMark( nMark ) );
1079 bool bLoadSuccessful = true;
1080 if( nId )
1082 if( !aName.isEmpty() )
1084 // load the object
1085 Reference< XInterface > x = m_rSMgr->createInstanceWithContext( aName, m_rCxt );
1086 xLoadedObj.set( x, UNO_QUERY );
1087 if( xLoadedObj.is() )
1089 sal_uInt32 nSize = m_aPersistVector.size();
1090 if( nSize <= nId )
1092 // grow to the right size
1093 Reference< XPersistObject > xEmpty;
1094 m_aPersistVector.insert( m_aPersistVector.end(), nId - nSize + 1, xEmpty );
1097 m_aPersistVector[nId] = xLoadedObj;
1098 xLoadedObj->read( Reference< XObjectInputStream >(
1099 static_cast< XObjectInputStream * >(this) ) );
1101 else
1103 // no service with this name could be instantiated
1104 bLoadSuccessful = false;
1107 else {
1108 if (nId >= m_aPersistVector.size())
1110 // id unknown, load failure !
1111 bLoadSuccessful = false;
1113 else
1115 // Object has already been read,
1116 xLoadedObj = m_aPersistVector[nId];
1121 // skip to the position behind the object
1122 skipBytes( nObjLen + nLen - m_rMarkable->offsetToMark( nMark ) );
1123 m_rMarkable->deleteMark( nMark );
1125 if( ! bLoadSuccessful )
1127 throw WrongFormatException();
1129 return xLoadedObj;
1133 void OObjectInputStream::connectToMarkable()
1135 if( m_bValidMarkable ) return;
1137 if( ! m_bValidStream )
1139 throw NotConnectedException( );
1142 // find the markable stream !
1143 Reference< XInterface > rTry(m_input);
1144 while( true ) {
1145 if( ! rTry.is() )
1147 throw NotConnectedException( );
1149 Reference< XMarkableStream > markable( rTry , UNO_QUERY );
1150 if( markable.is() )
1152 m_rMarkable = std::move(markable);
1153 break;
1155 Reference < XActiveDataSink > sink( rTry , UNO_QUERY );
1156 rTry = sink;
1158 m_bValidMarkable = true;
1161 sal_Int32 OObjectInputStream::createMark()
1163 connectToMarkable(); // throws an exception, if a markable is not connected !
1165 return m_rMarkable->createMark();
1168 void OObjectInputStream::deleteMark(sal_Int32 Mark)
1170 if( ! m_bValidMarkable )
1172 throw NotConnectedException();
1174 m_rMarkable->deleteMark( Mark );
1177 void OObjectInputStream::jumpToMark(sal_Int32 nMark)
1179 if( ! m_bValidMarkable )
1181 throw NotConnectedException();
1183 m_rMarkable->jumpToMark( nMark );
1185 void OObjectInputStream::jumpToFurthest()
1187 connectToMarkable();
1188 m_rMarkable->jumpToFurthest();
1191 sal_Int32 OObjectInputStream::offsetToMark(sal_Int32 nMark)
1193 if( ! m_bValidMarkable )
1195 throw NotConnectedException();
1197 return m_rMarkable->offsetToMark( nMark );
1200 // XServiceInfo
1201 OUString OObjectInputStream::getImplementationName()
1203 return u"com.sun.star.comp.io.stm.ObjectInputStream"_ustr;
1206 // XServiceInfo
1207 sal_Bool OObjectInputStream::supportsService(const OUString& ServiceName)
1209 return cppu::supportsService(this, ServiceName);
1212 // XServiceInfo
1213 Sequence< OUString > OObjectInputStream::getSupportedServiceNames()
1215 return { u"com.sun.star.io.ObjectInputStream"_ustr };
1218 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
1219 io_OObjectInputStream_get_implementation(
1220 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
1222 return cppu::acquire(new OObjectInputStream(context));
1228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */