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 .
22 #include <cppuhelper/implbase.hxx>
23 #include <cppuhelper/supportsservice.hxx>
25 #include <rtl/textenc.h>
26 #include <rtl/tencinfo.h>
28 #include <com/sun/star/io/IOException.hpp>
29 #include <com/sun/star/io/XTextOutputStream2.hpp>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
32 namespace com::sun::star::uno
{ class XComponentContext
; }
34 using namespace ::cppu
;
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::io
;
39 // Implementation XTextOutputStream
43 class OTextOutputStream
: public WeakImplHelper
< XTextOutputStream2
, XServiceInfo
>
45 Reference
< XOutputStream
> mxStream
;
48 bool mbEncodingInitialized
;
49 rtl_UnicodeToTextConverter mConvUnicode2Text
;
50 rtl_UnicodeToTextContext mContextUnicode2Text
;
52 Sequence
<sal_Int8
> implConvert( const OUString
& rSource
);
53 /// @throws IOException
54 void checkOutputStream() const;
58 virtual ~OTextOutputStream() override
;
60 // Methods XTextOutputStream
61 virtual void SAL_CALL
writeString( const OUString
& aString
) override
;
62 virtual void SAL_CALL
setEncoding( const OUString
& Encoding
) override
;
64 // Methods XOutputStream
65 virtual void SAL_CALL
writeBytes( const Sequence
< sal_Int8
>& aData
) override
;
66 virtual void SAL_CALL
flush( ) override
;
67 virtual void SAL_CALL
closeOutput( ) override
;
69 // Methods XActiveDataSource
70 virtual void SAL_CALL
setOutputStream( const Reference
< XOutputStream
>& aStream
) override
;
71 virtual Reference
< XOutputStream
> SAL_CALL
getOutputStream( ) override
;
73 // Methods XServiceInfo
74 virtual OUString SAL_CALL
getImplementationName() override
;
75 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
76 virtual sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) override
;
81 OTextOutputStream::OTextOutputStream()
82 : mbEncodingInitialized(false)
83 , mConvUnicode2Text(nullptr)
84 , mContextUnicode2Text(nullptr)
88 OTextOutputStream::~OTextOutputStream()
90 if( mbEncodingInitialized
)
92 rtl_destroyUnicodeToTextContext( mConvUnicode2Text
, mContextUnicode2Text
);
93 rtl_destroyUnicodeToTextConverter( mConvUnicode2Text
);
97 Sequence
<sal_Int8
> OTextOutputStream::implConvert( const OUString
& rSource
)
99 const sal_Unicode
*puSource
= rSource
.getStr();
100 sal_Int32 nSourceSize
= rSource
.getLength();
102 sal_Size nTargetCount
= 0;
103 sal_Size nSourceCount
= 0;
106 sal_Size nSrcCvtChars
;
108 // take nSourceSize * 3 as preference
109 // this is an upper boundary for converting to utf8,
110 // which most often used as the target.
111 sal_Int32 nSeqSize
= nSourceSize
* 3;
113 Sequence
<sal_Int8
> seqText( nSeqSize
);
114 char *pTarget
= reinterpret_cast<char *>(seqText
.getArray());
117 nTargetCount
+= rtl_convertUnicodeToText(
119 mContextUnicode2Text
,
120 &( puSource
[nSourceCount
] ),
121 nSourceSize
- nSourceCount
,
122 &( pTarget
[nTargetCount
] ),
123 nSeqSize
- nTargetCount
,
124 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT
|
125 RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT
,
128 nSourceCount
+= nSrcCvtChars
;
130 if( uiInfo
& RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL
)
133 seqText
.realloc( nSeqSize
); // double array size
134 pTarget
= reinterpret_cast<char*>(seqText
.getArray());
140 // reduce the size of the buffer (fast, no copy necessary)
141 seqText
.realloc( nTargetCount
);
148 void OTextOutputStream::writeString( const OUString
& aString
)
151 if( !mbEncodingInitialized
)
153 setEncoding( u
"utf8"_ustr
);
155 if( !mbEncodingInitialized
)
158 Sequence
<sal_Int8
> aByteSeq
= implConvert( aString
);
159 mxStream
->writeBytes( aByteSeq
);
162 void OTextOutputStream::setEncoding( const OUString
& Encoding
)
164 OString aOEncodingStr
= OUStringToOString( Encoding
, RTL_TEXTENCODING_ASCII_US
);
165 rtl_TextEncoding encoding
= rtl_getTextEncodingFromMimeCharset( aOEncodingStr
.getStr() );
166 if( RTL_TEXTENCODING_DONTKNOW
== encoding
)
169 mbEncodingInitialized
= true;
170 mConvUnicode2Text
= rtl_createUnicodeToTextConverter( encoding
);
171 mContextUnicode2Text
= rtl_createUnicodeToTextContext( mConvUnicode2Text
);
176 void OTextOutputStream::writeBytes( const Sequence
< sal_Int8
>& aData
)
179 mxStream
->writeBytes( aData
);
182 void OTextOutputStream::flush( )
188 void OTextOutputStream::closeOutput( )
191 mxStream
->closeOutput();
195 void OTextOutputStream::checkOutputStream() const
197 if (! mxStream
.is() )
198 throw IOException(u
"output stream is not initialized, you have to use setOutputStream first"_ustr
);
204 void OTextOutputStream::setOutputStream( const Reference
< XOutputStream
>& aStream
)
209 Reference
< XOutputStream
> OTextOutputStream::getOutputStream()
214 OUString
OTextOutputStream::getImplementationName()
216 return u
"com.sun.star.comp.io.TextOutputStream"_ustr
;
219 sal_Bool
OTextOutputStream::supportsService(const OUString
& ServiceName
)
221 return cppu::supportsService(this, ServiceName
);
224 Sequence
< OUString
> OTextOutputStream::getSupportedServiceNames()
226 return { u
"com.sun.star.io.TextOutputStream"_ustr
};
231 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
232 io_OTextOutputStream_get_implementation(
233 css::uno::XComponentContext
* , css::uno::Sequence
<css::uno::Any
> const&)
235 return cppu::acquire(new OTextOutputStream());
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */