editeng/source/editeng/impedit4.cxx           |    2 +-
 include/tools/stream.hxx                      |   16 +++++++++++-----
 svx/source/svdraw/svdpdf.cxx                  |   11 ++++++-----
 sw/source/ui/dbui/createaddresslistdialog.cxx |    2 +-
 sw/source/ui/index/cnttab.cxx                 |    4 ++--
 tools/source/stream/stream.cxx                |    4 ++--
 tools/source/stream/strmunx.cxx               |    4 +++-
 tools/source/stream/strmwnt.cxx               |    4 +++-
 8 files changed, 29 insertions(+), 18 deletions(-)

New commits:
commit a9503c4cf82f5ff2f32170d3c55b57f89d5c42d0
Author:     Noel Grandin <[email protected]>
AuthorDate: Wed Feb 25 10:31:24 2026 +0200
Commit:     Noel Grandin <[email protected]>
CommitDate: Thu Feb 26 16:57:00 2026 +0100

    SvFileStream should not need to specify encoding on per-call basis
    
    Starts a process of converting the call-sites of SvFileStream
    to just write data, relying on the stream being set
    into the correct encoding somewhere during initialisation.
    
    In this commit, I change WriteByteStringLine to do that.
    For most of the call sites, they either:
    (a) use the stream encoding explicitly
    (b) already set the stream to the encoding being used at the call-site
    
    Only in buildFontMenuName did I need to make the stream explicitly
    set the stream encoding.
    
    Also add a utility constructor to SvFileStream to make it easy
    to set the stream in the correct mode at open-time.
    Ideally a stream should be set into the correct encoding
    at open time and never changed.
    
    Change-Id: I57afa74807e15320a176a05d43c94c377dad4561
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/200289
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <[email protected]>

diff --git a/editeng/source/editeng/impedit4.cxx 
b/editeng/source/editeng/impedit4.cxx
index 8801ecdd0b8f..ce5201c17b68 100644
--- a/editeng/source/editeng/impedit4.cxx
+++ b/editeng/source/editeng/impedit4.cxx
@@ -252,7 +252,7 @@ ErrCode ImpEditEngine::WriteText( SvStream& rOutput, 
EditSelection aSel )
                 nEndPos = aSel.Max().GetIndex();
         }
         OUString aTmpStr = EditDoc::GetParaAsString( pNode, nStartPos, nEndPos 
);
-        rOutput.WriteByteStringLine( aTmpStr, rOutput.GetStreamEncoding() );
+        rOutput.WriteByteStringLine( aTmpStr );
     }
 
     return rOutput.GetError();
diff --git a/include/tools/stream.hxx b/include/tools/stream.hxx
index 9d00fb91fc23..a1d88674bff6 100644
--- a/include/tools/stream.hxx
+++ b/include/tools/stream.hxx
@@ -309,7 +309,10 @@ public:
     */
     bool            ReadByteStringLine( OUString& rStr, rtl_TextEncoding 
eSrcEncoding,
                                         sal_Int32 nMaxBytesToRead = 0xFFFE );
-    bool            WriteByteStringLine( std::u16string_view rStr, 
rtl_TextEncoding eDestEncoding );
+
+    /** Write a line of bytes. Uses stream encoding set on the stream.
+    */
+    bool            WriteByteStringLine( std::u16string_view rStr );
 
     /// Switch to no endian swapping and write 0xfeff
     void            StartWritingUnicodeText();
@@ -584,10 +587,13 @@ private:
     virtual void    FlushData() override;
 
 public:
-                    // Switches to Read StreamMode on failed attempt of Write 
opening
-                    SvFileStream( const OUString& rFileName, StreamMode 
eOpenMode );
-                    SvFileStream();
-                    virtual ~SvFileStream() override;
+    /*
+     * @param eOpenMode Switches to Read StreamMode on failed attempt of Write 
opening
+     * @param eStreamEncoding if not specified, defaults to 
osl_getThreadTextEncoding()
+    */
+    SvFileStream( const OUString& rFileName, StreamMode eOpenMode, 
std::optional<rtl_TextEncoding> oStreamEncoding = {} );
+    SvFileStream();
+    virtual ~SvFileStream() override;
 
     virtual void    ResetError() override;
 
diff --git a/svx/source/svdraw/svdpdf.cxx b/svx/source/svdraw/svdpdf.cxx
index eb573ccb53c7..caa67a77c4f3 100644
--- a/svx/source/svdraw/svdpdf.cxx
+++ b/svx/source/svdraw/svdpdf.cxx
@@ -1471,23 +1471,24 @@ static OUString buildFontMenuName(const OUString& 
FontMenuNameDBUrl,
     OUString longFontName = fontName;
 
     // create FontMenuName
-    SvFileStream FontMenuNameDB(FontMenuNameDBUrl, StreamMode::READWRITE | 
StreamMode::TRUNC);
+    SvFileStream FontMenuNameDB(FontMenuNameDBUrl, StreamMode::READWRITE | 
StreamMode::TRUNC,
+                                RTL_TEXTENCODING_UTF8);
     OUString postScriptFontName = u"["_ustr + postScriptName + u"]"_ustr;
-    FontMenuNameDB.WriteByteStringLine(postScriptFontName, 
RTL_TEXTENCODING_UTF8);
+    FontMenuNameDB.WriteByteStringLine(postScriptFontName);
     SAL_INFO("sd.filter",
              "wrote basefont name: " << postScriptFontName << " to: " << 
FontMenuNameDBUrl);
     OUString setFontName = "f=" + fontName;
-    FontMenuNameDB.WriteByteStringLine(setFontName, RTL_TEXTENCODING_UTF8);
+    FontMenuNameDB.WriteByteStringLine(setFontName);
     SAL_INFO("sd.filter", "wrote family name: " << setFontName << " to: " << 
FontMenuNameDBUrl);
     if (!isSimpleFamilyName(Weight))
     {
         longFontName = fontName + " " + OUString::createFromAscii(Weight);
         OUString setLongFontName = "l=" + fontName + " " + 
OUString::createFromAscii(Weight);
-        FontMenuNameDB.WriteByteStringLine(setLongFontName, 
RTL_TEXTENCODING_UTF8);
+        FontMenuNameDB.WriteByteStringLine(setLongFontName);
         SAL_INFO("sd.filter",
                  "wrote long family name: " << setLongFontName << " to: " << 
FontMenuNameDBUrl);
         OUString styleName = "s=" + OUString::createFromAscii(Weight);
-        FontMenuNameDB.WriteByteStringLine(styleName, RTL_TEXTENCODING_UTF8);
+        FontMenuNameDB.WriteByteStringLine(styleName);
         SAL_INFO("sd.filter", "wrote style name: " << styleName << " to: " << 
FontMenuNameDBUrl);
     }
     FontMenuNameDB.Close();
diff --git a/sw/source/ui/dbui/createaddresslistdialog.cxx 
b/sw/source/ui/dbui/createaddresslistdialog.cxx
index 93a0ee900e9c..f5b7d1cedf58 100644
--- a/sw/source/ui/dbui/createaddresslistdialog.cxx
+++ b/sw/source/ui/dbui/createaddresslistdialog.cxx
@@ -415,7 +415,7 @@ void lcl_WriteValues(const std::vector<OUString> *pFields, 
SvStream* pStream)
             sLine.append("     \"" + *aIter + "\"");
         }
     }
-    pStream->WriteByteStringLine( sLine, RTL_TEXTENCODING_UTF8 );
+    pStream->WriteByteStringLine( sLine );
 }
 
 }
diff --git a/sw/source/ui/index/cnttab.cxx b/sw/source/ui/index/cnttab.cxx
index 09d75252b718..9f15955d6fee 100644
--- a/sw/source/ui/index/cnttab.cxx
+++ b/sw/source/ui/index/cnttab.cxx
@@ -4075,7 +4075,7 @@ void SwEntryBrowseBox::WriteEntries(SvStream& rOutStr)
         if(!pEntry->sComment.isEmpty())
         {
             // tdf#108910, tdf#125496 - write index entries using the utf8 
text encoding
-            rOutStr.WriteByteStringLine( Concat2View("#" + pEntry->sComment), 
RTL_TEXTENCODING_UTF8 );
+            rOutStr.WriteByteStringLine( Concat2View("#" + pEntry->sComment) );
         }
 
         OUString sWrite( pEntry->sSearch + ";" +
@@ -4088,7 +4088,7 @@ void SwEntryBrowseBox::WriteEntries(SvStream& rOutStr)
 
         if( sWrite.getLength() > 5 )
             // tdf#108910, tdf#125496 - write index entries using the utf8 
text encoding
-            rOutStr.WriteByteStringLine( sWrite, RTL_TEXTENCODING_UTF8 );
+            rOutStr.WriteByteStringLine( sWrite );
     }
 }
 
diff --git a/tools/source/stream/stream.cxx b/tools/source/stream/stream.cxx
index 01c33a3ab534..703d0e63b185 100644
--- a/tools/source/stream/stream.cxx
+++ b/tools/source/stream/stream.cxx
@@ -658,9 +658,9 @@ bool SvStream::WriteUnicodeOrByteText(std::u16string_view 
rStr, rtl_TextEncoding
     return m_nError == ERRCODE_NONE;
 }
 
-bool SvStream::WriteByteStringLine(std::u16string_view rStr, rtl_TextEncoding 
eDestEncoding)
+bool SvStream::WriteByteStringLine( std::u16string_view rStr )
 {
-    return WriteLine(OUStringToOString(rStr, eDestEncoding));
+    return WriteLine(OUStringToOString(rStr, GetStreamEncoding()));
 }
 
 bool SvStream::WriteLine(std::string_view rStr)
diff --git a/tools/source/stream/strmunx.cxx b/tools/source/stream/strmunx.cxx
index 28c548540aeb..8fc233e55d4b 100644
--- a/tools/source/stream/strmunx.cxx
+++ b/tools/source/stream/strmunx.cxx
@@ -184,8 +184,10 @@ static ErrCode GetSvError( oslFileError nErrno )
     return nRetVal;
 }
 
-SvFileStream::SvFileStream( const OUString& rFileName, StreamMode nOpenMode )
+SvFileStream::SvFileStream( const OUString& rFileName, StreamMode nOpenMode, 
std::optional<rtl_TextEncoding> oStreamEncoding )
 {
+    if (oStreamEncoding)
+        SetStreamEncoding(*oStreamEncoding);
     bIsOpen             = false;
     m_isWritable        = false;
 
diff --git a/tools/source/stream/strmwnt.cxx b/tools/source/stream/strmwnt.cxx
index aa39a7f4434e..9cdbb31d1fbd 100644
--- a/tools/source/stream/strmwnt.cxx
+++ b/tools/source/stream/strmwnt.cxx
@@ -92,8 +92,10 @@ static ErrCode GetSvError( DWORD nWntError )
     return nRetVal;
 }
 
-SvFileStream::SvFileStream( const OUString& rFileName, StreamMode nMode )
+SvFileStream::SvFileStream( const OUString& rFileName, StreamMode nMode, 
std::optional<rtl_TextEncoding> oStreamEncoding )
 {
+    if (oStreamEncoding)
+        SetStreamEncoding(*oStreamEncoding);
     bIsOpen             = false;
     nLockCounter        = 0;
     m_isWritable        = false;

Reply via email to