1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: bufferedfile.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
35 #include "bufferedfile.hxx"
37 #include "filehelper.hxx"
45 BufferedOutputFile::BufferedOutputFile( rtl::OUString
const& aFileURL
, sal_uInt32 nBufferSizeHint
)
46 : m_pFile(new osl::File(aFileURL
))
49 m_buffer
.reserve( nBufferSizeHint
? nBufferSizeHint
: 512 );
52 BufferedOutputFile::~BufferedOutputFile ()
58 BufferedOutputFile::RC
BufferedOutputFile::open( sal_uInt32 uFlags
)
60 if (!m_pFile
) return E_BADF
;
62 return m_pFile
->open(uFlags
);
65 BufferedOutputFile::RC
BufferedOutputFile::close()
67 if (!m_pFile
) return E_BADF
;
70 RC rc2
= m_pFile
->close();
72 delete m_pFile
, m_pFile
= 0;
79 //BufferedOutputFile::RC BufferedOutputFile::getPos( sal_uInt64& uPos )
81 BufferedOutputFile::RC
BufferedOutputFile::write(const void *pBuffer
, sal_uInt64 uBytesToWrite
, sal_uInt64
& rBytesWritten
)
83 if (!m_pFile
) return E_BADF
;
85 if (uBytesToWrite
> m_buffer
.max_size()-m_buffer
.size())
87 // write big chunks natively
91 OSL_ASSERT(m_buffer
.empty());
92 rc
= m_pFile
->write(pBuffer
, uBytesToWrite
, rBytesWritten
);
97 // FIXME: handle out-out-memory here
98 const sal_uInt8
* data
= static_cast<const sal_uInt8
*>(pBuffer
);
99 m_buffer
.insert(m_buffer
.end(), data
, data
+ uBytesToWrite
);
100 rBytesWritten
= uBytesToWrite
;
105 BufferedOutputFile::RC
BufferedOutputFile::sync()
107 if (!m_pFile
) return E_BADF
;
109 sal_uInt64 size
= m_buffer
.size();
110 sal_uInt64 written
= 0;
112 RC rc
= m_pFile
->write(&m_buffer
.front(),size
,written
);
117 // we don't support special files where multiple write passes are needed
120 // but we try our best to stay consistent
121 m_buffer
.erase(m_buffer
.begin(),
122 m_buffer
.end() + sal::static_int_cast
<sal_uInt32
>( written
));
132 } // namespace configmgr