bump product version to 5.0.4.1
[LibreOffice.git] / io / source / TextOutputStream / TextOutputStream.cxx
blob5d678a0a73c16620730a01b31348894e5d164b3c
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 .
21 #include <osl/diagnose.h>
23 #include <uno/mapping.hxx>
25 #include <cppuhelper/factory.hxx>
26 #include <cppuhelper/implbase2.hxx>
27 #include <cppuhelper/implementationentry.hxx>
28 #include <cppuhelper/supportsservice.hxx>
30 #include <rtl/textenc.h>
31 #include <rtl/tencinfo.h>
33 #include <com/sun/star/io/XTextOutputStream2.hpp>
34 #include <com/sun/star/lang/XServiceInfo.hpp>
36 #include "services.hxx"
38 #define IMPLEMENTATION_NAME "com.sun.star.comp.io.TextOutputStream"
39 #define SERVICE_NAME "com.sun.star.io.TextOutputStream"
41 using namespace ::osl;
42 using namespace ::cppu;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::lang;
45 using namespace ::com::sun::star::io;
46 using namespace ::com::sun::star::registry;
48 namespace io_TextOutputStream
51 // Implementation XTextOutputStream
53 typedef WeakImplHelper2< XTextOutputStream2, XServiceInfo > TextOutputStreamHelper;
55 class OTextOutputStream : public TextOutputStreamHelper
57 Reference< XOutputStream > mxStream;
59 // Encoding
60 OUString mEncoding;
61 bool mbEncodingInitialized;
62 rtl_UnicodeToTextConverter mConvUnicode2Text;
63 rtl_UnicodeToTextContext mContextUnicode2Text;
65 Sequence<sal_Int8> implConvert( const OUString& rSource );
66 void checkOutputStream() throw(IOException);
68 public:
69 OTextOutputStream();
70 virtual ~OTextOutputStream();
72 // Methods XTextOutputStream
73 virtual void SAL_CALL writeString( const OUString& aString )
74 throw(IOException, RuntimeException, std::exception) SAL_OVERRIDE;
75 virtual void SAL_CALL setEncoding( const OUString& Encoding )
76 throw(RuntimeException, std::exception) SAL_OVERRIDE;
78 // Methods XOutputStream
79 virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData )
80 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
81 virtual void SAL_CALL flush( )
82 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
83 virtual void SAL_CALL closeOutput( )
84 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
86 // Methods XActiveDataSource
87 virtual void SAL_CALL setOutputStream( const Reference< XOutputStream >& aStream )
88 throw(RuntimeException, std::exception) SAL_OVERRIDE;
89 virtual Reference< XOutputStream > SAL_CALL getOutputStream( )
90 throw(RuntimeException, std::exception) SAL_OVERRIDE;
92 // Methods XServiceInfo
93 virtual OUString SAL_CALL getImplementationName() throw(std::exception) SAL_OVERRIDE;
94 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(std::exception) SAL_OVERRIDE;
95 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw(std::exception) SAL_OVERRIDE;
98 OTextOutputStream::OTextOutputStream()
99 : mbEncodingInitialized(false)
100 , mConvUnicode2Text(NULL)
101 , mContextUnicode2Text(NULL)
105 OTextOutputStream::~OTextOutputStream()
107 if( mbEncodingInitialized )
109 rtl_destroyUnicodeToTextContext( mConvUnicode2Text, mContextUnicode2Text );
110 rtl_destroyUnicodeToTextConverter( mConvUnicode2Text );
114 Sequence<sal_Int8> OTextOutputStream::implConvert( const OUString& rSource )
116 const sal_Unicode *puSource = rSource.getStr();
117 sal_Int32 nSourceSize = rSource.getLength();
119 sal_Size nTargetCount = 0;
120 sal_Size nSourceCount = 0;
122 sal_uInt32 uiInfo;
123 sal_Size nSrcCvtChars;
125 // take nSourceSize * 3 as preference
126 // this is an upper boundary for converting to utf8,
127 // which most often used as the target.
128 sal_Int32 nSeqSize = nSourceSize * 3;
130 Sequence<sal_Int8> seqText( nSeqSize );
131 sal_Char *pTarget = reinterpret_cast<char *>(seqText.getArray());
132 while( true )
134 nTargetCount += rtl_convertUnicodeToText(
135 mConvUnicode2Text,
136 mContextUnicode2Text,
137 &( puSource[nSourceCount] ),
138 nSourceSize - nSourceCount ,
139 &( pTarget[nTargetCount] ),
140 nSeqSize - nTargetCount,
141 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT |
142 RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT ,
143 &uiInfo,
144 &nSrcCvtChars);
145 nSourceCount += nSrcCvtChars;
147 if( uiInfo & RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL )
149 nSeqSize *= 2;
150 seqText.realloc( nSeqSize ); // double array size
151 pTarget = reinterpret_cast<char*>(seqText.getArray());
152 continue;
154 break;
157 // reduce the size of the buffer (fast, no copy necessary)
158 seqText.realloc( nTargetCount );
159 return seqText;
164 // XTextOutputStream
166 void OTextOutputStream::writeString( const OUString& aString )
167 throw(IOException, RuntimeException, std::exception)
169 checkOutputStream();
170 if( !mbEncodingInitialized )
172 OUString aUtf8Str("utf8");
173 setEncoding( aUtf8Str );
175 if( !mbEncodingInitialized )
176 return;
178 Sequence<sal_Int8> aByteSeq = implConvert( aString );
179 mxStream->writeBytes( aByteSeq );
182 void OTextOutputStream::setEncoding( const OUString& Encoding )
183 throw(RuntimeException, std::exception)
185 OString aOEncodingStr = OUStringToOString( Encoding, RTL_TEXTENCODING_ASCII_US );
186 rtl_TextEncoding encoding = rtl_getTextEncodingFromMimeCharset( aOEncodingStr.getStr() );
187 if( RTL_TEXTENCODING_DONTKNOW == encoding )
188 return;
190 mbEncodingInitialized = true;
191 mConvUnicode2Text = rtl_createUnicodeToTextConverter( encoding );
192 mContextUnicode2Text = rtl_createUnicodeToTextContext( mConvUnicode2Text );
193 mEncoding = Encoding;
197 // XOutputStream
198 void OTextOutputStream::writeBytes( const Sequence< sal_Int8 >& aData )
199 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
201 checkOutputStream();
202 mxStream->writeBytes( aData );
205 void OTextOutputStream::flush( )
206 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
208 checkOutputStream();
209 mxStream->flush();
212 void OTextOutputStream::closeOutput( )
213 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
215 checkOutputStream();
216 mxStream->closeOutput();
220 void OTextOutputStream::checkOutputStream()
221 throw(IOException)
223 if (! mxStream.is() )
224 throw IOException("output stream is not initialized, you have to use setOutputStream first");
229 // XActiveDataSource
231 void OTextOutputStream::setOutputStream( const Reference< XOutputStream >& aStream )
232 throw(RuntimeException, std::exception)
234 mxStream = aStream;
237 Reference< XOutputStream > OTextOutputStream::getOutputStream()
238 throw(RuntimeException, std::exception)
240 return mxStream;
244 Reference< XInterface > SAL_CALL TextOutputStream_CreateInstance(
245 SAL_UNUSED_PARAMETER const Reference< XComponentContext > &)
247 return Reference < XInterface >( ( OWeakObject * ) new OTextOutputStream() );
250 OUString TextOutputStream_getImplementationName()
252 return OUString( IMPLEMENTATION_NAME );
256 Sequence< OUString > TextOutputStream_getSupportedServiceNames()
258 Sequence< OUString > seqNames(1);
259 seqNames.getArray()[0] = SERVICE_NAME;
260 return seqNames;
263 OUString OTextOutputStream::getImplementationName() throw(std::exception)
265 return TextOutputStream_getImplementationName();
268 sal_Bool OTextOutputStream::supportsService(const OUString& ServiceName) throw(std::exception)
270 return cppu::supportsService(this, ServiceName);
273 Sequence< OUString > OTextOutputStream::getSupportedServiceNames() throw(std::exception)
275 return TextOutputStream_getSupportedServiceNames();
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */